blob: d47c8c0796220defe744146bad18ed18ce3782fc [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.
69 syntax_class = getattr(topic_module, topic)
70 syntax_obj = syntax_class()
71
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:
79 syntax_obj.invalid_syntax('No action argument')
80
81 # Instantiate a topic object
82 try:
83 topic_class = getattr(topic_module, topic + '_' + action)
84 except AttributeError:
85 syntax_obj.invalid_syntax('Invalid action %s' % action)
86
87 topic_obj = topic_class()
88
89 topic_obj.parse()
90 try:
91 try:
92 results = topic_obj.execute()
93 except topic_common.CliError:
94 pass
95 except Exception, err:
showardfb64e6a2009-04-22 21:01:18 +000096 traceback.print_exc()
mblighbe630eb2008-08-01 16:41:48 +000097 topic_obj.generic_error("Unexpected exception: %s" % err)
98 else:
mblighcae0da72008-10-18 14:28:13 +000099 try:
100 topic_obj.output(results)
101 except Exception:
102 traceback.print_exc()
mblighbe630eb2008-08-01 16:41:48 +0000103 finally:
104 return topic_obj.show_all_failures()