Commit 42cb1feb authored by Jérome Perrin's avatar Jérome Perrin

new `slapos console script.py`style invocation

To execute a slapconsole script, we could only do:

`slapos console < script.py`
but this does not stop in case of error

or
 `echo 'execfile("script.py")' | slapos console`
but this is too complicated.

Extend this API so that we can simply use

`slapos console script.py`

inspired by `python script.py`


/reviewed-on nexedi/slapos.core!39
parents bbab9e01 afed8dd7
...@@ -79,6 +79,9 @@ class ConsoleCommand(ClientConfigCommand): ...@@ -79,6 +79,9 @@ class ConsoleCommand(ClientConfigCommand):
action='store_true', action='store_true',
help='Use plain Python shell') help='Use plain Python shell')
shell.add_argument('script_file', nargs='?',
help='Script to run')
return ap return ap
def take_action(self, args): def take_action(self, args):
...@@ -86,6 +89,9 @@ class ConsoleCommand(ClientConfigCommand): ...@@ -86,6 +89,9 @@ class ConsoleCommand(ClientConfigCommand):
conf = ClientConfig(args, configp) conf = ClientConfig(args, configp)
local = init(conf, self.app.log) local = init(conf, self.app.log)
if args.script_file:
return execfile(args.script_file, globals(), local)
if not any([args.python, args.ipython, args.bpython]): if not any([args.python, args.ipython, args.bpython]):
args.ipython = True args.ipython = True
......
...@@ -28,11 +28,16 @@ ...@@ -28,11 +28,16 @@
import logging import logging
import pprint import pprint
import unittest import unittest
import tempfile
import StringIO
import sys
from mock import patch, create_autospec from mock import patch, create_autospec
import slapos.cli.list import slapos.cli.console
import slapos.cli.entry
import slapos.cli.info import slapos.cli.info
import slapos.cli.list
import slapos.cli.supervisorctl import slapos.cli.supervisorctl
from slapos.client import ClientConfig from slapos.client import ClientConfig
import slapos.grid.svcbackend import slapos.grid.svcbackend
...@@ -151,3 +156,55 @@ class TestCliSupervisorctl(CliMixin): ...@@ -151,3 +156,55 @@ class TestCliSupervisorctl(CliMixin):
slapos.cli.supervisorctl.do_supervisorctl(self.logger, instance_root, ['status'], True) slapos.cli.supervisorctl.do_supervisorctl(self.logger, instance_root, ['status'], True)
self.assertFalse(launchSupervisord.called) self.assertFalse(launchSupervisord.called)
class TestCliConsole(unittest.TestCase):
def setUp(self):
cp = slapos.slap.ComputerPartition('computer_id', 'partition_id')
cp._parameter_dict = {'parameter_name': 'parameter_value'}
request_patch = patch.object(slapos.slap.OpenOrder, 'request', return_value = cp)
self.mock_request = request_patch.start()
self.config_file = tempfile.NamedTemporaryFile()
self.config_file.write('''[slapos]
master_url=null
''')
self.config_file.flush()
def tearDown(self):
self.mock_request.stop()
self.config_file.close()
def test_console_interactive(self):
app = slapos.cli.entry.SlapOSApp()
saved_stdin = sys.stdin
saved_stdout = sys.stdout
try:
sys.stdin = app_stdin = StringIO.StringIO(
"""print request('software_release', 'instance').getInstanceParameterDict()['parameter_name']\n""")
sys.stdout = app_stdout = StringIO.StringIO()
app.run(('console', '--cfg', self.config_file.name))
finally:
sys.sdin = saved_stdin
sys.stdout = saved_stdout
self.mock_request.assert_called_once_with('software_release', 'instance')
self.assertIn('parameter_value', app_stdout.getvalue())
def test_console_script(self):
with tempfile.NamedTemporaryFile() as script:
script.write(
"""print request('software_release', 'instance').getInstanceParameterDict()['parameter_name']\n""")
script.flush()
app = slapos.cli.entry.SlapOSApp()
saved_stdout = sys.stdout
try:
sys.stdout = app_stdout = StringIO.StringIO()
app.run(('console', '--cfg', self.config_file.name, script.name))
finally:
sys.stdout = saved_stdout
self.mock_request.assert_called_once_with('software_release', 'instance')
self.assertIn('parameter_value', app_stdout.getvalue())
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