mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime
This commit is contained in:
parent
27e3745589
commit
74e1670fc6
2 changed files with 23 additions and 0 deletions
1
changelog/62381.added
Normal file
1
changelog/62381.added
Normal file
|
@ -0,0 +1 @@
|
|||
Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime
|
22
run.py
22
run.py
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue