Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime

This commit is contained in:
Lukas Raska 2022-07-27 07:58:42 +02:00 committed by Megan Wilhite
parent 27e3745589
commit 74e1670fc6
2 changed files with 23 additions and 0 deletions

1
changelog/62381.added Normal file
View file

@ -0,0 +1 @@
Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime

22
run.py
View file

@ -30,6 +30,7 @@ AVAIL = (
"ssh",
"support",
"syndic",
"python",
)
@ -56,6 +57,19 @@ def py_shell():
shell.interact()
def python_runtime():
# extract the absolute script path to alter sys.path and specific dunder variables
script = pathlib.Path(sys.argv[2]).expanduser().absolute()
sys.path.insert(0, str(script.parent))
__file__ = str(script)
# update passed args so they don't start with "<binary> python"
sys.argv = sys.argv[2:]
with open(script) as f:
exec(f.read())
def redirect(argv):
"""
Change the args and redirect to another salt script
@ -70,6 +84,14 @@ def redirect(argv):
if cmd == "shell":
py_shell()
return
if cmd == "python":
if len(argv) < 3:
msg = "Must pass script location to this command"
print(msg, file=sys.stderr, flush=True)
sys.exit(1)
python_runtime()
return
if tiamatpip.cli.should_redirect_argv(argv):
tiamatpip.cli.process_pip_argv(argv)
return