Skip to content

Commit b15e15f

Browse files
philscf0rmiga
andauthored
Allow requirements_in to be generated (#829)
* Allow requirements_in to be generated We generate the `requirements_in` file from various input files roughly like so: genrule( name = "generate_3.7_x86_requirements", srcs = [ "requirements_base.in.txt", "requirements_extra_37.in.txt", ], outs = ["requirements_3.7_x86.txt"], cmd = "cat $(SRCS) > $(OUTS)", ) compile_pip_requirements( name = "compile_requirements_3.7_x86", requirements_in = ":requirements_3.7_x86.txt", requirements_txt = "requirements_3.7_x86.lock.txt", ) The current code errors out with a message like this: Updating common/python/requirements_3.7_x86.lock.txt Usage: pip_compile.py [OPTIONS] [SRC_FILES]... Try 'pip_compile.py -h' for help. Error: Invalid value for '[SRC_FILES]...': Path 'common/python/requirements_3.7_x86.txt' does not exist. This patch here fixes the issue by resolving the `requirements_in` path before the tool `cd`s into the workspace directory. * Make tests pass * Run black * Fix some runtime problems Co-authored-by: Thulio Ferraz Assis <[email protected]>
1 parent 44d41ee commit b15e15f

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

examples/pip_parse/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ alias(
5555
actual = entry_point("yamllint"),
5656
)
5757

58+
# The requirements.in file can be checked in to the source tree or it can be
59+
# generated. Pretend that we do some generating of the file. For this example,
60+
# the "template" is already the file we want.
61+
genrule(
62+
name = "generate_requirements_in",
63+
srcs = ["requirements.in.template"],
64+
outs = ["requirements.in"],
65+
cmd = "cp $(SRCS) $(OUTS)",
66+
)
67+
5868
# This rule adds a convenient way to update the requirements file.
5969
compile_pip_requirements(
6070
name = "requirements",

python/pip_install/pip_compile.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sys
5+
from pathlib import Path
56
from shutil import copyfile
67

78
from piptools.scripts.compile import cli
@@ -25,6 +26,21 @@ def _select_golden_requirements_file(
2526
return requirements_txt
2627

2728

29+
def _fix_up_requirements_in_path(
30+
resolved_requirements_in, requirements_in, output_file
31+
):
32+
"""Fix up references to the input file inside of the generated requirements file.
33+
34+
We don't want fully resolved, absolute paths in the generated requirements file.
35+
The paths could differ for every invocation. Replace them with a predictable path.
36+
"""
37+
output_file = Path(output_file)
38+
fixed_requirements_text = output_file.read_text().replace(
39+
resolved_requirements_in, requirements_in
40+
)
41+
output_file.write_text(fixed_requirements_text)
42+
43+
2844
if __name__ == "__main__":
2945
if len(sys.argv) < 4:
3046
print(
@@ -42,6 +58,10 @@ def _select_golden_requirements_file(
4258
requirements_windows = parse_str_none(sys.argv.pop(1))
4359
update_target_label = sys.argv.pop(1)
4460

61+
# The requirements_in file could be generated. We need to get the path to it before we change
62+
# directory into the workspace directory.
63+
resolved_requirements_in = str(Path(requirements_in).resolve())
64+
4565
# Before loading click, set the locale for its parser.
4666
# If it leaks through to the system setting, it may fail:
4767
# RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII
@@ -96,11 +116,18 @@ def _select_golden_requirements_file(
96116
sys.argv.append("--generate-hashes")
97117
sys.argv.append("--output-file")
98118
sys.argv.append(requirements_txt if UPDATE else requirements_out)
99-
sys.argv.append(requirements_in)
119+
sys.argv.append(resolved_requirements_in)
100120

101121
if UPDATE:
102122
print("Updating " + requirements_txt)
103-
cli()
123+
try:
124+
cli()
125+
except SystemExit as e:
126+
if e.code == 0:
127+
_fix_up_requirements_in_path(
128+
resolved_requirements_in, requirements_in, requirements_txt
129+
)
130+
raise
104131
else:
105132
# cli will exit(0) on success
106133
try:
@@ -118,6 +145,9 @@ def _select_golden_requirements_file(
118145
)
119146
sys.exit(1)
120147
elif e.code == 0:
148+
_fix_up_requirements_in_path(
149+
resolved_requirements_in, requirements_in, requirements_out
150+
)
121151
golden_filename = _select_golden_requirements_file(
122152
requirements_txt,
123153
requirements_linux,

0 commit comments

Comments
 (0)