From 429e15c4a64d29926f1b7a7168c23592e7dec0b7 Mon Sep 17 00:00:00 2001 From: Mike Nolta Date: Mon, 14 Dec 2015 15:22:51 -0500 Subject: spack python: add -c option Allows passing program in as a string. Example: $ spack python -c 'print 2+3' 5 Also imports spack module by default into the environment. --- lib/spack/spack/cmd/python.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/python.py b/lib/spack/spack/cmd/python.py index e26b8d3e79..5325e8fd9a 100644 --- a/lib/spack/spack/cmd/python.py +++ b/lib/spack/spack/cmd/python.py @@ -31,6 +31,8 @@ import platform import spack def setup_parser(subparser): + subparser.add_argument( + '-c', dest='python_command', help='Command to execute.') subparser.add_argument( 'python_args', nargs=argparse.REMAINDER, help="File to run plus arguments.") @@ -38,7 +40,8 @@ description = "Launch an interpreter as spack would launch a command" def python(parser, args): # Fake a main python shell by setting __name__ to __main__. - console = code.InteractiveConsole({'__name__' : '__main__'}) + console = code.InteractiveConsole({'__name__' : '__main__', + 'spack' : spack}) if "PYTHONSTARTUP" in os.environ: startup_file = os.environ["PYTHONSTARTUP"] @@ -47,7 +50,10 @@ def python(parser, args): console.runsource(startup.read(), startup_file, 'exec') python_args = args.python_args - if python_args: + python_command = args.python_command + if python_command: + console.runsource(python_command) + elif python_args: sys.argv = python_args with open(python_args[0]) as file: console.runsource(file.read(), python_args[0], 'exec') -- cgit v1.2.3-60-g2f50