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