Commit 64879137 authored by Joanne Hugé's avatar Joanne Hugé Committed by Thomas Gambier

console: support args when passing a script file

parent 92b93714
......@@ -27,6 +27,7 @@
#
##############################################################################
import sys
import textwrap
from slapos.cli.config import ClientConfigCommand
......@@ -81,8 +82,8 @@ class ConsoleCommand(ClientConfigCommand):
action='store_true',
help='Use plain Python shell')
shell.add_argument('script_file', nargs='?',
help='Script to run')
shell.add_argument('script_file_and_arguments', nargs='*',
help='Script to run, with arguments', default=[])
return ap
......@@ -91,10 +92,12 @@ class ConsoleCommand(ClientConfigCommand):
conf = ClientConfig(args, configp)
local = init(conf, self.app.log)
if args.script_file:
with open(args.script_file) as f:
code = compile(f.read(), args.script_file, 'exec')
local['__file__'] = args.script_file
if args.script_file_and_arguments:
script_file = args.script_file_and_arguments[0]
with open(script_file) as f:
code = compile(f.read(), script_file, 'exec')
local['__file__'] = script_file
sys.argv = args.script_file_and_arguments
return exec_(code, local, local)
if not any([args.python, args.ipython, args.bpython]):
......
......@@ -874,6 +874,15 @@ print(request('software_release', 'instance').getInstanceParameterDict()['parame
app.run(('console', '--cfg', config_file, script.name))
self.assertIn('OK __file__ is set to script', stdout.getvalue())
def test_console_script_and_args(self):
with self._test_console() as (app, config_file, mock_request, stdout), \
tempfile.NamedTemporaryFile('w') as script:
script.write('import sys; print(sys.argv)')
script.flush()
app.run(('console', '--cfg', config_file, script.name, 'arg1', 'arg2'))
self.assertEqual(
stdout.getvalue().strip(), str([script.name, 'arg1', 'arg2']))
class TestCliComplete(CliMixin):
def test_complete_bash(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment