1212from subprocess import PIPE , CalledProcessError
1313from traceback import format_exception_only
1414
15+ from python .runfiles import runfiles
1516
16- async def _exec (* args , ** kwargs ):
17- proc = await asyncio .create_subprocess_exec (* args , ** kwargs )
17+
18+ async def _exec (go , * args , ** kwargs ):
19+ proc = await asyncio .create_subprocess_exec (go , * args , ** kwargs )
1820 try :
1921 stdout , _ = await proc .communicate ()
2022 except BaseException : # reap the process on any CancelledError, KeyboardInterrupt, SystemExit, TimeoutError, etc.
@@ -28,26 +30,26 @@ async def _exec(*args, **kwargs):
2830 raise
2931 if proc .returncode == 0 :
3032 return stdout
31- raise CalledProcessError (proc .returncode , " " .join (args ), output = stdout )
33+ raise CalledProcessError (proc .returncode , " " .join (( os . path . basename ( go ), * args ) ), output = stdout )
3234
3335
34- async def _tidy (max_workers , mod_path , args ):
36+ async def _tidy (max_workers , go , mod_path , args ):
3537 async with max_workers :
36- await _exec ("go" , "mod" , "tidy" , "-C" , mod_path , * args )
38+ await _exec (go , "mod" , "tidy" , "-C" , mod_path , * args )
3739
3840
39- async def main (args ):
40- mod_paths = await _exec ("go" , "list" , "-f" , "{{.Dir}}" , "-m" , stdout = PIPE )
41+ async def main (go , args ):
42+ mod_paths = await _exec (go , "list" , "-f" , "{{.Dir}}" , "-m" , stdout = PIPE )
4143 max_workers = asyncio .Semaphore ((os .cpu_count () or 1 ) + 4 ) # TODO(regis): cpu_count -> Py 3.13's process_cpu_count
4244 # global timeout: on cold cache, per-task timeouts were unfairly hit because early tasks download most modules
4345 async with asyncio .timeout (timedelta (minutes = 15 ).total_seconds ()), asyncio .TaskGroup () as tg :
4446 for mod_path in mod_paths .decode ().splitlines ():
45- tg .create_task (_tidy (max_workers , mod_path , args ))
47+ tg .create_task (_tidy (max_workers , go , mod_path , args ))
4648
4749
4850if __name__ == "__main__" :
4951 logging .getLogger ("asyncio" ).setLevel (logging .ERROR ) # no `Unknown child process pid N, will report returncode 255`
5052 try :
51- asyncio .run (main (sys .argv [1 :]))
53+ asyncio .run (main (runfiles . Create (). Rlocation ( sys .argv [1 ]), sys . argv [ 2 :]))
5254 except* BaseException as eg :
5355 sys .exit ("\n " .join (line .rstrip () for e in eg .exceptions for line in format_exception_only (e )))
0 commit comments