Skip to content

Commit 7f97945

Browse files
committed
pythonpath try outs
1 parent 5e3587a commit 7f97945

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

aikido_zen/cli/__init__.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1+
import os
2+
import shutil
13
import sys
24
import subprocess
35
from typing import List
46

57
import 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("\nCommand 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

2731
def 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

4455
if __name__ == "__main__":
4556
sys.exit(main())

aikido_zen/cli/preload/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print("debug")
2+
raise Exception("debug")

aikido_zen/cli/preload/hypercorn/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)