def GetOptions(cmd_name=None): """Returns the parser to use for commandline parsing. Args: cmd_name: The subcommand to import & add. Returns: A commandline.ArgumentParser object. """ parser = commandline.ArgumentParser(caching=True, default_log_level='notice') subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand') subparsers.required = True # We add all the commands so `cros --help ...` looks reasonable. # We add them in order also so the --help output is stable for users. for subcommand in sorted(command.ListCommands()): if subcommand == cmd_name: class_def = command.ImportCommand(cmd_name) epilog = getattr(class_def, 'EPILOG', None) sub_parser = subparsers.add_parser( cmd_name, description=class_def.__doc__, epilog=epilog, caching=class_def.use_caching_options, formatter_class=commandline.argparse. RawDescriptionHelpFormatter) class_def.AddParser(sub_parser) else: subparsers.add_parser(subcommand, add_help=False) return parser
def main(argv): try: parser = GetOptions(command.ListCommands()) # Cros currently does nothing without a subcmd. Print help if no args are # specified. if not argv: parser.print_help() return 1 namespace = parser.parse_args(argv) subcommand = namespace.command_class(namespace) try: code = _RunSubCommand(subcommand) except (commandline.ChrootRequiredError, commandline.ExecRequiredError): # The higher levels want these passed back, so oblige. raise except Exception as e: code = 1 logging.error('cros %s failed before completing.', subcommand.command_name) if namespace.debug: raise else: logging.error(e) if code is not None: return code return 0 except KeyboardInterrupt: logging.debug('Aborted due to keyboard interrupt.') return 1
def testFindModules(self): """Tests that we can return modules correctly when mocking out glob.""" fake_command_file = 'cros_command_test.py' filtered_file = 'cros_command_unittest.py' self.PatchObject(os, 'listdir', return_value=[fake_command_file, filtered_file]) self.assertEqual(command.ListCommands(), {'command-test'})
def main(argv): try: parser = GetOptions(command.ListCommands()) # Cros currently does nothing without a subcmd. Print help if no args are # specified. if not argv: parser.print_help() return 1 namespace = parser.parse_args(argv) subcommand = namespace.command_class(namespace) with stats.UploadContext() as queue: if subcommand.upload_stats: cmd_base = subcommand.options.command_class.command_name cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base) if cmd_stats: queue.put([ cmd_stats, stats.StatsUploader.URL, subcommand.upload_stats_timeout ]) # TODO: to make command completion faster, send an interrupt signal to the # stats uploader task after the subcommand completes. try: code = _RunSubCommand(subcommand) except (commandline.ChrootRequiredError, commandline.ExecRequiredError): # The higher levels want these passed back, so oblige. raise except Exception as e: code = 1 logging.error('cros %s failed before completing.', subcommand.command_name) if namespace.debug: raise else: logging.error(e) if code is not None: return code return 0 except KeyboardInterrupt: logging.debug('Aborted due to keyboard interrupt.') return 1
def testListCrosCommands(self): """Tests we get a sane `cros` list back.""" cros_commands = command.ListCommands() # Pick some commands that are likely to not go away. self.assertIn('chrome-sdk', cros_commands) self.assertIn('flash', cros_commands)