-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsetup.py
144 lines (121 loc) · 4.26 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
#
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/
# Written by Angelos Katharopoulos <[email protected]>
#
"""Setup attention-sampling"""
from distutils.file_util import copy_file
from multiprocessing import cpu_count
from itertools import dropwhile
import os
from os import path
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
from subprocess import CalledProcessError, check_call
class TensorflowExtension(Extension, object):
@property
def path(self):
cmake = [s for s in self.sources if "CMakeLists.txt" in s][0]
return path.dirname(cmake)
def compile(self):
print("Building {}".format(self.name))
extension_dir = path.join(self.path, "build")
os.makedirs(extension_dir, exist_ok=True)
try:
check_call(
["cmake", "-DCMAKE_BUILD_TYPE=Release", ".."],
cwd=extension_dir
)
check_call(
["make", "-j{}".format(max(cpu_count()-1, 1))],
cwd=extension_dir
)
check_call(
["make", "install"],
cwd=extension_dir
)
except CalledProcessError as e:
raise RuntimeError(
"Couldn't compile and install {}".format(self.name)
) from e
class custom_build_ext(build_ext):
def build_extension(self, ext):
if isinstance(ext, TensorflowExtension):
ext.compile()
filename = self.get_ext_filename(ext.name)
if not self.dry_run:
os.makedirs(
path.join(self.build_lib, path.dirname(filename)),
exist_ok=True
)
copy_file(filename, path.join(self.build_lib, filename),
verbose=self.verbose, dry_run=self.dry_run)
else:
super(custom_build_ext, self).build_extension(ext)
def get_ext_filename(self, fullname):
return path.sep.join(fullname.split(".")) + ".so"
def collect_docstring(lines):
"""Return document docstring if it exists"""
lines = dropwhile(lambda x: not x.startswith('"""'), lines)
doc = ""
for line in lines:
doc += line
if doc.endswith('"""\n'):
break
return doc[3:-4].replace("\r", "").replace("\n", " ")
def collect_metadata():
meta = {}
with open(path.join("ats", "__init__.py")) as f:
lines = iter(f)
meta["description"] = collect_docstring(lines)
for line in lines:
if line.startswith("__"):
key, value = map(lambda x: x.strip(), line.split("="))
meta[key[2:-2]] = value[1:-1]
return meta
def get_extensions():
return [
TensorflowExtension(
"ats.ops.extract_patches.libpatches",
[
"ats/ops/extract_patches/extract_patches.h",
"ats/ops/extract_patches/extract_patches.cc",
"ats/ops/extract_patches/extract_patches.cu",
"ats/ops/extract_patches/CMakeLists.txt",
]
)
]
def get_install_requirements():
return [
"keras>=2",
"numpy"
]
def setup_package():
with open("README.rst") as f:
long_description = f.read()
meta = collect_metadata()
setup(
name="attention-sampling",
version=meta["version"],
description=meta["description"],
long_description=long_description,
maintainer=meta["maintainer"],
maintainer_email=meta["email"],
url=meta["url"],
license=meta["license"],
classifiers=[
"Intended Audience :: Science/Research",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Topic :: Scientific/Engineering",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
],
packages=find_packages(exclude=["docs", "tests", "scripts"]),
install_requires=get_install_requirements(),
ext_modules=get_extensions(),
cmdclass={"build_ext": custom_build_ext}
)
if __name__ == "__main__":
setup_package()