forked from drcandacemakedamoore/cleanX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·716 lines (621 loc) · 22 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/usr/bin/env python
import sys
import shlex
import os
import subprocess
import site
import shutil
import platform
import posixpath
import zipfile
import email
import pkg_resources
import setuptools
from glob import glob
from distutils.dir_util import copy_tree
from io import BytesIO
from string import Template
from setuptools import setup
from setuptools.command.install import install as InstallCommand
from setuptools.command.easy_install import easy_install as EZInstallCommand
from setuptools.dist import Distribution
from setuptools.wheel import Wheel
from setuptools.command.egg_info import write_requirements
from setuptools import Command
from pkg_resources import parse_version
project_dir = os.path.dirname(os.path.realpath(__file__))
# This will exclude the project directory from sys.path so that Sphinx
# doesn't get confused about where to load the sources.
# _Note_ you _must_ install the project
# before you generate documentation, otherwise it will not work.
sys.path = [x for x in sys.path if not x == project_dir]
with open('README.md', 'r') as f:
readme = f.read()
name = 'cleanX'
try:
tag = subprocess.check_output([
'git',
'--no-pager',
'describe',
'--abbrev=0',
'--tags',
]).strip().decode()
except subprocess.CalledProcessError as e:
print(e.output)
tag = 'v0.0.0'
version = tag[1:]
def _convert_metadata(_, zf, destination_eggdir, dist_info, egg_info):
def get_metadata(name):
with zf.open(posixpath.join(dist_info, name)) as fp:
value = fp.read().decode('utf-8')
return email.parser.Parser().parsestr(value)
wheel_metadata = get_metadata('WHEEL')
# Check wheel format version is supported.
wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))
wheel_v1 = (
parse_version('1.0') <= wheel_version < parse_version('2.0dev0')
)
if not wheel_v1:
raise ValueError(
'unsupported wheel format version: %s' % wheel_version)
# Extract to target directory.
# PATCH(wvxvw): This function is copied from wheel.py.
# There are some issues. Specifically, this place will get conflicted with
# itself because it's trying to create a directory that it already
# created earlier...
try:
os.mkdir(destination_eggdir)
except FileExistsError:
pass
zf.extractall(destination_eggdir)
# Convert metadata.
dist_info = os.path.join(destination_eggdir, dist_info)
dist = pkg_resources.Distribution.from_location(
destination_eggdir, dist_info,
metadata=pkg_resources.PathMetadata(destination_eggdir, dist_info),
)
# Note: Evaluate and strip markers now,
# as it's difficult to convert back from the syntax:
# foobar; "linux" in sys_platform and extra == 'test'
def raw_req(req):
req.marker = None
return str(req)
install_requires = list(sorted(map(raw_req, dist.requires())))
extras_require = {
extra: sorted(
req
for req in map(raw_req, dist.requires((extra,)))
if req not in install_requires
)
for extra in dist.extras
}
try:
# This used to be outside try-except, but, as this function
# ultimately doesn't care if whatever it does succeeds... why
# not put this here too, as it fails anyways because one of
# the directories had some files in them.
# Eventually, we just need to create wheels ourselves, not
# relying on `pip` and its friends, and life will be a
# lot easier.
os.rename(dist_info, egg_info)
os.rename(
os.path.join(egg_info, 'METADATA'),
os.path.join(egg_info, 'PKG-INFO'),
)
setup_dist = setuptools.Distribution(
attrs=dict(
install_requires=install_requires,
extras_require=extras_require,
),
)
write_requirements(
setup_dist.get_command_obj('egg_info'),
None,
os.path.join(egg_info, 'requires.txt'),
)
except Exception as e:
# The original function didn't care about exceptions here
# either. Turns out, all this work it did to store the
# metatada, and then: whatever, who cares if it was stored,
# right?
print(e)
Wheel._convert_metadata = _convert_metadata
class TestCommand(Command):
user_options = [
('pytest-args=', 'a', 'Arguments to pass into py.test'),
('fast', 'f', (
'Don\'t install dependencies, test in the current environment'
)
),
]
def initialize_options(self):
self.pytest_args = ''
self.fast = False
def finalize_options(self):
self.test_args = []
self.test_suite = True
def prepare(self):
recs = self.distribution.tests_require
if os.environ.get('CONDA_DEFAULT_ENV'):
if recs:
result = subprocess.call([
'conda',
'install',
'-y',
'-c', 'conda-forge',
] + recs
)
if result:
raise RuntimeError('Cannot install test requirements')
else:
test_dist = Distribution()
test_dist.install_requires = recs
ezcmd = EZInstallCommand(test_dist)
ezcmd.initialize_options()
ezcmd.args = recs
ezcmd.always_copy = True
ezcmd.finalize_options()
ezcmd.run()
site.main()
def run(self):
if not self.fast:
self.prepare()
self.run_tests()
class PyTest(TestCommand):
description = 'run unit tests'
def run_tests(self):
import pytest
if self.fast:
here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
errno = pytest.main(shlex.split(self.pytest_args))
sys.exit(errno)
class Pep8(TestCommand):
description = 'validate sources against PEP8'
def run_tests(self):
from pycodestyle import StyleGuide
package_dir = os.path.dirname(os.path.abspath(__file__))
sources = glob(
os.path.join(package_dir, 'cleanX', '**/*.py'),
recursive=True,
)
style_guide = StyleGuide(paths=sources)
options = style_guide.options
report = style_guide.check_files()
report.print_statistics()
if report.total_errors:
if options.count:
sys.stderr.write(str(report.total_errors) + '\n')
sys.exit(1)
class SphinxApiDoc(Command):
description = 'run apidoc to generate documentation'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from sphinx.ext.apidoc import main
src = os.path.join(project_dir, 'docs')
special = (
'index.rst',
'cli.rst',
'developers.rst',
'medical-professionals.rst',
)
for f in glob(os.path.join(src, '*.rst')):
for end in special:
if f.endswith(end):
os.utime(f, None)
break
else:
os.unlink(f)
sys.exit(main([
'-o', src,
'-f',
os.path.join(project_dir, 'cleanX'),
'--separate',
]))
class AnacondaUpload(Command):
description = 'upload packages for Anaconda'
user_options = [
('token=', 't', 'Anaconda token'),
('package=', 'p', 'Package to upload'),
]
def initialize_options(self):
self.token = None
self.package = None
def finalize_options(self):
if (self.token is None) or (self.package is None):
sys.stderr.write('Token and package are required\n')
raise SystemExit(2)
def run(self):
env = dict(os.environ)
env['ANACONDA_API_TOKEN'] = self.token
upload = glob(self.package)[0]
sys.stderr.write('Uploading: {}\n'.format(upload))
args = ['upload', '--force', '--label', 'main', upload]
try:
proc = subprocess.Popen(
['anaconda'] + args,
env=env,
stderr=subprocess.PIPE,
)
except FileNotFoundError:
for elt in os.environ.get('PATH', '').split(os.pathsep):
found = False
sys.stderr.write('Searching for anaconda: {!r}\n'.format(elt))
base = os.path.basename(elt)
if base == 'condabin':
# My guess is conda is adding path to shell
# profile with backslashes. Wouldn't be the first
# time they do something like this...
sub = os.path.join(os.path.dirname(elt), 'conda', 'bin')
sys.stderr.write(
'Anacondas hiding place: {}\n'.format(sub),
)
sys.stderr.write(
' {}: {}\n'.format(elt, os.path.isdir(elt)),
)
sys.stderr.write(
' {}: {}\n'.format(sub, os.path.isdir(sub)),
)
if os.path.isdir(sub):
elt = sub
executable = os.path.join(elt, 'anaconda')
exists = os.path.isfile(executable)
sys.stderr.write(
' {}: {}\n'.format(executable, exists),
)
sys.stderr.write(' Possible matches:\n')
for g in glob(os.path.join(elt, '*anaconda*')):
sys.stderr.write(' {}\n'.format(g))
elif base == 'miniconda':
# Another thing that might happen is that whoever
# configured our environment forgot to add
# miniconda/bin messed up the directory nam somehow
minibin = os.path.join(elt, 'bin')
if os.path.isdir(minibin):
sys.stderr.write(
'Maybe anaconda is here:{}\n'.format(minibin),
)
elt = minibin
for p in glob(os.path.join(elt, 'anaconda')):
sys.stderr.write('Found anaconda: {}'.format(p))
anaconda = p
found = True
break
if found:
proc = subprocess.Popen(
[anaconda] + args,
env=env,
stderr=subprocess.PIPE,
)
break
else:
import traceback
traceback.print_exc()
raise
_, err = proc.communicate()
if proc.returncode:
sys.stderr.write('Upload to Anaconda failed\n')
sys.stderr.write('Stderr:\n')
for line in err.decode().split('\n'):
sys.stderr.write(line)
sys.stderr.write('\n')
raise SystemExit(1)
class GenerateCondaYaml(Command):
description = 'generate metadata for conda package'
user_options = [(
'target-python=',
't',
'Python version to build the package for',
)]
user_options = [(
'target-conda=',
'c',
'Conda version to build the package for',
)]
def initialize_options(self):
self.target_python = None
self.target_conda = None
def finalize_options(self):
if self.target_python is None:
maj, min, patch = sys.version.split(maxsplit=1)[0].split('.')
self.target_python = '{}.{}'.format(maj, min)
if self.target_conda is None:
conda_exe = os.environ.get('CONDA_EXE', 'conda')
self.target_conda = subprocess.check_output(
[conda_exe, '--version'],
).split()[-1].decode()
def run(self):
tpls = glob(os.path.join(project_dir, 'conda-pkg/*.in'))
for tpl_path in tpls:
if tpl_path.endswith('env.yml.in'):
continue
with open(tpl_path) as f:
tpl = Template(f.read())
dst_path = tpl_path[:-3]
with open(dst_path, 'w') as f:
f.write(tpl.substitute(
version=version,
tag=tag,
python_version=self.target_python,
conda_version=self.target_conda,
))
class FindEgg(Command):
description = 'find Eggs built by this script'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
print(glob('./dist/*.egg')[0])
class GenCondaEnv(Command):
description = (
'generate YAML file with requirements for conda environmnent'
)
user_options = [(
'output=',
'o',
'File to save the environmnent description',
)]
def initialize_options(self):
self.output = None
def finalize_options(self):
if self.output is None:
self.output = 'cleanx-env-{}-py{}{}.yml'.format(
platform.system().lower(),
sys.version_info[0],
sys.version_info[1],
)
def run(self):
if os.environ.get('CONDA_DEFAULT_ENV') is None:
raise RuntimeError(
'This command can only run in conda environmnent',
)
env_tpl_path = os.path.join(
os.path.dirname(__file__),
'conda-pkg',
'env.yml.in',
)
with open(env_tpl_path) as f:
tpl = Template(f.read())
conda_recs = []
# TODO(wvxvw): Add flags to also include extras
all_recs = (
self.distribution.install_requires +
self.distribution.tests_require +
self.distribution.setup_requires
)
for rec in all_recs:
result = subprocess.check_output(['conda', 'list', '-e', rec])
for line in BytesIO(result):
if not line.startswith(b'#'):
conda_recs.append(line.strip().decode())
break
else:
raise RuntimeError(
'Missing {}\n'.format(rec) +
'run "conda install -c conda-forge {}"'.format(rec),
)
output_contents = tpl.substitute(
env_name=os.path.splitext(self.output)[0],
conda_recs='\n - '.join(conda_recs),
)
with open(self.output, 'w') as f:
f.write(output_contents)
class Install(InstallCommand):
def run(self):
if os.environ.get('CONDA_DEFAULT_ENV'):
# Apparently, we need to specify this. You'd think that a
# sane package installer would leave your Python alone,
# and yet...
frozen = 'python={}.{}'.format(*sys.version_info[:2])
conda = subprocess.check_output(
['conda', '--version'],
).replace(' ', '=')
packages = subprocess.check_output(['conda', 'list', '--export'])
cmd = [
'conda',
'install', '-y',
'--strict-channel-priority',
'--override-channels',
'-c', 'conda-forge',
'conda-build',
'conda-verify',
frozen,
conda,
]
for line in packages.split(b'\n'):
if line.startswith(b'conda-build='):
break
else:
if subprocess.call(cmd):
raise RuntimeError('Cannot install conda-build')
shutil.rmtree(
os.path.join(project_dir, 'dist'),
ignore_errors=True,
)
shutil.rmtree(
os.path.join(project_dir, 'build'),
ignore_errors=True,
)
cmd = [
'conda',
'build',
'--no-anaconda-upload',
'--override-channels',
'-c', 'conda-forge',
os.path.join(project_dir, 'conda-pkg'),
]
if subprocess.call(cmd):
raise RuntimeError('Couldn\'t build {} package'.format(name))
cmd = [
'conda',
'install',
'--strict-channel-priority',
'--override-channels',
'-c', 'conda-forge',
'--use-local',
'--update-deps',
'--force-reinstall',
'-y',
'cleanx',
frozen,
conda,
]
if subprocess.call(cmd):
raise RuntimeError('Couldn\'t install {} package'.format(name))
else:
# TODO(wvxvw): Find a way to avoid using subprocess to do
# this
if subprocess.call([sys.executable, __file__, 'bdist_egg']):
raise RuntimeError('Couldn\'t build {} package'.format(name))
egg = glob(os.path.join(project_dir, 'dist', '*.egg'))[0]
# TODO(wvxvw): Use EZInstallCommand instead
if subprocess.call([
sys.executable,
__file__,
'easy_install',
'--always-copy',
egg
]):
raise RuntimeError('Couldn\'t install {} package'.format(name))
package_dir = os.path.dirname(os.path.abspath(__file__))
egg_info = os.path.join(package_dir, 'cleanX.egg-info')
# Apparently, this is only set if we are in bdist_xxx
if self.root:
# PyPA members run setup.py install inside setup.py
# bdist_wheel. Because we don't do what a typical
# install command would, and they rely on a bunch of
# side effects of a typical install command, we need
# to pretend that install happened in a way that they
# expect.
egg_info_cmd = self.distribution.get_command_obj(
'install_egg_info',
)
egg_info_cmd.ensure_finalized()
make_pypa_happy = egg_info_cmd.target
package_contents = os.path.join(
package_dir,
'build',
'lib',
)
copy_tree(egg_info, make_pypa_happy)
copy_tree(package_contents, self.root)
class InstallDev(Install):
def run(self):
super().run()
if os.environ.get('CONDA_DEFAULT_ENV'):
frozen = 'python={}.{}'.format(*sys.version_info[:2])
cmd = [
'conda',
'install',
'-c', 'conda-forge',
'-y',
frozen,
] + self.distribution.extras_require['dev']
if subprocess.call(cmd):
raise RuntimeError('Couldn\'t install {} package'.format(name))
else:
extras_dist = Distribution()
extras_dist.install_requires = self.distribution.extras_require['dev']
ezcmd = EZInstallCommand(extras_dist)
ezcmd.initialize_options()
ezcmd.args = self.distribution.extras_require['dev']
ezcmd.always_copy = True
ezcmd.finalize_options()
ezcmd.run()
def install_requires():
if os.environ.get('CONDA_DEFAULT_ENV'):
return [
'pandas',
'numpy',
'matplotlib',
'pillow',
'tesserocr',
'opencv',
]
return [
'pandas',
'numpy',
'matplotlib',
'pillow',
'tesserocr',
'opencv-python',
'pytz',
]
def dev_deps():
if os.environ.get('CONDA_DEFAULT_ENV'):
return [
'wheel',
'sphinx',
'pytest',
'pycodestyle',
'click',
'pydicom',
'SimpleITK',
]
return [
'wheel',
'sphinx',
'pytest',
'codestyle',
'click',
'pydicom',
'SimpleITK',
]
# If we don't do this, we cannot run tests that involve
# multiprocessing
if __name__ == '__main__':
setup(
name=name,
version=version,
description='Python library for cleaning data in large datasets of Xrays',
long_description=readme,
long_description_content_type='text/markdown',
author='[email protected]',
author_email='[email protected]',
maintainer='[email protected]',
maintainer_email= '[email protected]',
url='https://github.com/drcandacemakedamoore/cleanX',
license='MIT',
packages=[
'cleanX',
'cleanX.dataset_processing',
'cleanX.dicom_processing',
'cleanX.image_work',
'cleanX.cli',
],
cmdclass={
'test': PyTest,
'lint': Pep8,
'apidoc': SphinxApiDoc,
'genconda': GenerateCondaYaml,
'install': Install,
'install_dev': InstallDev,
'find_egg': FindEgg,
'anaconda_upload': AnacondaUpload,
'anaconda_gen_env': GenCondaEnv,
},
tests_require=['pytest', 'pycodestyle'],
command_options={
'build_sphinx': {
'project': ('setup.py', name),
'version': ('setup.py', version),
'source_dir': ('setup.py', './docs'),
'config_dir': ('setup.py', './docs'),
},
},
setup_requires=['sphinx'],
install_requires=install_requires(),
extras_require={
'cli': ['click'],
'pydicom': ['pydicom'],
'simpleitk': ['SimpleITK'],
'dev': dev_deps(),
},
zip_safe=False,
)