forked from zopefoundation/Zope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
90 lines (69 loc) · 2.63 KB
/
util.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
import os
from typing import Optional, Tuple, List
from configparser import RawConfigParser
HERE = os.path.abspath(os.path.dirname(__file__))
PYTHON_VERSIONED = {}
class CaseSensitiveParser(RawConfigParser):
def optionxform(self, value):
return value
def generate(in_, requirements_file, constraints_file):
in_file = os.path.join(HERE, in_)
out_file_requirements = os.path.join(HERE, requirements_file)
out_file_constraints = os.path.join(HERE, constraints_file)
parser = CaseSensitiveParser()
parser.read(in_file)
requirements = []
constraints = []
zope_requirement = (
'-e git+https://github.com/zopefoundation/Zope.git@master#egg=Zope\n')
zope_requirement = _generate(
parser.items('versions:python36'), '3.6', requirements, constraints,
zope_requirement)
# "Unversioned" pins must come last, how they are handled depends on
# Python version qualifiers for dependencies of the same name.
zope_requirement = _generate(
parser.items('versions'), None, requirements, constraints,
zope_requirement)
with open(out_file_requirements, 'w') as fd:
fd.write(zope_requirement)
for req in sorted(requirements):
fd.write(req)
with open(out_file_constraints, 'w') as fcon:
for con in sorted(constraints):
fcon.write(con)
def _generate(
versions: List[Tuple[str, str]],
python_version: Optional[str],
requirements: List[str],
constraints: List[str],
zope_requirement: str
) -> str:
"""Generate requirements and constraints for a specific Python version.
If ``python_version`` is falsy, generate for all python versions.
Returns a probably changed ``zope_requirement``.
"""
global PYTHON_VERSIONED
for name, pin in versions:
if name == 'Zope':
if pin:
zope_requirement = 'Zope==%s\n' % pin
continue
if not pin:
continue
spec = f'{name}=={pin}'
if python_version:
spec = f"{spec}; python_version == '{python_version}'"
versions = PYTHON_VERSIONED.get(name, set())
versions.add(python_version)
PYTHON_VERSIONED[name] = versions
else:
if name in PYTHON_VERSIONED:
versions = sorted(PYTHON_VERSIONED.get(name))
spec = f"{spec}; python_version > '{versions[-1]}'"
requirements.append(spec + '\n')
constraints.append(spec + '\n')
return zope_requirement
def main():
generate('versions-prod.cfg', 'requirements-full.txt', 'constraints.txt')
if __name__ == '__main__':
main()