def finish_parse_and_run(parser, cmd_name, env_format_error): """Finish parsing after we know the command to run.""" # add the found command to the parser and re-run then re-parse command = parser.add_command(cmd_name) args, unknown = parser.parse_known_args() # Now that we know what command this is and what its args are, determine # whether we can continue with a bad environment and raise if not. if env_format_error: subcommand = getattr(args, "config_command", None) if (cmd_name, subcommand) != ("config", "edit"): raise env_format_error # many operations will fail without a working directory. set_working_dir() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown)
def __call__(self): """Run the script's main() according to command line parameters.""" try: if self.args.debug_traps: log.set_exception_trap(False) if self.args.version: _show_version() elif self.args.profile: self._profile() elif self.args.pdb: pdb.runctx("self._main()", globals(), locals()) else: return self._main() except KeyboardInterrupt: if self.args.pdb: raise else: raise KeyboardInterrupt("Interrupted... quitting.")
def __call__(self): """Run the script's _main() according to command line parameters.""" try: if self.args.debug_traps: log.set_exception_trap(False) if self.args.version: _show_version() elif self.args.profile: self._profile() elif self.args.pdb: pdb.runctx("self._main()", globals(), locals()) else: self._main() except KeyboardInterrupt: if self.args.pdb: raise else: raise KeyboardInterrupt("Interrupted... quitting.") return self._exit_status
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # Recover stored LD_LIBRARY_PATH variables from spack shell function # This is necessary because MacOS System Integrity Protection clears # (DY?)LD_LIBRARY_PATH variables on process start. # Spack clears these variables before building and installing packages, # but needs to know the prior state for commands like `spack load` and # `spack env activate that modify the user environment. recovered_vars = ('LD_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'DYLD_FALLBACK_LIBRARY_PATH') for var in recovered_vars: stored_var_name = 'SPACK_%s' % var if stored_var_name in os.environ: os.environ[var] = os.environ[stored_var_name] # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes # activate an environment if one was specified on the command line if not args.no_env: env = ev.find_environment(args) if env: ev.activate(env, args.use_env_repo, add_view=False) if args.print_shell_vars: print_setup_info(*args.print_shell_vars.split(',')) return 0 # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: print(get_version()) return 0 elif args.help: sys.stdout.write(parser.format_help(level=args.help)) return 0 elif not args.command: parser.print_help() return 1 try: # ensure options on spack command come before everything setup_main_options(args) # Try to load the particular command the caller asked for. cmd_name = args.command[0] cmd_name = aliases.get(cmd_name, cmd_name) command = parser.add_command(cmd_name) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # many operations will fail without a working directory. set_working_dir() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown) except SpackError as e: tty.debug(e) e.die() # gracefully die on any SpackErrors except Exception as e: if spack.config.get('config:debug'): raise tty.die(e) except KeyboardInterrupt: if spack.config.get('config:debug'): raise sys.stderr.write('\n') tty.die("Keyboard interrupt.") except SystemExit as e: if spack.config.get('config:debug'): traceback.print_exc() return e.code
def _main(argv=None): """Logic for the main entry point for the Spack command. ``main()`` calls ``_main()`` and catches any errors that emerge. ``_main()`` handles: 1. Parsing arguments; 2. Setting up configuration; and 3. Finding and executing a Spack command. Args: argv (list or None): command line arguments, NOT including the executable name. If None, parses from ``sys.argv``. """ # ------------------------------------------------------------------------ # main() is tricky to get right, so be careful where you put things. # # Things in this first part of `main()` should *not* require any # configuration. This doesn't include much -- setting up th parser, # restoring some key environment variables, very simple CLI options, etc. # ------------------------------------------------------------------------ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # Recover stored LD_LIBRARY_PATH variables from spack shell function # This is necessary because MacOS System Integrity Protection clears # (DY?)LD_LIBRARY_PATH variables on process start. # Spack clears these variables before building and installing packages, # but needs to know the prior state for commands like `spack load` and # `spack env activate that modify the user environment. recovered_vars = ('LD_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'DYLD_FALLBACK_LIBRARY_PATH') for var in recovered_vars: stored_var_name = 'SPACK_%s' % var if stored_var_name in os.environ: os.environ[var] = os.environ[stored_var_name] # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: print(get_version()) return 0 elif args.help: sys.stdout.write(parser.format_help(level=args.help)) return 0 # ------------------------------------------------------------------------ # This part of the `main()` sets up Spack's configuration. # # We set command line options (like --debug), then command line config # scopes, then environment configuration here. # ------------------------------------------------------------------------ # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes # ensure options on spack command come before everything setup_main_options(args) # activate an environment if one was specified on the command line env_format_error = None if not args.no_env: try: env = spack.cmd.find_environment(args) if env: ev.activate(env, args.use_env_repo) except spack.config.ConfigFormatError as e: # print the context but delay this exception so that commands like # `spack config edit` can still work with a bad environment. e.print_context() env_format_error = e # ------------------------------------------------------------------------ # Things that require configuration should go below here # ------------------------------------------------------------------------ if args.print_shell_vars: print_setup_info(*args.print_shell_vars.split(',')) return 0 # At this point we've considered all the options to spack itself, so we # need a command or we're done. if not args.command: parser.print_help() return 1 # Try to load the particular command the caller asked for. cmd_name = args.command[0] cmd_name = aliases.get(cmd_name, cmd_name) command = parser.add_command(cmd_name) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # Now that we know what command this is and what its args are, determine # whether we can continue with a bad environment and raise if not. if env_format_error: subcommand = getattr(args, "config_command", None) if (cmd_name, subcommand) != ("config", "edit"): raise env_format_error # many operations will fail without a working directory. set_working_dir() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown)
def main(argv=None): """This is the entry point for the pymod command. Parameters ---------- argv : list of str or None command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Pull out the shell from argv argv = argv or sys.argv[1:] assert len(argv) >= 1 shell = argv.pop(0) shells = ("bash", "csh", "python") if shell not in shells: raise ValueError("shell argument must by one of {0}".format(shells)) # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from pymod.command when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument("command", nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) args.shell = shell # Just print help and exit if run with no arguments at all no_args = len(argv) == 0 if no_args: with redirect_stdout(): parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: sys.stderr.write(str(pymod.pymod_version) + "\n") return 0 elif args.help: message = parser.format_help(level=args.help) with redirect_stdout(): sys.stderr.write(message) return 0 elif not args.command: with redirect_stdout(): parser.print_help() return 1 try: # ensure options on pymod command come before everything setup_main_options(args) # Try to load the particular command the caller asked for. If there # is no module for it, just die. cmd_name = args.command[0] cmd_name = aliases.get(cmd_name, cmd_name) try: command = parser.add_command(cmd_name) except ImportError: # if pymod.config.get('config:debug'): # raise tty.die("Unknown command: %s" % args.command[0]) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args(argv) # many operations will fail without a working directory. set_working_dir() # now we can actually execute the command. if args.pdb: import pdb pdb.runctx("_invoke_command(command, parser, args, unknown)", globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown) except Exception as e: if pymod.config.get("debug"): raise tty.die(str(e)) except KeyboardInterrupt: sys.stderr.write("\n") tty.die("Keyboard interrupt.") except SystemExit as e: return e.code
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # activate an environment if one was specified on the command line if not args.no_env: env = ev.find_environment(args) if env: ev.activate(env, args.use_env_repo) # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes if args.print_shell_vars: print_setup_info(*args.print_shell_vars.split(',')) return 0 # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: print(spack.spack_version) return 0 elif args.help: sys.stdout.write(parser.format_help(level=args.help)) return 0 elif not args.command: parser.print_help() return 1 try: # ensure options on spack command come before everything setup_main_options(args) # Try to load the particular command the caller asked for. If there # is no module for it, just die. cmd_name = args.command[0] cmd_name = aliases.get(cmd_name, cmd_name) command = parser.add_command(cmd_name) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # many operations will fail without a working directory. set_working_dir() # pre-run hooks happen after we know we have a valid working dir spack.hooks.pre_run() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown) except SpackError as e: tty.debug(e) e.die() # gracefully die on any SpackErrors except Exception as e: if spack.config.get('config:debug'): raise tty.die(e) except KeyboardInterrupt: if spack.config.get('config:debug'): raise sys.stderr.write('\n') tty.die("Keyboard interrupt.") except SystemExit as e: if spack.config.get('config:debug'): traceback.print_exc() return e.code
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # activate an environment if one was specified on the command line if not args.no_env: env = ev.find_environment(args) if env: ev.activate(env, args.use_env_repo) # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes if args.print_shell_vars: print_setup_info(*args.print_shell_vars.split(',')) return 0 # from spack.spec import Spec # #import llnl.util.tty as tty # import time # import llnl.util.lock as lk # # tty.set_verbose(args.verbose) # tty.set_debug(args.debug) # # def use_llnl_lock(path='/tmp/lockfile', start=10000, len=100): # mylk = lk.Lock(path, start, len) # mylk.acquire_write() # tty.warn('ENTERED LOCK') # time.sleep(1) # tty.warn('EXITING LOCK') # mylk.release_write() # # # def use_db_lock(): # s = Spec('m4') # s.concretize() # #tty.warn('Locking phase...\n') # time.sleep(0.1) # with spack.store.db.prefix_write_lock(s): # tty.warn('ENTERED LOCK') # time.sleep(1) # tty.warn('EXITING LOCK') # # #use_db_lock() # use_llnl_lock( # '/home/sknigh/code/github/spack/opt/spack/.spack-db/prefix_lock', # 4651886554793840719, 1) # exit() # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: print(spack.spack_version) return 0 elif args.help: sys.stdout.write(parser.format_help(level=args.help)) return 0 elif not args.command: parser.print_help() return 1 try: # ensure options on spack command come before everything setup_main_options(args) # Try to load the particular command the caller asked for. If there # is no module for it, just die. cmd_name = args.command[0] cmd_name = aliases.get(cmd_name, cmd_name) try: command = parser.add_command(cmd_name) except ImportError: if spack.config.get('config:debug'): raise tty.die("Unknown command: %s" % args.command[0]) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # many operations will fail without a working directory. set_working_dir() # pre-run hooks happen after we know we have a valid working dir spack.hooks.pre_run() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown) except SpackError as e: e.die() # gracefully die on any SpackErrors except Exception as e: if spack.config.get('config:debug'): raise tty.die(str(e)) except KeyboardInterrupt: sys.stderr.write('\n') tty.die("Keyboard interrupt.") except SystemExit as e: return e.code
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes if args.print_shell_vars: print_setup_info(*args.print_shell_vars.split(',')) return 0 # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h, -H, and -V are special as they do not require a command, but # all the other options do nothing without a command. if args.version: print(spack.spack_version) return 0 elif args.help: sys.stdout.write(parser.format_help(level=args.help)) return 0 elif not args.command: parser.print_help() return 1 try: # ensure options on spack command come before everything setup_main_options(args) # Try to load the particular command the caller asked for. If there # is no module for it, just die. cmd_name = args.command[0] try: command = parser.add_command(cmd_name) except ImportError: if spack.config.get('config:debug'): raise tty.die("Unknown command: %s" % args.command[0]) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # many operations will fail without a working directory. set_working_dir() # pre-run hooks happen after we know we have a valid working dir spack.hooks.pre_run() # now we can actually execute the command. if args.spack_profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_invoke_command(command, parser, args, unknown)', globals(), locals()) return 0 else: return _invoke_command(command, parser, args, unknown) except SpackError as e: e.die() # gracefully die on any SpackErrors except Exception as e: if spack.config.get('config:debug'): raise tty.die(str(e)) except KeyboardInterrupt: sys.stderr.write('\n') tty.die("Keyboard interrupt.") except SystemExit as e: return e.code
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument('command', nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h and -V are special as they do not require a command, but all the # other options do nothing without a command. if not args.command: if args.version: print(spack.spack_version) return 0 else: parser.print_help() return 0 if args.help else 1 # Try to load the particular command the caller asked for. If there # is no module for it, just die. command_name = spack.cmd.get_python_name(args.command[0]) try: parser.add_command(command_name) except ImportError: if spack.debug: raise tty.die("Unknown command: %s" % args.command[0]) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # we now know whether options go with spack or the command if args.version: print(spack.spack_version) return 0 elif args.help: parser.print_help() return 0 # now we can actually execute the command. command = spack.cmd.get_command(command_name) try: if args.profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_main(command, parser, args, unknown)', globals(), locals()) return 0 else: return _main(command, parser, args, unknown) except SystemExit as e: return e.code
def update_event(self, inp=-1): self.set_output_val( 0, pdb.runctx(self.input(0), self.input(1), self.input(2)))
def main(argv=None): """This is the entry point for the Spack command. Args: argv (list of str or None): command line arguments, NOT including the executable name. If None, parses from sys.argv. """ # Create a parser with a simple positional argument first. We'll # lazily load the subcommand(s) we need later. This allows us to # avoid loading all the modules from spack.cmd when we don't need # them, which reduces startup latency. parser = make_argument_parser() parser.add_argument( 'command', metavar='COMMAND', nargs='?', action='store') args, unknown = parser.parse_known_args(argv) # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: parser.print_help() return 1 # -h and -V are special as they do not require a command, but all the # other options do nothing without a command. if not args.command: if args.version: print(spack.spack_version) return 0 else: parser.print_help() return 0 if args.help else 1 # Try to load the particular command the caller asked for. If there # is no module for it, just die. command_name = args.command.replace('-', '_') try: parser.add_command(command_name) except ImportError: if spack.debug: raise tty.die("Unknown command: %s" % args.command) # Re-parse with the proper sub-parser added. args, unknown = parser.parse_known_args() # we now know whether options go with spack or the command if args.version: print(spack.spack_version) return 0 elif args.help: parser.print_help() return 0 # now we can actually execute the command. command = spack.cmd.get_command(command_name) try: if args.profile or args.sorted_profile: _profile_wrapper(command, parser, args, unknown) elif args.pdb: import pdb pdb.runctx('_main(command, parser, args, unknown)', globals(), locals()) return 0 else: return _main(command, parser, args, unknown) except SystemExit as e: return e.code