def run_from_argv(self, argv): """Execute the command using the provided arguments. The options and commandline arguments will be parsed from ``argv`` and the commands ``main`` method will be called. """ self.config = load_config() parser = self.create_parser(self.config, argv) parser.add_argument('args', nargs=argparse.REMAINDER) self.options = parser.parse_args(argv[2:]) args = self.options.args # Check that the proper number of arguments have been provided. argspec = inspect.getargspec(self.main) minargs = len(argspec[0]) - 1 maxargs = minargs # Arguments that have a default value are considered optional. if argspec[3] is not None: minargs -= len(argspec[3]) if argspec[1] is not None: maxargs = None if len(args) < minargs or (maxargs is not None and len(args) > maxargs): parser.error('Invalid number of arguments provided') sys.exit(1) self.init_logging() logging.debug('Command line: %s', subprocess.list2cmdline(argv)) try: exit_code = self.main(*args) or 0 except CommandError as e: if isinstance(e, ParseError): parser.error(e) elif self.options.debug: raise logging.error(e) exit_code = 1 except CommandExit as e: exit_code = e.exit_code except Exception as e: # If debugging is on, we'll let python spit out the # stack trace and report the exception, otherwise # we'll suppress the trace and print the exception # manually. if self.options.debug: raise logging.critical(e) exit_code = 1 cleanup_tempfiles() sys.exit(exit_code)
def run_from_argv(self, argv): """Execute the command using the provided arguments. The options and commandline arguments will be parsed from ``argv`` and the commands ``main`` method will be called. """ parser = self.create_arg_parser(argv) self.options = parser.parse_args(argv[2:]) args = self.options.args # Check that the proper number of arguments have been provided. if six.PY3: argspec = inspect.getfullargspec(self.main) else: argspec = inspect.getargspec(self.main) minargs = len(argspec[0]) - 1 maxargs = minargs # Arguments that have a default value are considered optional. if argspec[3] is not None: minargs -= len(argspec[3]) if argspec[1] is not None: maxargs = None if len(args) < minargs or (maxargs is not None and len(args) > maxargs): parser.error('Invalid number of arguments provided') sys.exit(1) self.init_logging() log_command_line('Command line: %s', argv) try: exit_code = self.main(*args) or 0 except CommandError as e: if isinstance(e, ParseError): parser.error(e) elif self.options.debug: raise logging.error(e) exit_code = 1 except CommandExit as e: exit_code = e.exit_code except Exception as e: # If debugging is on, we'll let python spit out the # stack trace and report the exception, otherwise # we'll suppress the trace and print the exception # manually. if self.options.debug: raise logging.critical(e) exit_code = 1 cleanup_tempfiles() sys.exit(exit_code)
def tearDown(self): super(TestCase, self).tearDown() os.chdir(self._old_cwd) cleanup_tempfiles() if self.old_home: self.set_user_home(self.old_home)
def tearDown(self): super(RBTestBase, self).tearDown() os.chdir(self._old_cwd) cleanup_tempfiles() if self.old_home: os.environ['HOME'] = self.old_home
def die(msg=None): """ Cleanly exits the program with an error message. Erases all remaining temporary files. """ from rbtools.utils.filesystem import cleanup_tempfiles cleanup_tempfiles() if msg: print msg sys.exit(1)
def die(msg=None): """Cleanly exits the program with an error message. Erases all remaining temporary files. """ from rbtools.utils.filesystem import cleanup_tempfiles cleanup_tempfiles() if msg: print(msg) sys.exit(1)
def test_make_empty_files(self): """Testing make_empty_files""" # Use make_tempdir to get a unique directory name tmpdir = make_tempdir() self.assertTrue(os.path.isdir(tmpdir)) cleanup_tempfiles() fname = os.path.join(tmpdir, 'file') make_empty_files([fname]) self.assertTrue(os.path.isdir(tmpdir)) self.assertTrue(os.path.isfile(fname)) self.assertEqual(os.stat(fname).st_uid, os.geteuid()) self.assertTrue(os.access(fname, os.R_OK | os.W_OK)) shutil.rmtree(tmpdir, ignore_errors=True)
def test_make_empty_files(self): """Testing 'make_empty_files' method.""" # Use make_tempdir to get a unique directory name tmpdir = filesystem.make_tempdir() self.assertTrue(os.path.isdir(tmpdir)) filesystem.cleanup_tempfiles() fname = os.path.join(tmpdir, 'file') filesystem.make_empty_files([fname]) self.assertTrue(os.path.isdir(tmpdir)) self.assertTrue(os.path.isfile(fname)) self.assertEqual(os.stat(fname).st_uid, os.geteuid()) self.assertTrue(os.access(fname, os.R_OK | os.W_OK)) shutil.rmtree(tmpdir, ignore_errors=True)
logging.error(e) exit_code = 1 except CommandExit, e: exit_code = e.exit_code except Exception, e: # If debugging is on, we'll let python spit out the # stack trace and report the exception, otherwise # we'll suppress the trace and print the exception # manually. if self.options.debug: raise logging.critical(e) exit_code = 1 cleanup_tempfiles() sys.exit(exit_code) def initialize_scm_tool(self, client_name=None): """Initialize the SCM tool for the current working directory.""" repository_info, tool = scan_usable_client(self.options, client_name=client_name) tool.user_config = self.config tool.configs = [self.config] tool.check_options() return repository_info, tool def setup_tool(self, tool, api_root=None): """Performs extra initialization on the tool. If api_root is not provided we'll assume we want to
def tearDown(self): cleanup_tempfiles()
elif self.options.debug: raise exit_code = 1 except Exception, e: # If debugging is on, we'll let python spit out the # stack trace and report the exception, otherwise # we'll suppress the trace and print the exception # manually. if self.options.debug: raise logging.critical(e) exit_code = 1 cleanup_tempfiles() sys.exit(exit_code) def initialize_scm_tool(self): """Initialize the SCM tool for the current working directory.""" repository_info, tool = scan_usable_client(self.options) tool.user_config = self.config tool.configs = [self.config] tool.check_options() return repository_info, tool def setup_tool(self, tool, api_root=None): """Performs extra initialization on the tool. If api_root is not provided we'll assume we want to initialize the tool using only local information
def tearDown(self): os.chdir(self._old_cwd) cleanup_tempfiles()
def tearDown(self): try: self.revert_user_home() except OSError: self.reset_user_home() cleanup_tempfiles()
def tearDown(self): os.chdir(self._old_cwd) cleanup_tempfiles() if self.old_home: os.environ['HOME'] = self.old_home