-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathupgrade-python-packages
More file actions
executable file
·156 lines (139 loc) · 4.64 KB
/
upgrade-python-packages
File metadata and controls
executable file
·156 lines (139 loc) · 4.64 KB
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
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# This script compiles dependency groups from `pyproject.toml` into
# hash-locked `requirements.txt` files using `uv pip compile`. The
# generated files are consumed by ansible tasks and Dockerfiles via
# `pip install --require-hashes`.
import argparse
import subprocess
import sys
from pathlib import Path
S3_FIND_LINKS = (
"--find-links https://s3-us-west-2.amazonaws.com/moz-packages/pypi/index.html"
)
LOCAL_FIND_LINKS = "--find-links /var/tmp/pip"
# Each entry maps a dependency group to its output path, target Python
# version, and `--find-links` directives to prepend to the output.
GROUPS = {
"ansible": {
"output": "ansible/files/requirements-ansible.txt",
"python": "3.9",
"find_links": [],
},
"docs": {
"output": "docs-requirements.txt",
"python": "3.7",
"find_links": [],
},
"test": {
"output": "test-requirements-3.txt",
"python": "3.9",
"find_links": [S3_FIND_LINKS],
},
"hgweb": {
"output": "ansible/roles/hg-web/files/requirements-hgweb.txt",
"python": "3.6",
"find_links": [S3_FIND_LINKS, LOCAL_FIND_LINKS],
},
"hg-web-replication": {
"output": "ansible/roles/hg-web/files/requirements-replication.txt",
"python": "3.6",
"find_links": [S3_FIND_LINKS, LOCAL_FIND_LINKS],
},
"hg-web-tools": {
"output": "ansible/roles/hg-web/files/requirements-tools.txt",
"python": "3.6",
"find_links": [S3_FIND_LINKS, LOCAL_FIND_LINKS],
},
"hg-web-tools-py3": {
"output": "ansible/roles/hg-web/files/requirements-tools-py3.txt",
"python": "3.6",
"find_links": [],
},
"hg-ssh-tools": {
"output": "ansible/roles/hg-ssh/files/requirements-tools.txt",
"python": "3.9",
"find_links": [S3_FIND_LINKS, LOCAL_FIND_LINKS],
},
"hg-ssh-bundles": {
"output": "ansible/roles/hg-ssh/files/requirements-bundles.txt",
"python": "3.9",
"find_links": [],
},
"hg-ssh-pash": {
"output": "ansible/roles/hg-ssh-server/files/requirements-pash.txt",
"python": "3.9",
"find_links": [S3_FIND_LINKS, LOCAL_FIND_LINKS],
},
"hg-ssh-testing": {
"output": "ansible/roles/test-hg-ssh/files/requirements-testing.txt",
"python": "3.9",
"find_links": [],
},
}
def compile_group(group: str, output_path: Path, python: str, find_links: list[str]):
"""Compile a dependency group into a hash-locked requirements.txt file."""
print(f"Compiling {group} (python {python}) -> {output_path}")
try:
result = subprocess.run(
[
"uv",
"pip",
"compile",
"pyproject.toml",
"--group",
group,
"--generate-hashes",
"--annotation-style",
"line",
"--python-version",
python,
],
check=True,
capture_output=True,
)
except subprocess.CalledProcessError as error:
print(f"`uv pip compile` failed for group `{group}`:", file=sys.stderr)
print(error.stderr.decode("utf-8"), file=sys.stderr)
sys.exit(error.returncode)
compiled = result.stdout.decode()
if find_links:
header = "\n".join(find_links) + "\n\n"
compiled = header + compiled
output_path.write_text(compiled)
def main():
parser = argparse.ArgumentParser(
description="Compile dependency groups into hash-locked requirements.txt files."
)
parser.add_argument(
"group",
nargs="*",
default=[],
help="Specific group(s) to compile. Compiles all if omitted.",
)
args = parser.parse_args()
if args.group:
targets = {}
for group in args.group:
if group not in GROUPS:
print(
f"`{group}` is not a known dependency group.", file=sys.stderr
)
print(
f"Known groups: {', '.join(sorted(GROUPS))}", file=sys.stderr
)
sys.exit(1)
targets[group] = GROUPS[group]
else:
targets = GROUPS
for group, config in sorted(targets.items()):
compile_group(
group,
Path(config["output"]),
config["python"],
config["find_links"],
)
if __name__ == "__main__":
main()