Skip to content

Commit e0c2715

Browse files
refactor: Unify invoking pip-compile
1 parent 938f015 commit e0c2715

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

python/private/pypi/dependency_resolver/dependency_resolver.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"Set defaults for the pip-compile command to run it under Bazel"
1616

1717
import atexit
18+
import functools
1819
import os
1920
import shutil
2021
import sys
2122
from pathlib import Path
22-
from typing import Optional, Tuple
23+
from typing import List, Optional, Tuple
2324

2425
import click
2526
import piptools.writer as piptools_writer
@@ -168,6 +169,12 @@ def main(
168169
)
169170
argv.extend(extra_args)
170171

172+
_run_pip_compile = functools.partial(
173+
run_pip_compile,
174+
argv,
175+
srcs_relative=srcs_relative,
176+
)
177+
171178
if UPDATE:
172179
print("Updating " + requirements_file_relative)
173180

@@ -187,49 +194,52 @@ def main(
187194
atexit.register(
188195
lambda: shutil.copy(absolute_output_file, requirements_file_tree)
189196
)
190-
cli(argv, standalone_mode=False)
197+
_run_pip_compile()
191198
requirements_file_relative_path = Path(requirements_file_relative)
192199
content = requirements_file_relative_path.read_text()
193200
content = content.replace(absolute_path_prefix, "")
194201
requirements_file_relative_path.write_text(content)
195202
else:
196-
# cli will exit(0) on success
197-
try:
198-
print("Checking " + requirements_file)
199-
cli(argv)
200-
print("cli() should exit", file=sys.stderr)
203+
print("Checking " + requirements_file)
204+
_run_pip_compile()
205+
golden = open(_locate(bazel_runfiles, requirements_file)).readlines()
206+
out = open(requirements_out).readlines()
207+
out = [line.replace(absolute_path_prefix, "") for line in out]
208+
if golden != out:
209+
import difflib
210+
211+
print("".join(difflib.unified_diff(golden, out)), file=sys.stderr)
212+
print(
213+
"Lock file out of date. Run '" + update_command + "' to update.",
214+
file=sys.stderr,
215+
)
216+
sys.exit(1)
217+
218+
219+
def run_pip_compile(
220+
args: List[str],
221+
*,
222+
srcs_relative: List[str],
223+
) -> None:
224+
try:
225+
cli(args, standalone_mode=False)
226+
except SystemExit as e:
227+
if e.code == 0:
228+
return # shouldn't happen, but just in case
229+
elif e.code == 2:
230+
print(
231+
"pip-compile exited with code 2. This means that pip-compile found "
232+
"incompatible requirements or could not find a version that matches "
233+
f"the install requirement in one of {srcs_relative}.",
234+
file=sys.stderr,
235+
)
236+
sys.exit(1)
237+
else:
238+
print(
239+
f"pip-compile unexpectedly exited with code {e.code}.",
240+
file=sys.stderr,
241+
)
201242
sys.exit(1)
202-
except SystemExit as e:
203-
if e.code == 2:
204-
print(
205-
"pip-compile exited with code 2. This means that pip-compile found "
206-
"incompatible requirements or could not find a version that matches "
207-
f"the install requirement in one of {srcs_relative}.",
208-
file=sys.stderr,
209-
)
210-
sys.exit(1)
211-
elif e.code == 0:
212-
golden = open(_locate(bazel_runfiles, requirements_file)).readlines()
213-
out = open(requirements_out).readlines()
214-
out = [line.replace(absolute_path_prefix, "") for line in out]
215-
if golden != out:
216-
import difflib
217-
218-
print("".join(difflib.unified_diff(golden, out)), file=sys.stderr)
219-
print(
220-
"Lock file out of date. Run '"
221-
+ update_command
222-
+ "' to update.",
223-
file=sys.stderr,
224-
)
225-
sys.exit(1)
226-
sys.exit(0)
227-
else:
228-
print(
229-
f"pip-compile unexpectedly exited with code {e.code}.",
230-
file=sys.stderr,
231-
)
232-
sys.exit(1)
233243

234244

235245
if __name__ == "__main__":

0 commit comments

Comments
 (0)