blob: 4a88424bce3ebe5056d7c0a1400c900989880f7c [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
Allen Lib774aa22017-02-01 17:42:15 -080011on the command line and imports the <topic> module.
mblighbe630eb2008-08-01 16:41:48 +000012
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
Simran Basi9f364a62015-12-07 14:15:19 -080019import os, sys, re, traceback
mblighbe630eb2008-08-01 16:41:48 +000020
21import common
22from autotest_lib.cli import topic_common
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -070023from autotest_lib.client.common_lib import lsbrelease_utils
Simran Basi9f364a62015-12-07 14:15:19 -080024from autotest_lib.server import utils
mblighbe630eb2008-08-01 16:41:48 +000025
26
27def main():
28 """
29 The generic syntax is:
30 atest <topic> <action> <options>
31 atest-<topic> <action> <options>
32 atest --help
33 """
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -070034 _disallow_root_user_on_moblab()
mblighbe630eb2008-08-01 16:41:48 +000035 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
mblighbe630eb2008-08-01 16:41:48 +000055 # Import the topic specific file
56 cli_dir = os.path.abspath(os.path.dirname(__file__))
Allen Lib774aa22017-02-01 17:42:15 -080057 if not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)):
mblighbe630eb2008-08-01 16:41:48 +000058 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.
mbligh5a496082009-08-03 16:44:54 +000064 topic_class = getattr(topic_module, topic)
65 topic_obj = topic_class()
mblighbe630eb2008-08-01 16:41:48 +000066
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:
mbligh5a496082009-08-03 16:44:54 +000074 topic_obj.invalid_syntax('No action argument')
75
76 # Any backward compatibility changes?
77 action = topic_obj.backward_compatibility(action, sys.argv)
mblighbe630eb2008-08-01 16:41:48 +000078
79 # Instantiate a topic object
80 try:
mbligh5a496082009-08-03 16:44:54 +000081 action_class = getattr(topic_module, topic + '_' + action)
mblighbe630eb2008-08-01 16:41:48 +000082 except AttributeError:
mbligh5a496082009-08-03 16:44:54 +000083 topic_obj.invalid_syntax('Invalid action %s' % action)
mblighbe630eb2008-08-01 16:41:48 +000084
mbligh5a496082009-08-03 16:44:54 +000085 action_obj = action_class()
mblighbe630eb2008-08-01 16:41:48 +000086
mbligh5a496082009-08-03 16:44:54 +000087 action_obj.parse()
mblighbe630eb2008-08-01 16:41:48 +000088 try:
89 try:
mbligh5a496082009-08-03 16:44:54 +000090 results = action_obj.execute()
mblighbe630eb2008-08-01 16:41:48 +000091 except topic_common.CliError:
92 pass
93 except Exception, err:
showardfb64e6a2009-04-22 21:01:18 +000094 traceback.print_exc()
mbligh5a496082009-08-03 16:44:54 +000095 action_obj.generic_error("Unexpected exception: %s" % err)
mblighbe630eb2008-08-01 16:41:48 +000096 else:
mblighcae0da72008-10-18 14:28:13 +000097 try:
mbligh5a496082009-08-03 16:44:54 +000098 action_obj.output(results)
mblighcae0da72008-10-18 14:28:13 +000099 except Exception:
100 traceback.print_exc()
mblighbe630eb2008-08-01 16:41:48 +0000101 finally:
mbligh5a496082009-08-03 16:44:54 +0000102 return action_obj.show_all_failures()
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -0700103
104
105def _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()