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.
This commit is contained in:
parent
df5dc1c9bb
commit
429e15c4a6
@ -31,6 +31,8 @@
|
||||
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 @@ def setup_parser(subparser):
|
||||
|
||||
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')
|
||||
|
Loading…
Reference in New Issue
Block a user