19
19
import shutil
20
20
import sys
21
21
from pathlib import Path
22
+ from typing import Optional , Tuple
22
23
24
+ import click
23
25
import piptools .writer as piptools_writer
24
26
from piptools .scripts .compile import cli
25
27
@@ -77,24 +79,25 @@ def _locate(bazel_runfiles, file):
77
79
return bazel_runfiles .Rlocation (file )
78
80
79
81
80
- if __name__ == "__main__" :
81
- if len (sys .argv ) < 4 :
82
- print (
83
- "Expected at least two arguments: requirements_in requirements_out" ,
84
- file = sys .stderr ,
85
- )
86
- sys .exit (1 )
87
-
88
- parse_str_none = lambda s : None if s == "None" else s
82
+ @click .command (context_settings = {"ignore_unknown_options" : True })
83
+ @click .argument ("requirements_in" )
84
+ @click .argument ("requirements_txt" )
85
+ @click .argument ("update_target_label" )
86
+ @click .option ("--requirements-linux" )
87
+ @click .option ("--requirements-darwin" )
88
+ @click .option ("--requirements-windows" )
89
+ @click .argument ("extra_args" , nargs = - 1 , type = click .UNPROCESSED )
90
+ def main (
91
+ requirements_in : str ,
92
+ requirements_txt : str ,
93
+ update_target_label : str ,
94
+ requirements_linux : Optional [str ],
95
+ requirements_darwin : Optional [str ],
96
+ requirements_windows : Optional [str ],
97
+ extra_args : Tuple [str , ...],
98
+ ) -> None :
89
99
bazel_runfiles = runfiles .Create ()
90
100
91
- requirements_in = sys .argv .pop (1 )
92
- requirements_txt = sys .argv .pop (1 )
93
- requirements_linux = parse_str_none (sys .argv .pop (1 ))
94
- requirements_darwin = parse_str_none (sys .argv .pop (1 ))
95
- requirements_windows = parse_str_none (sys .argv .pop (1 ))
96
- update_target_label = sys .argv .pop (1 )
97
-
98
101
resolved_requirements_in = _locate (bazel_runfiles , requirements_in )
99
102
resolved_requirements_txt = _locate (bazel_runfiles , requirements_txt )
100
103
@@ -123,6 +126,8 @@ def _locate(bazel_runfiles, file):
123
126
os .environ ["LC_ALL" ] = "C.UTF-8"
124
127
os .environ ["LANG" ] = "C.UTF-8"
125
128
129
+ argv = []
130
+
126
131
UPDATE = True
127
132
# Detect if we are running under `bazel test`.
128
133
if "TEST_TMPDIR" in os .environ :
@@ -131,8 +136,7 @@ def _locate(bazel_runfiles, file):
131
136
# to the real user cache, Bazel sandboxing makes the file read-only
132
137
# and we fail.
133
138
# In theory this makes the test more hermetic as well.
134
- sys .argv .append ("--cache-dir" )
135
- sys .argv .append (os .environ ["TEST_TMPDIR" ])
139
+ argv .append (f"--cache-dir={ os .environ ['TEST_TMPDIR' ]} " )
136
140
# Make a copy for pip-compile to read and mutate.
137
141
requirements_out = os .path .join (
138
142
os .environ ["TEST_TMPDIR" ], os .path .basename (requirements_txt ) + ".out"
@@ -148,15 +152,14 @@ def _locate(bazel_runfiles, file):
148
152
os .environ ["CUSTOM_COMPILE_COMMAND" ] = update_command
149
153
os .environ ["PIP_CONFIG_FILE" ] = os .getenv ("PIP_CONFIG_FILE" ) or os .devnull
150
154
151
- sys .argv .append ("--generate-hashes" )
152
- sys .argv .append ("--output-file" )
153
- sys .argv .append (requirements_txt_relative if UPDATE else requirements_out )
154
- sys .argv .append (
155
+ argv .append ("--generate-hashes" )
156
+ argv .append (f"--output-file={ requirements_txt_relative if UPDATE else requirements_out } " )
157
+ argv .append (
155
158
requirements_in_relative
156
159
if Path (requirements_in_relative ).exists ()
157
160
else resolved_requirements_in
158
161
)
159
- print ( sys . argv )
162
+ argv . extend ( extra_args )
160
163
161
164
if UPDATE :
162
165
print ("Updating " + requirements_txt_relative )
@@ -172,7 +175,7 @@ def _locate(bazel_runfiles, file):
172
175
resolved_requirements_txt , requirements_txt_tree
173
176
)
174
177
)
175
- cli ()
178
+ cli (argv )
176
179
requirements_txt_relative_path = Path (requirements_txt_relative )
177
180
content = requirements_txt_relative_path .read_text ()
178
181
content = content .replace (absolute_path_prefix , "" )
@@ -181,7 +184,7 @@ def _locate(bazel_runfiles, file):
181
184
# cli will exit(0) on success
182
185
try :
183
186
print ("Checking " + requirements_txt )
184
- cli ()
187
+ cli (argv )
185
188
print ("cli() should exit" , file = sys .stderr )
186
189
sys .exit (1 )
187
190
except SystemExit as e :
@@ -221,3 +224,7 @@ def _locate(bazel_runfiles, file):
221
224
file = sys .stderr ,
222
225
)
223
226
sys .exit (1 )
227
+
228
+
229
+ if __name__ == "__main__" :
230
+ main ()
0 commit comments