-
Notifications
You must be signed in to change notification settings - Fork 0
/
relock.py
218 lines (185 loc) · 7.46 KB
/
relock.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from collections.abc import Mapping
import os
import pprint
import shutil
import subprocess
import sys
import tempfile
import click
from conda.models.match_spec import MatchSpec
from ruamel.yaml import YAML
yaml = YAML(typ="safe", pure=True)
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.default_flow_style = False
def _split_package_list(package_list):
packages = []
for nline in package_list.split("\n"):
for cline in nline.split(","):
for sline in cline.split():
_pkg = sline.strip()
if _pkg:
packages.append(_pkg)
return packages
def _lock_to_ver(lock, platform):
pkg_to_ver = {}
for pkg in lock["package"]:
if pkg["platform"] == platform:
pkg_to_ver[pkg["name"]] = pkg["version"]
return pkg_to_ver
def _reformat_lockfile(lockfile):
# load / dump the lockfile to make sure it is sorted
# so we get nice diffs
with open(lockfile) as f:
new_lock = yaml.load(f)
new_lock["package"] = sorted(
new_lock["package"], key=lambda x: (x["name"], x["platform"])
)
with open(lockfile, "w") as f:
yaml.dump(new_lock, f)
with open(lockfile) as f:
lines = [line.rstrip() for line in f]
with open(lockfile, "w") as f:
f.write("\n".join(lines) + "\n")
@click.command()
@click.option("--environment-file", required=True, type=click.Path(exists=True))
@click.option("--lock-file", required=True, type=click.Path())
@click.option("--ignored-packages", required=True, type=str)
@click.option("--relock-all-packages", required=True, type=str)
@click.option("--include-only-packages", required=True, type=str)
def main(
environment_file,
lock_file,
ignored_packages,
relock_all_packages,
include_only_packages,
):
relocked = False
with tempfile.TemporaryDirectory() as tmpdir:
try:
ignored_packages = _split_package_list(ignored_packages)
relock_all_packages = relock_all_packages.lower() == "true"
have_existing_lock_file = os.path.exists(lock_file)
backup_lock_file = os.path.join(tmpdir, os.path.basename(lock_file))
if have_existing_lock_file:
shutil.move(lock_file, backup_lock_file)
else:
print(
"No existing lock file found. Creating a new one.",
flush=True,
file=sys.stderr,
)
print("Relocking environment.yml...", flush=True, file=sys.stderr)
subprocess.run(
["conda", "lock", "--file", environment_file, "--lockfile", lock_file],
check=True,
capture_output=True,
)
_reformat_lockfile(lock_file)
if not have_existing_lock_file:
print(
"A lock file has been created in this PR since no existing one was found.",
flush=True,
)
relocked = True
else:
with open(environment_file) as f:
envyml = yaml.load(f)
with open(backup_lock_file) as f:
old_lock = yaml.load(f)
with open(lock_file) as f:
new_lock = yaml.load(f)
old_platform_pkg_to_ver = {
platform: _lock_to_ver(old_lock, platform)
for platform in envyml["platforms"]
}
new_platform_pkg_to_ver = {
platform: _lock_to_ver(new_lock, platform)
for platform in envyml["platforms"]
}
if relock_all_packages:
deps_to_relock = set()
for platform in envyml["platforms"]:
for pkg in new_platform_pkg_to_ver[platform]:
deps_to_relock.add(pkg)
elif include_only_packages:
deps_to_relock = set(_split_package_list(include_only_packages))
else:
deps_to_relock = set()
for _spec in envyml["dependencies"]:
if not isinstance(_spec, Mapping):
deps_to_relock.add(MatchSpec(_spec).name)
else:
for _pkg in _spec:
deps_to_relock.add(MatchSpec(_pkg).name)
print(
"relock all packages:",
relock_all_packages,
flush=True,
file=sys.stderr,
)
print(
"initial deps to relock:\n",
pprint.pformat(deps_to_relock),
flush=True,
file=sys.stderr,
)
print(
"ignored packages:\n",
pprint.pformat(ignored_packages),
flush=True,
file=sys.stderr,
)
print(
"include only packages:\n",
pprint.pformat(include_only_packages),
flush=True,
file=sys.stderr,
)
deps_to_relock = deps_to_relock - set(ignored_packages)
print(
"final deps to relock:\n",
pprint.pformat(deps_to_relock),
flush=True,
file=sys.stderr,
)
relock_tuples = {platform: [] for platform in envyml["platforms"]}
for pkg in deps_to_relock:
for platform in envyml["platforms"]:
if old_platform_pkg_to_ver[platform].get(
pkg
) != new_platform_pkg_to_ver[platform].get(pkg):
relock_tuples[platform].append(
(
pkg,
old_platform_pkg_to_ver[platform].get(pkg),
new_platform_pkg_to_ver[platform].get(pkg),
)
)
if any(relock_tuples[platform] for platform in envyml["platforms"]):
msg = "The following packages have been updated:\n\n"
for platform in envyml["platforms"]:
msg += f" platform: {platform}\n"
for pkg, old_ver, new_ver in relock_tuples[platform]:
msg += f" - {pkg}: {old_ver} -> {new_ver}\n"
msg += "\n"
print(msg, flush=True, file=sys.stderr)
print(msg, flush=True)
relocked = True
else:
print("No packages have been updated.", flush=True, file=sys.stderr)
shutil.move(backup_lock_file, lock_file)
relocked = False
except Exception as e:
if os.path.exists(backup_lock_file) and have_existing_lock_file:
shutil.move(backup_lock_file, lock_file)
subprocess.run(
'echo "env_relocked=false" >> "$GITHUB_OUTPUT"',
shell=True,
)
raise e
subprocess.run(
f'echo "env_relocked={"true" if relocked else "false"}" >> "$GITHUB_OUTPUT"',
shell=True,
)
if __name__ == "__main__":
main()