1# 2# Copyright 2008 Google Inc. All Rights Reserved. 3# 4"""Command line interface for autotest 5 6This module contains the generic CLI processing 7 8See topic_common.py for a High Level Design and Algorithm. 9 10This file figures out the topic and action from the 2 first arguments 11on the command line and imports the <topic> module. 12 13It then creates a <topic>_<action> object, and calls it parses), 14execute() and output() methods. 15""" 16 17__author__ = 'jmeurin@google.com (Jean-Marc Eurin)' 18 19import os, sys, re, traceback 20 21import common 22from autotest_lib.cli import topic_common 23from autotest_lib.server import utils 24 25 26def main(): 27 """ 28 The generic syntax is: 29 atest <topic> <action> <options> 30 atest-<topic> <action> <options> 31 atest --help 32 """ 33 utils.verify_not_root_user() 34 cli = os.path.basename(sys.argv[0]) 35 syntax_obj = topic_common.atest() 36 37 # Normalize the various --help, -h and help to -h 38 sys.argv = [re.sub('--help|help', '-h', arg) for arg in sys.argv] 39 40 match = re.search('^atest-(\w+)$', cli) 41 if match: 42 topic = match.group(1) 43 else: 44 if len(sys.argv) > 1: 45 topic = sys.argv.pop(1) 46 else: 47 syntax_obj.invalid_syntax('No topic argument') 48 49 50 if topic == '-h': 51 sys.argv.insert(1, '-h') 52 syntax_obj.parse() 53 54 # Import the topic specific file 55 cli_dir = os.path.abspath(os.path.dirname(__file__)) 56 if not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)): 57 syntax_obj.invalid_syntax('Invalid topic %s' % topic) 58 topic_module = common.setup_modules.import_module(topic, 59 'autotest_lib.cli') 60 61 # If we have a syntax error now, it should 62 # refer to the topic class. 63 topic_class = getattr(topic_module, topic) 64 topic_obj = topic_class() 65 66 if len(sys.argv) > 1: 67 action = sys.argv.pop(1) 68 69 if action == '-h': 70 action = 'help' 71 sys.argv.insert(1, '-h') 72 else: 73 topic_obj.invalid_syntax('No action argument') 74 75 # Any backward compatibility changes? 76 action = topic_obj.backward_compatibility(action, sys.argv) 77 78 # Instantiate a topic object 79 try: 80 action_class = getattr(topic_module, topic + '_' + action) 81 except AttributeError: 82 topic_obj.invalid_syntax('Invalid action %s' % action) 83 84 action_obj = action_class() 85 86 action_obj.parse() 87 try: 88 try: 89 results = action_obj.execute() 90 except topic_common.CliError: 91 pass 92 except Exception, err: 93 traceback.print_exc() 94 action_obj.generic_error("Unexpected exception: %s" % err) 95 else: 96 try: 97 action_obj.output(results) 98 except Exception: 99 traceback.print_exc() 100 finally: 101 return action_obj.show_all_failures() 102