mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | # |
| 4 | """Command line interface for autotest |
| 5 | |
| 6 | This module contains the generic CLI processing |
| 7 | |
| 8 | See topic_common.py for a High Level Design and Algorithm. |
| 9 | |
| 10 | This file figures out the topic and action from the 2 first arguments |
Allen Li | b774aa2 | 2017-02-01 17:42:15 -0800 | [diff] [blame] | 11 | on the command line and imports the <topic> module. |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 12 | |
| 13 | It then creates a <topic>_<action> object, and calls it parses), |
| 14 | execute() and output() methods. |
| 15 | """ |
| 16 | |
Gregory Nisbet | 8d60b1c | 2020-07-14 16:26:46 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
| 18 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 19 | __author__ = 'jmeurin@google.com (Jean-Marc Eurin)' |
| 20 | |
Simran Basi | 9f364a6 | 2015-12-07 14:15:19 -0800 | [diff] [blame] | 21 | import os, sys, re, traceback |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 22 | |
| 23 | import common |
| 24 | from autotest_lib.cli import topic_common |
Prathmesh Prabhu | 8efaad7 | 2018-06-06 09:07:26 -0700 | [diff] [blame] | 25 | from autotest_lib.client.common_lib import lsbrelease_utils |
Simran Basi | 9f364a6 | 2015-12-07 14:15:19 -0800 | [diff] [blame] | 26 | from autotest_lib.server import utils |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def main(): |
| 30 | """ |
| 31 | The generic syntax is: |
| 32 | atest <topic> <action> <options> |
| 33 | atest-<topic> <action> <options> |
| 34 | atest --help |
| 35 | """ |
Prathmesh Prabhu | 8efaad7 | 2018-06-06 09:07:26 -0700 | [diff] [blame] | 36 | _disallow_root_user_on_moblab() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 37 | 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 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 57 | # Import the topic specific file |
| 58 | cli_dir = os.path.abspath(os.path.dirname(__file__)) |
Allen Li | b774aa2 | 2017-02-01 17:42:15 -0800 | [diff] [blame] | 59 | if not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)): |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 60 | 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. |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 66 | topic_class = getattr(topic_module, topic) |
| 67 | topic_obj = topic_class() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 68 | |
| 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: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 76 | topic_obj.invalid_syntax('No action argument') |
| 77 | |
| 78 | # Any backward compatibility changes? |
| 79 | action = topic_obj.backward_compatibility(action, sys.argv) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 80 | |
| 81 | # Instantiate a topic object |
| 82 | try: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 83 | action_class = getattr(topic_module, topic + '_' + action) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 84 | except AttributeError: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 85 | topic_obj.invalid_syntax('Invalid action %s' % action) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 86 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 87 | action_obj = action_class() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 88 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 89 | action_obj.parse() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 90 | try: |
| 91 | try: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 92 | results = action_obj.execute() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 93 | except topic_common.CliError: |
| 94 | pass |
Gregory Nisbet | 8d60b1c | 2020-07-14 16:26:46 -0700 | [diff] [blame] | 95 | except Exception as err: |
showard | fb64e6a | 2009-04-22 21:01:18 +0000 | [diff] [blame] | 96 | traceback.print_exc() |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 97 | action_obj.generic_error("Unexpected exception: %s" % err) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 98 | else: |
mbligh | cae0da7 | 2008-10-18 14:28:13 +0000 | [diff] [blame] | 99 | try: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 100 | action_obj.output(results) |
mbligh | cae0da7 | 2008-10-18 14:28:13 +0000 | [diff] [blame] | 101 | except Exception: |
| 102 | traceback.print_exc() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 103 | finally: |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 104 | return action_obj.show_all_failures() |
Prathmesh Prabhu | 8efaad7 | 2018-06-06 09:07:26 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def _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() |