error: Introduce exception context strings
[autotest-zwu.git] / cli / atest.py
bloba366c62c669fc9dc47127b3c1e2a7402dff38234
2 # Copyright 2008 Google Inc. All Rights Reserved.
4 """Command line interface for autotest
6 This module contains the generic CLI processing
8 See topic_common.py for a High Level Design and Algorithm.
10 This file figures out the topic and action from the 2 first arguments
11 on the command line and imports the site_<topic> or <topic> module.
13 It then creates a <topic>_<action> object, and calls it parses),
14 execute() and output() methods.
15 """
17 __author__ = 'jmeurin@google.com (Jean-Marc Eurin)'
19 import os, sys, optparse, re, traceback
21 import common
22 from autotest_lib.cli import topic_common
25 def 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()
35 # Normalize the various --help, -h and help to -h
36 sys.argv = [re.sub('--help|help', '-h', arg) for arg in sys.argv]
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')
48 if topic == '-h':
49 sys.argv.insert(1, '-h')
50 syntax_obj.parse()
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')
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')
67 # If we have a syntax error now, it should
68 # refer to the topic class.
69 topic_class = getattr(topic_module, topic)
70 topic_obj = topic_class()
72 if len(sys.argv) > 1:
73 action = sys.argv.pop(1)
75 if action == '-h':
76 action = 'help'
77 sys.argv.insert(1, '-h')
78 else:
79 topic_obj.invalid_syntax('No action argument')
81 # Any backward compatibility changes?
82 action = topic_obj.backward_compatibility(action, sys.argv)
84 # Instantiate a topic object
85 try:
86 action_class = getattr(topic_module, topic + '_' + action)
87 except AttributeError:
88 topic_obj.invalid_syntax('Invalid action %s' % action)
90 action_obj = action_class()
92 action_obj.parse()
93 try:
94 try:
95 results = action_obj.execute()
96 except topic_common.CliError:
97 pass
98 except Exception, err:
99 traceback.print_exc()
100 action_obj.generic_error("Unexpected exception: %s" % err)
101 else:
102 try:
103 action_obj.output(results)
104 except Exception:
105 traceback.print_exc()
106 finally:
107 return action_obj.show_all_failures()