1919import shutil
2020import sys
2121from pathlib import Path
22+ from typing import List , Optional , Tuple
2223
24+ import click
2325import piptools .writer as piptools_writer
2426from piptools .scripts .compile import cli
2527
@@ -66,27 +68,27 @@ def _select_golden_requirements_file(
6668 return requirements_txt
6769
6870
69- if __name__ == "__main__" :
70- if len ( sys . argv ) < 4 :
71- print (
72- "Expected at least two arguments: requirements_in requirements_out" ,
73- file = sys . stderr ,
74- )
75- sys . exit ( 1 )
76-
77- parse_str_none = lambda s : None if s == "None" else s
78-
79- requirements_in = sys . argv . pop ( 1 )
80- requirements_txt = sys . argv . pop ( 1 )
81- requirements_linux = parse_str_none ( sys . argv . pop ( 1 ))
82- requirements_darwin = parse_str_none ( sys . argv . pop ( 1 ))
83- requirements_windows = parse_str_none ( sys . argv . pop ( 1 ))
84- update_target_label = sys . argv . pop ( 1 )
85-
71+ @ click . command ( context_settings = { "ignore_unknown_options" : True })
72+ @ click . option ( "--in" , "requirements_ins" , multiple = True , required = True )
73+ @ click . argument ( "requirements_txt" )
74+ @ click . argument ( "update_target_label" )
75+ @ click . option ( "--requirements-linux" )
76+ @ click . option ( "--requirements-darwin" )
77+ @ click . option ( "--requirements-windows" )
78+ @ click . argument ( "extra_args" , nargs = - 1 , type = click . UNPROCESSED )
79+ def main (
80+ requirements_ins : Tuple [ str , ...],
81+ requirements_txt : str ,
82+ update_target_label : str ,
83+ requirements_linux : Optional [ str ],
84+ requirements_darwin : Optional [ str ],
85+ requirements_windows : Optional [ str ],
86+ extra_args : Tuple [ str , ...],
87+ ) -> None :
8688 # The requirements_in file could be generated, so we will need to remove the
8789 # absolute prefixes in the locked requirements output file.
88- requirements_in_path = Path (requirements_in )
89- resolved_requirements_in = str (requirements_in_path .resolve ())
90+ requirements_in_paths = [ Path (f ) for f in requirements_ins ]
91+ resolved_requirements_ins = [ str (p .resolve ()) for p in requirements_in_paths ]
9092
9193 # Before loading click, set the locale for its parser.
9294 # If it leaks through to the system setting, it may fail:
@@ -96,6 +98,8 @@ def _select_golden_requirements_file(
9698 os .environ ["LC_ALL" ] = "C.UTF-8"
9799 os .environ ["LANG" ] = "C.UTF-8"
98100
101+ argv = []
102+
99103 UPDATE = True
100104 # Detect if we are running under `bazel test`.
101105 if "TEST_TMPDIR" in os .environ :
@@ -104,8 +108,7 @@ def _select_golden_requirements_file(
104108 # to the real user cache, Bazel sandboxing makes the file read-only
105109 # and we fail.
106110 # In theory this makes the test more hermetic as well.
107- sys .argv .append ("--cache-dir" )
108- sys .argv .append (os .environ ["TEST_TMPDIR" ])
111+ argv .append (f"--cache-dir={ os .environ ['TEST_TMPDIR' ]} " )
109112 # Make a copy for pip-compile to read and mutate.
110113 requirements_out = os .path .join (
111114 os .environ ["TEST_TMPDIR" ], os .path .basename (requirements_txt ) + ".out"
@@ -121,12 +124,17 @@ def _select_golden_requirements_file(
121124 os .environ ["CUSTOM_COMPILE_COMMAND" ] = update_command
122125 os .environ ["PIP_CONFIG_FILE" ] = os .getenv ("PIP_CONFIG_FILE" ) or os .devnull
123126
124- sys .argv .append ("--generate-hashes" )
125- sys .argv .append ("--output-file" )
126- sys .argv .append (requirements_txt if UPDATE else requirements_out )
127- sys .argv .append (
128- requirements_in if requirements_in_path .exists () else resolved_requirements_in
127+ argv .append ("--generate-hashes" )
128+ argv .append (f"--output-file={ requirements_txt if UPDATE else requirements_out } " )
129+ argv .extend (
130+ (req_in if req_in_path .exists () else resolved_req_in )
131+ for req_in , req_in_path , resolved_req_in in zip (
132+ requirements_ins , requirements_in_paths , resolved_requirements_ins
133+ )
129134 )
135+ argv .extend (extra_args )
136+
137+ argv .extend (extra_args )
130138
131139 if UPDATE :
132140 print ("Updating " + requirements_txt )
@@ -140,20 +148,20 @@ def _select_golden_requirements_file(
140148 atexit .register (
141149 lambda : shutil .copy (requirements_txt , requirements_txt_tree )
142150 )
143- cli ()
151+ cli (argv )
144152 else :
145153 # cli will exit(0) on success
146154 try :
147155 print ("Checking " + requirements_txt )
148- cli ()
156+ cli (argv )
149157 print ("cli() should exit" , file = sys .stderr )
150158 sys .exit (1 )
151159 except SystemExit as e :
152160 if e .code == 2 :
153161 print (
154162 "pip-compile exited with code 2. This means that pip-compile found "
155163 "incompatible requirements or could not find a version that matches "
156- f"the install requirement in { requirements_in } ." ,
164+ f"the install requirement in one of { requirements_ins } ." ,
157165 file = sys .stderr ,
158166 )
159167 sys .exit (1 )
@@ -184,3 +192,7 @@ def _select_golden_requirements_file(
184192 file = sys .stderr ,
185193 )
186194 sys .exit (1 )
195+
196+
197+ if __name__ == "__main__" :
198+ main ()
0 commit comments