1+ import os
2+ import shutil
13import sys
24import subprocess
35from typing import List
46
57import aikido_zen
68
79
8- def run_command ( args : List [ str ]) -> int :
10+ def add_preload_directory ( preload_directory : str ) :
911 """
10- Run the provided command as a subprocess.
12+ Idea here is to load in the code stored in the preload_directory first, Inspired by ddtrace-run:
13+ https://github.com/DataDog/dd-trace-py/blob/3ad335edd4a032ea53680fefbcc10f8fca0690a0/ddtrace/commands/ddtrace_run.py#L41
1114 """
12- if not args :
13- print ("Error: No command provided." , file = sys .stderr )
14- return 1
15+ python_path = os .environ .get ("PYTHONPATH" , "" )
1516
16- try :
17- # Run the command as a subprocess
18- result = subprocess .run (args , check = False )
19- return result .returncode
20- except KeyboardInterrupt :
21- print ("\n Command interrupted by user." , file = sys .stderr )
22- return 130 # SIGINT exit code
23- except Exception as e :
24- print (f"Error: { e } " , file = sys .stderr )
25- return 1
17+ if python_path :
18+ new_path = "%s%s%s" % (preload_directory , os .path .pathsep , os .environ ["PYTHONPATH" ])
19+ os .environ ["PYTHONPATH" ] = new_path
20+ else :
21+ os .environ ["PYTHONPATH" ] = preload_directory
22+
23+ print (os .environ ["PYTHONPATH" ])
24+
25+
26+ def get_executable (command ):
27+ if os .path .isfile (command ):
28+ return command
29+ return shutil .which (command )
2630
2731def main () -> int :
2832 """
@@ -32,14 +36,21 @@ def main() -> int:
3236 print ("Usage: aikido_zen <command> [args...]" , file = sys .stderr )
3337 return 1
3438
35- # Start background process
36- aikido_zen .protect (mode = "daemon_only" )
39+ root_dir = os .path .dirname (aikido_zen .__file__ )
40+ preload_directory = os .path .join (root_dir , "cli/preload" )
41+ add_preload_directory (preload_directory )
3742
3843 # The command to run is everything after `aikido_zen`
39- command = sys .argv [1 :]
40- status = run_command (command )
44+ executable = get_executable (sys .argv [1 ])
45+ try :
46+ os .execl (executable , executable , * sys .argv [2 :])
47+ except PermissionError :
48+ print ("aikido_zen: permission error while launching '%s'" % executable )
49+ return 1
50+ except Exception :
51+ print ("ddtrace-run: error launching '%s'" % executable )
52+ raise
4153
42- return status
4354
4455if __name__ == "__main__" :
4556 sys .exit (main ())
0 commit comments