15397: Merge branch 'main'
[arvados.git] / sdk / python / setup.py
1 #!/usr/bin/env python3
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 import os
7 import sys
8 import re
9
10 from pathlib import Path
11 from setuptools import setup, find_packages
12 from setuptools.command import build_py
13
14 import arvados_version
15 version = arvados_version.get_version()
16 short_tests_only = arvados_version.short_tests_only()
17 README = os.path.join(arvados_version.SETUP_DIR, 'README.rst')
18
19 class BuildPython(build_py.build_py):
20     """Extend setuptools `build_py` to generate API documentation
21
22     This class implements a setuptools subcommand, so it follows
23     [the SubCommand protocol][1]. Most of these methods are required by that
24     protocol, except `should_run`, which we register as the subcommand
25     predicate.
26
27     [1]: https://setuptools.pypa.io/en/latest/userguide/extension.html#setuptools.command.build.SubCommand
28     """
29     # This is implemented as functionality on top of `build_py`, rather than a
30     # dedicated subcommand, because that's the only way I can find to run this
31     # code during both `build` and `install`. setuptools' `install` command
32     # normally calls specific `build` subcommands directly, rather than calling
33     # the entire command, so it skips custom subcommands.
34     user_options = build_py.build_py.user_options + [
35         ('discovery-json=', 'J', 'JSON discovery document used to build pydoc'),
36         ('discovery-output=', 'O', 'relative path to write discovery document pydoc'),
37     ]
38
39     def initialize_options(self):
40         super().initialize_options()
41         self.discovery_json = 'arvados-v1-discovery.json'
42         self.discovery_output = str(Path('arvados', 'api_resources.py'))
43
44     def _relative_path(self, src, optname):
45         retval = Path(src)
46         if retval.is_absolute():
47             raise Exception(f"--{optname} should be a relative path")
48         else:
49             return retval
50
51     def finalize_options(self):
52         super().finalize_options()
53         self.json_path = self._relative_path(self.discovery_json, 'discovery-json')
54         self.out_path = Path(
55             self.build_lib,
56             self._relative_path(self.discovery_output, 'discovery-output'),
57         )
58
59     def run(self):
60         super().run()
61         import discovery2pydoc
62         arglist = ['--output-file', str(self.out_path), str(self.json_path)]
63         returncode = discovery2pydoc.main(arglist)
64         if returncode != 0:
65             raise Exception(f"discovery2pydoc exited {returncode}")
66
67     def get_outputs(self):
68         retval = super().get_outputs()
69         retval.append(str(self.out_path))
70         return retval
71
72     def get_source_files(self):
73         retval = super().get_source_files()
74         retval.append(str(self.json_path))
75         return retval
76
77     def get_output_mapping(self):
78         retval = super().get_output_mapping()
79         retval[str(self.json_path)] = str(self.out_path)
80         return retval
81
82
83 setup(name='arvados-python-client',
84       version=version,
85       description='Arvados client library',
86       long_description=open(README).read(),
87       author='Arvados',
88       author_email='info@arvados.org',
89       url="https://arvados.org",
90       download_url="https://github.com/arvados/arvados.git",
91       license='Apache 2.0',
92       cmdclass={
93           'build_py': BuildPython,
94       },
95       packages=find_packages(),
96       scripts=[
97           'bin/arv-copy',
98           'bin/arv-get',
99           'bin/arv-keepdocker',
100           'bin/arv-ls',
101           'bin/arv-migrate-docker19',
102           'bin/arv-federation-migrate',
103           'bin/arv-normalize',
104           'bin/arv-put',
105           'bin/arv-ws'
106       ],
107       data_files=[
108           ('share/doc/arvados-python-client', ['LICENSE-2.0.txt', 'README.rst']),
109       ],
110       install_requires=[
111           *arvados_version.iter_dependencies(version),
112           'ciso8601 >=2.0.0',
113           'google-api-python-client >=2.1.0',
114           'google-auth',
115           'httplib2 >=0.9.2',
116           'pycurl >=7.19.5.1',
117           'setuptools >=40.3.0',
118           'websockets >=11.0',
119       ],
120       python_requires="~=3.8",
121       classifiers=[
122           'Programming Language :: Python :: 3',
123       ],
124       test_suite='tests',
125       tests_require=['PyYAML', 'parameterized'],
126       zip_safe=False
127       )