Skip to content

Commit 538e4fe

Browse files
refactor: Unify invoking pip-compile
1 parent e14cd37 commit 538e4fe

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

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

236246

237247
if __name__ == "__main__":

0 commit comments

Comments
 (0)