blob: 524ad718d8beb85d352b12ea621dd842469eafaa [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
Gregory Nisbet8d60b1c2020-07-14 16:26:46 -070017from __future__ import print_function
18
mblighbe630eb2008-08-01 16:41:48 +000019__author__ = 'jmeurin@google.com (Jean-Marc Eurin)'
20
Simran Basi9f364a62015-12-07 14:15:19 -080021import os, sys, re, traceback
mblighbe630eb2008-08-01 16:41:48 +000022
23import common
24from autotest_lib.cli import topic_common
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -070025from autotest_lib.client.common_lib import lsbrelease_utils
Simran Basi9f364a62015-12-07 14:15:19 -080026from autotest_lib.server import utils
mblighbe630eb2008-08-01 16:41:48 +000027
28
29def main():
30 """
31 The generic syntax is:
32 atest <topic> <action> <options>
33 atest-<topic> <action> <options>
34 atest --help
35 """
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -070036 _disallow_root_user_on_moblab()
mblighbe630eb2008-08-01 16:41:48 +000037 cli = os.path.basename(sys.argv[0])
38 syntax_obj = topic_common.atest()
39
40 # Normalize the various --help, -h and help to -h
41 sys.argv = [re.sub('--help|help', '-h', arg) for arg in sys.argv]
42
43 match = re.search('^atest-(\w+)$', cli)
44 if match:
45 topic = match.group(1)
46 else:
47 if len(sys.argv) > 1:
48 topic = sys.argv.pop(1)
49 else:
50 syntax_obj.invalid_syntax('No topic argument')
51
52
53 if topic == '-h':
54 sys.argv.insert(1, '-h')
55 syntax_obj.parse()
56
mblighbe630eb2008-08-01 16:41:48 +000057 # Import the topic specific file
58 cli_dir = os.path.abspath(os.path.dirname(__file__))
Allen Lib774aa22017-02-01 17:42:15 -080059 if not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)):
mblighbe630eb2008-08-01 16:41:48 +000060 syntax_obj.invalid_syntax('Invalid topic %s' % topic)
61 topic_module = common.setup_modules.import_module(topic,
62 'autotest_lib.cli')
63
64 # If we have a syntax error now, it should
65 # refer to the topic class.
mbligh5a496082009-08-03 16:44:54 +000066 topic_class = getattr(topic_module, topic)
67 topic_obj = topic_class()
mblighbe630eb2008-08-01 16:41:48 +000068
69 if len(sys.argv) > 1:
70 action = sys.argv.pop(1)
71
72 if action == '-h':
73 action = 'help'
74 sys.argv.insert(1, '-h')
75 else:
mbligh5a496082009-08-03 16:44:54 +000076 topic_obj.invalid_syntax('No action argument')
77
78 # Any backward compatibility changes?
79 action = topic_obj.backward_compatibility(action, sys.argv)
mblighbe630eb2008-08-01 16:41:48 +000080
81 # Instantiate a topic object
82 try:
mbligh5a496082009-08-03 16:44:54 +000083 action_class = getattr(topic_module, topic + '_' + action)
mblighbe630eb2008-08-01 16:41:48 +000084 except AttributeError:
mbligh5a496082009-08-03 16:44:54 +000085 topic_obj.invalid_syntax('Invalid action %s' % action)
mblighbe630eb2008-08-01 16:41:48 +000086
mbligh5a496082009-08-03 16:44:54 +000087 action_obj = action_class()
mblighbe630eb2008-08-01 16:41:48 +000088
mbligh5a496082009-08-03 16:44:54 +000089 action_obj.parse()
mblighbe630eb2008-08-01 16:41:48 +000090 try:
91 try:
mbligh5a496082009-08-03 16:44:54 +000092 results = action_obj.execute()
mblighbe630eb2008-08-01 16:41:48 +000093 except topic_common.CliError:
94 pass
Gregory Nisbet8d60b1c2020-07-14 16:26:46 -070095 except Exception as err:
showardfb64e6a2009-04-22 21:01:18 +000096 traceback.print_exc()
mbligh5a496082009-08-03 16:44:54 +000097 action_obj.generic_error("Unexpected exception: %s" % err)
mblighbe630eb2008-08-01 16:41:48 +000098 else:
mblighcae0da72008-10-18 14:28:13 +000099 try:
mbligh5a496082009-08-03 16:44:54 +0000100 action_obj.output(results)
mblighcae0da72008-10-18 14:28:13 +0000101 except Exception:
102 traceback.print_exc()
mblighbe630eb2008-08-01 16:41:48 +0000103 finally:
mbligh5a496082009-08-03 16:44:54 +0000104 return action_obj.show_all_failures()
Prathmesh Prabhu8efaad72018-06-06 09:07:26 -0700105
106
107def _disallow_root_user_on_moblab():
108 """Running these tools as root interferes with moblab services"""
109 if lsbrelease_utils.is_moblab():
110 utils.verify_not_root_user()