diff options
author | Mike Nolta <mike@nolta.net> | 2015-12-14 15:22:51 -0500 |
---|---|---|
committer | Mike Nolta <mike@nolta.net> | 2015-12-19 16:32:17 -0500 |
commit | 429e15c4a64d29926f1b7a7168c23592e7dec0b7 (patch) | |
tree | 021ea5ed890219d1ded83a45e0eaa92ae5b31625 | |
parent | df5dc1c9bb29a56cc1cf6b4bce903b8d4f40b52e (diff) | |
download | spack-429e15c4a64d29926f1b7a7168c23592e7dec0b7.tar.gz spack-429e15c4a64d29926f1b7a7168c23592e7dec0b7.tar.bz2 spack-429e15c4a64d29926f1b7a7168c23592e7dec0b7.tar.xz spack-429e15c4a64d29926f1b7a7168c23592e7dec0b7.zip |
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.
-rw-r--r-- | lib/spack/spack/cmd/python.py | 10 |
1 files changed, 8 insertions, 2 deletions
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 @@ -32,13 +32,16 @@ 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.") 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') |