-
Notifications
You must be signed in to change notification settings - Fork 87
/
requirements.py
60 lines (52 loc) · 2.27 KB
/
requirements.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
import logging
import requirements
logger = logging.getLogger(__name__)
EXCLUDE_REQUIREMENTS = frozenset((
# obviously already satisfied or unwanted
'ansible', 'ansible-base', 'python', 'ansible-core',
# general python test requirements
'tox', 'pycodestyle', 'yamllint', 'pylint',
'flake8', 'pytest', 'pytest-xdist', 'coverage', 'mock', 'testinfra',
# test requirements highly specific to Ansible testing
'ansible-lint', 'molecule', 'galaxy-importer', 'voluptuous',
# already present in image for py3 environments
'yaml', 'pyyaml', 'json',
))
def sanitize_requirements(collection_py_reqs):
# de-duplication
consolidated = []
seen_pkgs = set()
for collection, lines in collection_py_reqs.items():
try:
for req in requirements.parse('\n'.join(lines)):
req.collections = [collection] # add backref for later
if req.name is None:
consolidated.append(req)
continue
if req.name in seen_pkgs:
for prior_req in consolidated:
if req.name == prior_req.name:
prior_req.specs.extend(req.specs)
prior_req.collections.append(collection)
break
continue
consolidated.append(req)
seen_pkgs.add(req.name)
except Exception as e:
logger.warning('Warning: failed to parse requirments from {}, error: {}'.format(collection, e))
# removal of unwanted packages
sanitized = []
for req in consolidated:
if req.name and req.name.lower() in EXCLUDE_REQUIREMENTS:
logger.debug(f'# Excluding requirement {req.name} from {req.collections}')
continue
if req.name is None and req.vcs:
# A source control requirement like git+, return as-is
new_line = req.line
elif req.name:
specs = ['{0}{1}'.format(cmp, ver) for cmp, ver in req.specs]
new_line = req.name + ','.join(specs)
else:
raise RuntimeError('Could not process {0}'.format(req.line))
sanitized.append(new_line + ' # from collection {}'.format(','.join(req.collections)))
return sanitized