1616
1717import atexit
1818import os
19+ import re
1920import shutil
2021import sys
2122from pathlib import Path
@@ -168,6 +169,17 @@ def main(
168169 )
169170 argv .extend (extra_args )
170171
172+ # Replace in the output lock file
173+ # the lines like: # via -r /absolute/path/to/requirements_extra.in
174+ # with: # via -r requirements_extra.in
175+ pattern = re .compile (r"(# via -r )(.+requirements.*\.in)" )
176+
177+ def replace_abspath_with_name (match ):
178+ assert match is not None
179+ # match[1] is "# via -r "
180+ # match[2] is "/absolute/path/to/requirements*.in"
181+ return match [1 ] + Path (match [2 ]).name
182+
171183 if UPDATE :
172184 print ("Updating " + requirements_file_relative )
173185
@@ -185,14 +197,13 @@ def main(
185197 # and we should copy the updated requirements back to the source tree.
186198 if not absolute_output_file .samefile (requirements_file_tree ):
187199 atexit .register (
188- lambda : shutil .copy (
189- absolute_output_file , requirements_file_tree
190- )
200+ lambda : shutil .copy (absolute_output_file , requirements_file_tree )
191201 )
192- cli (argv , standalone_mode = False )
202+ cli (argv , standalone_mode = False )
193203 requirements_file_relative_path = Path (requirements_file_relative )
194204 content = requirements_file_relative_path .read_text ()
195205 content = content .replace (absolute_path_prefix , "" )
206+ content = pattern .sub (replace_abspath_with_name , content )
196207 requirements_file_relative_path .write_text (content )
197208 else :
198209 # cli will exit(0) on success
@@ -214,6 +225,18 @@ def main(
214225 golden = open (_locate (bazel_runfiles , requirements_file )).readlines ()
215226 out = open (requirements_out ).readlines ()
216227 out = [line .replace (absolute_path_prefix , "" ) for line in out ]
228+
229+ def replace_via_minus_r (line ):
230+ if "# via -r " in line :
231+ return pattern .sub (replace_abspath_with_name , line )
232+ return line
233+
234+ out = [replace_via_minus_r (line ) for line in out ]
235+
236+
237+ print ("OUT:" , out )
238+ print ("GOLDEN:" , golden )
239+ assert False
217240 if golden != out :
218241 import difflib
219242
0 commit comments