blob: a366c62c669fc9dc47127b3c1e2a7402dff38234 [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#
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 site_<topic> or <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
mblighbac1d2f2008-11-07 00:10:19 +000019import os, sys, optparse, re, traceback
mblighbe630eb2008-08-01 16:41:48 +000020
21import common
22from autotest_lib.cli import topic_common
23
24
25def main():
26 """
27 The generic syntax is:
28 atest <topic> <action> <options>
29 atest-<topic> <action> <options>
30 atest --help
31 """
32 cli = os.path.basename(sys.argv[0])
33 syntax_obj = topic_common.atest()
34
35 # Normalize the various --help, -h and help to -h
36 sys.argv = [re.sub('--help|help', '-h', arg) for arg in sys.argv]
37
38 match = re.search('^atest-(\w+)$', cli)
39 if match:
40 topic = match.group(1)
41 else:
42 if len(sys.argv) > 1:
43 topic = sys.argv.pop(1)
44 else:
45 syntax_obj.invalid_syntax('No topic argument')
46
47
48 if topic == '-h':
49 sys.argv.insert(1, '-h')
50 syntax_obj.parse()
51
52 # The ignore flag should *only* be used by unittests.
53 ignore_site = '--ignore_site_file' in sys.argv
54 if ignore_site:
55 sys.argv.remove('--ignore_site_file')
56
57 # Import the topic specific file
58 cli_dir = os.path.abspath(os.path.dirname(__file__))
59 if (not ignore_site and
60 os.path.exists(os.path.join(cli_dir, 'site_%s.py' % topic))):
61 topic = 'site_%s' % topic
62 elif not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)):
63 syntax_obj.invalid_syntax('Invalid topic %s' % topic)
64 topic_module = common.setup_modules.import_module(topic,
65 'autotest_lib.cli')
66
67 # If we have a syntax error now, it should
68 # refer to the topic class.
mbligh5a496082009-08-03 16:44:54 +000069 topic_class = getattr(topic_module, topic)
70 topic_obj = topic_class()
mblighbe630eb2008-08-01 16:41:48 +000071
72 if len(sys.argv) > 1:
73 action = sys.argv.pop(1)
74
75 if action == '-h':
76 action = 'help'
77 sys.argv.insert(1, '-h')
78 else:
mbligh5a496082009-08-03 16:44:54 +000079 topic_obj.invalid_syntax('No action argument')
80
81 # Any backward compatibility changes?
82 action = topic_obj.backward_compatibility(action, sys.argv)
mblighbe630eb2008-08-01 16:41:48 +000083
84 # Instantiate a topic object
85 try:
mbligh5a496082009-08-03 16:44:54 +000086 action_class = getattr(topic_module, topic + '_' + action)
mblighbe630eb2008-08-01 16:41:48 +000087 except AttributeError:
mbligh5a496082009-08-03 16:44:54 +000088 topic_obj.invalid_syntax('Invalid action %s' % action)
mblighbe630eb2008-08-01 16:41:48 +000089
mbligh5a496082009-08-03 16:44:54 +000090 action_obj = action_class()
mblighbe630eb2008-08-01 16:41:48 +000091
mbligh5a496082009-08-03 16:44:54 +000092 action_obj.parse()
mblighbe630eb2008-08-01 16:41:48 +000093 try:
94 try:
mbligh5a496082009-08-03 16:44:54 +000095 results = action_obj.execute()
mblighbe630eb2008-08-01 16:41:48 +000096 except topic_common.CliError:
97 pass
98 except Exception, err:
showardfb64e6a2009-04-22 21:01:18 +000099 traceback.print_exc()
mbligh5a496082009-08-03 16:44:54 +0000100 action_obj.generic_error("Unexpected exception: %s" % err)
mblighbe630eb2008-08-01 16:41:48 +0000101 else:
mblighcae0da72008-10-18 14:28:13 +0000102 try:
mbligh5a496082009-08-03 16:44:54 +0000103 action_obj.output(results)
mblighcae0da72008-10-18 14:28:13 +0000104 except Exception:
105 traceback.print_exc()
mblighbe630eb2008-08-01 16:41:48 +0000106 finally:
mbligh5a496082009-08-03 16:44:54 +0000107 return action_obj.show_all_failures()