forked from rucio/rucio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setuputil.py
173 lines (152 loc) · 5.22 KB
/
setuputil.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
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess
import sys
from typing import TYPE_CHECKING, Union
from pkg_resources import parse_requirements, safe_name
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping
clients_requirements_table = {
'install_requires': [
'requests',
'urllib3',
'dogpile-cache',
'tabulate',
'jsonschema',
'dataclasses',
],
'ssh': ['paramiko'],
'kerberos': [
'kerberos',
'pykerberos',
'requests-kerberos',
],
'swift': ['python-swiftclient'],
'argcomplete': ['argcomplete'],
'sftp': ['paramiko'],
'dumper': [
'python-magic',
],
}
dev_requirements = [
'pytest',
'pytest-xdist',
'pyflakes',
'flake8',
'pylint',
'isort',
'virtualenv',
'xmltodict',
'pytz',
'pycodestyle',
'pydoc-markdown',
'docspec_python',
'sh',
'PyYAML',
]
server_requirements_table = {
'install_requires': clients_requirements_table['install_requires'] + [
'argcomplete',
'boto',
'python-magic',
'paramiko',
'boto3',
'sqlalchemy',
'alembic',
'pymemcache',
'python-dateutil',
'stomp-py',
'statsd',
'geoip2',
'google-auth',
'redis',
'flask',
'oic',
'prometheus_client',
],
'oracle': ['cx_oracle'],
'mongo': ['pymongo'],
'postgresql': ['psycopg2-binary'],
'mysql': ['PyMySQL'],
'kerberos': [
'kerberos',
'pykerberos',
'requests-kerberos',
],
'globus': [
'PyYAML',
'globus-sdk',
],
'saml': ['python3-saml'],
'dev': dev_requirements
}
def run_shell_command(cmd: str) -> Union[str, bytearray, memoryview]:
"""
Run a shell command in path and return output"
:param cmd: the shell command.
:return: Output of the shell command.
"""
output = subprocess.Popen(["/bin/sh", "-c", cmd], stdout=subprocess.PIPE)
stdout = output.communicate()[0].strip()
if isinstance(stdout, bytes):
stdout = stdout.decode(errors='replace')
return stdout
def get_rucio_version() -> str:
python_executable = "'" + sys.executable + "'"
ver = run_shell_command(
"PYTHONPATH=lib " + python_executable + " -c "
'"from rucio import version; print(version.version_string())"'
)
if not ver:
raise RuntimeError("Could not fetch Rucio version")
return str(ver)
def _build_requirements_table_by_key(requirements_table: "Mapping[str, Iterable[str]]") -> tuple[dict[str, list[str]], dict[str, list[str]]]:
extras_require: dict[str, list[str]] = {}
req_table_by_key: dict[str, list[str]] = {}
for group in requirements_table.keys():
if group != 'install_requires' and group not in extras_require:
extras_require[group] = []
for key in map(str.lower, map(safe_name, requirements_table[group])):
if key in req_table_by_key:
req_table_by_key[key].append(group)
else:
req_table_by_key[key] = [group]
return req_table_by_key, extras_require
def match_define_requirements(app_type: str, requirements_table: "Mapping[str, Iterable[str]]") -> tuple[list[str], dict[str, list[str]]]:
install_requires = []
req_table_by_key, extras_require = _build_requirements_table_by_key(requirements_table)
req_file_name = "requirements/requirements.{}.txt".format(app_type)
with open(req_file_name, 'r') as fhandle:
for req in parse_requirements(fhandle.readlines()):
if req.key in req_table_by_key:
for group in req_table_by_key[req.key]:
print("requirement found", group, req, file=sys.stderr)
if group == 'install_requires':
install_requires.append(str(req))
else:
extras_require[group].append(str(req))
else:
print("requirement unused", req, "(from " + req.key + ")", file=sys.stderr)
sys.stderr.flush()
for extra, deps in extras_require.items():
if not deps:
raise RuntimeError('Empty extra: {}'.format(extra))
return install_requires, extras_require
def list_all_requirements(app_type: str, requirements_table: "Mapping[str, Iterable[str]]") -> None:
req_table_by_key, _ = _build_requirements_table_by_key(requirements_table)
req_file_name = "requirements/requirements.{}.txt".format(app_type)
with open(req_file_name, 'r') as fhandle:
for req in parse_requirements(fhandle.readlines()):
if req.key in req_table_by_key:
print(str(req))