-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsetup.py
executable file
·316 lines (281 loc) · 13.4 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
import ast
import os
from pathlib import Path
import pkg_resources
# noinspection PyUnresolvedReferences
from pkg_resources.extern import packaging
import re
import setuptools
from typing import cast, Generator, Iterable, List, Optional, Tuple, Union
import urllib.parse
###############################################################################
# Some helper functions
def parse_version(fpath: Union[str, Path]) -> str:
"""
Statically parse the "__version__" number string from a python file.
TODO: Auto-append dev version based on how forward from latest release
Basically a simpler version of what setuptools_scm does but without
the added cruft and bringing the ENTIRE git repo in with the dist
See: https://github.com/pypa/setuptools_scm/blob/master/setuptools_scm/version.py
Would need to know number of commits ahead from last version tag.
"""
with open(fpath, 'r') as file_:
pt = ast.parse(file_.read())
class VersionVisitor(ast.NodeVisitor):
def __init__(self) -> None:
self.version: Optional[str] = None
def visit_Assign(self, node: ast.Assign) -> None:
for target in node.targets:
if isinstance(target, ast.Name) and target.id == "__version__":
self.version = cast(ast.Str, node.value).s
visitor = VersionVisitor()
visitor.visit(pt)
if visitor.version is None:
raise RuntimeError("Failed to find __version__!")
return visitor.version
def parse_req_strip_version(filepath: Union[str, Path]) -> List[str]:
"""
Read requirements file and return the list of requirements specified
therein but with their version aspects striped.
See pkg_resources.Requirement docs here:
https://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirement-objects
"""
filepath = Path(filepath)
# Known prefixes of lines that are definitely not requirements
# specifications.
skip_prefix_tuple = (
"#", "--index-url"
)
def filter_req_lines(_filepath: Path) -> Generator[str, None, None]:
""" Filter lines from file that are requirements. """
with open(_filepath, 'r') as _f:
for _line in _f:
_line = _line.strip()
if not _line or _line.startswith(skip_prefix_tuple):
# Empty or has a skippable prefix.
continue
elif _line.startswith('-r '):
# sub-requirements file specification, yield that file's
# req lines.
target = _filepath.parent / _line.split(" ")[1]
for _r_line in filter_req_lines(target):
yield _r_line
elif _line.startswith('-e '):
# Indicator for URL-based requirement. Look to the egg
# fragment.
frag = urllib.parse.urlparse(_line.split(' ')[1]).fragment
try:
egg = dict(
cast(Tuple[str, str], part.split('=', 1))
for part in frag.split('&')
if part # handle no fragments
)['egg']
except KeyError:
raise packaging.requirements.InvalidRequirement(
f"Failed to parse egg name from the requirements "
f"line: '{_line}'"
)
yield egg
else:
yield _line
def strip_req_specifier(
req_iter: Iterable[pkg_resources.Requirement]
) -> Generator[pkg_resources.Requirement, None, None]:
"""
Modify requirements objects to null out the specifier component.
"""
for r in req_iter:
r.specs = []
# `specifier` property is defined in extern base-class of the
# `pkg_resources.Requirement` type.
# noinspection PyTypeHints
r.specifier = packaging.specifiers.SpecifierSet() # type: ignore
yield r
return [
str(req)
for req in strip_req_specifier(
pkg_resources.parse_requirements(filter_req_lines(filepath))
)
]
PYTHON_FILE_RE = re.compile(r'.*\.(?:py[co]?)$')
def find_package_datafiles(package_dir):
""" Return a list of non-python files in package source tree
File paths are relative to the top of the package directory provided.
"""
# TODO: Add exclusion list/glob/regex parameter if necessary.
non_python = set()
for dirpath, _, fnames in os.walk(package_dir):
non_python.update([os.path.relpath(os.path.join(dirpath, fp),
package_dir)
for fp in fnames
# Things that are NOT python files
if PYTHON_FILE_RE.match(fp) is None])
return list(non_python)
def list_directory_files(dirpath, exclude_dirs=(), exclude_files=()):
"""
List files and their parent directories in the format required for the
``setup`` function ``data_files`` parameter:
...
data_files=[
('dir', 'root-relative-file-path'),
...
],
...
This function is intended to effectively install a directory located in the
source root as is (e.g. the ``etc`` directory).
:param dirpath: Base directory to start with. The directory paths returned
start with this directory.
:param exclude_dirs: sequence if directory paths (starting from ``dirpath``)
that should not be included. For example, we don't want the `bin/memex'
directory to be installed, when gathering data files for `bin`, we call
this function like:
list_directory_files('bin', ['bin/memex'])
:param exclude_files: File names to ignore in directories traversed.
"""
exclude_dirs = set(ed.strip(' /') for ed in exclude_dirs)
exclude_files = set(ef.strip() for ef in exclude_files)
file_paths = []
for dirpath, dnames, fnames in os.walk(dirpath):
# Filter out directories to be excluded
for dn in dnames:
if os.path.join(dirpath, dn) in exclude_dirs:
print("skipping:", os.path.join(dirpath, dn))
del dnames[dnames.index(dn)]
# filter out excluded files
fnames = set(fnames).difference(exclude_files)
# collect directory to file paths reference
file_paths.append(
(dirpath, [os.path.join(dirpath, fn) for fn in fnames])
)
return file_paths
################################################################################
PYTHON_SRC = 'python'
PACKAGE_NAME = "smqtk"
SETUP_DIR = Path(__file__).parent
with open(SETUP_DIR / "README.md") as f:
LONG_DESCRIPTION = f.read()
VERSION = parse_version(SETUP_DIR / PYTHON_SRC / PACKAGE_NAME / "__init__.py")
if __name__ == "__main__":
setuptools.setup(
name=PACKAGE_NAME,
version=VERSION,
description='Python toolkit for pluggable algorithms and data structures '
'for multimedia-based machine learning',
long_description=LONG_DESCRIPTION,
author='Kitware, Inc.',
author_email='[email protected]',
url='https://github.com/Kitware/SMQTK',
license='BSD 3-Clause',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Unix',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Scientific/Engineering :: Image Recognition'
],
platforms=[
'Linux',
'Max OS-X',
'Unix',
# 'Windows', # Not tested yet
],
package_dir={'': PYTHON_SRC},
packages=setuptools.find_packages(PYTHON_SRC),
package_data={
'smqtk': find_package_datafiles(os.path.join(PYTHON_SRC, 'smqtk'))
},
data_files=list_directory_files('etc'),
# Required for mypy to be able to find the installed package.
# https://mypy.readthedocs.io/en/latest/installed_packages.html#installed-packages
zip_safe=False,
install_requires=parse_req_strip_version(SETUP_DIR / "requirements" / "runtime.txt"),
extras_require={
# Various optional dependencies for plugins
'docs': parse_req_strip_version(SETUP_DIR / "requirements" / "docs.txt"),
'caffe': [
'protobuf',
'scikit-image',
],
'magic': [
'file-magic',
],
'flann': [
'pyflann>=1.8.4',
],
'libmagic': [
'file-magic',
],
'postgres': [
'psycopg2-binary',
],
'solr': [
'solrpy',
],
'girder': [
'girder-client',
],
},
# See entry_points/console_scripts as the preferred method for publishing
# executable scripts. Might have redesign how scripts are done if that is
# to be used...
# TODO: Rename camel-case scripts to ``smqtk-...`` format without camel-case
entry_points={
'smqtk_plugins': [
# Included batteries!
# Algorithms
"smqtk.algorithms.classifier._plugins = smqtk.algorithms.classifier._plugins",
"smqtk.algorithms.descriptor_generator._plugins = smqtk.algorithms.descriptor_generator._plugins",
"smqtk.algorithms.image_io._plugins = smqtk.algorithms.image_io._plugins",
"smqtk.algorithms.nn_index._plugins = smqtk.algorithms.nn_index._plugins",
"smqtk.algorithms.nn_index.hash_index._plugins = smqtk.algorithms.nn_index.hash_index._plugins",
"smqtk.algorithms.nn_index.lsh.functors._plugins = smqtk.algorithms.nn_index.lsh.functors._plugins",
"smqtk.algorithms.rank_relevancy._plugins = smqtk.algorithms.rank_relevancy._plugins",
"smqtk.algorithms.relevancy_index._plugins = smqtk.algorithms.relevancy_index._plugins",
# Representations
"smqtk.representation.classification_element._plugins"
" = smqtk.representation.classification_element._plugins",
"smqtk.representation.data_element._plugins = smqtk.representation.data_element._plugins",
"smqtk.representation.data_set._plugins = smqtk.representation.data_set._plugins",
"smqtk.representation.descriptor_element._plugins = smqtk.representation.descriptor_element._plugins",
"smqtk.representation.descriptor_set._plugins = smqtk.representation.descriptor_set._plugins",
"smqtk.representation.detection_element._plugins = smqtk.representation.detection_element._plugins",
"smqtk.representation.key_value._plugins = smqtk.representation.key_value._plugins",
# Web
"smqtk.web._plugins = smqtk.web._plugins",
],
'console_scripts': [
'classifier_kfold_validation = smqtk.bin.classifier_kfold_validation:classifier_kfold_validation',
'classifier_model_validation = smqtk.bin.classifier_model_validation:main',
'smqtk-classify-files = smqtk.bin.classifyFiles:main ',
'compute_classifications = smqtk.bin.compute_classifications:main',
'compute_hash_codes = smqtk.bin.compute_hash_codes:main',
'compute_many_descriptors = smqtk.bin.compute_many_descriptors:main',
'smqtk-compute-descriptor = smqtk.bin.computeDescriptor:main',
'smqtk-create-file-ingest = smqtk.bin.createFileIngest:main',
'smqtk-create-girder-ingest = smqtk.bin.createGirderIngest:main',
'descriptors_to_svmtrain = smqtk.bin.descriptors_to_svmtrainfile:main',
'generate_image_transform = smqtk.bin.generate_image_transform:main',
'iqr_app_model_generation = smqtk.bin.iqr_app_model_generation:main',
'iqrTrainClassifier = smqtk.bin.iqrTrainClassifier:main',
'make_balltree = smqtk.bin.make_balltree:main',
'minibatch_kmeans_clusters = smqtk.bin.minibatch_kmeans_clusters:main',
'smqtk-remove-old-files = smqtk.bin.removeOldFiles:main',
'smqtk-proxy-manager-server = smqtk.bin.proxyManagerServer:main',
'runApplication = smqtk.bin.runApplication:main',
'smqtk-summarize-plugins = smqtk.bin.summarizePlugins:main',
'train_itq = smqtk.bin.train_itq:main',
'smqtk-make-train-test-sets = smqtk.bin.make_train_test_sets:main',
'smqtk-nearest-neighbors = smqtk.bin.nearest_neighbors:main',
'smqtk-check-images = smqtk.bin.check_images:main',
'smqtk-nn-index-tool = smqtk.bin.nn_index_tool:cli_group',
]
}
)