1be630eb07ddb84310fcc580acffcaa362a2dc606mbligh#
2be630eb07ddb84310fcc580acffcaa362a2dc606mbligh# Copyright 2008 Google Inc. All Rights Reserved.
3be630eb07ddb84310fcc580acffcaa362a2dc606mbligh#
4be630eb07ddb84310fcc580acffcaa362a2dc606mbligh"""Command line interface for autotest
5be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
6be630eb07ddb84310fcc580acffcaa362a2dc606mblighThis module contains the generic CLI processing
7be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
8be630eb07ddb84310fcc580acffcaa362a2dc606mblighSee topic_common.py for a High Level Design and Algorithm.
9be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
10be630eb07ddb84310fcc580acffcaa362a2dc606mblighThis file figures out the topic and action from the 2 first arguments
11be630eb07ddb84310fcc580acffcaa362a2dc606mblighon the command line and imports the site_<topic> or <topic> module.
12be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
13be630eb07ddb84310fcc580acffcaa362a2dc606mblighIt then creates a <topic>_<action> object, and calls it parses),
14be630eb07ddb84310fcc580acffcaa362a2dc606mblighexecute() and output() methods.
15be630eb07ddb84310fcc580acffcaa362a2dc606mbligh"""
16be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
17be630eb07ddb84310fcc580acffcaa362a2dc606mbligh__author__ = 'jmeurin@google.com (Jean-Marc Eurin)'
18be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
199f364a6fe1fedaecc652f111cd143ab2ee7ca825Simran Basiimport os, sys, re, traceback
20be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
21be630eb07ddb84310fcc580acffcaa362a2dc606mblighimport common
22be630eb07ddb84310fcc580acffcaa362a2dc606mblighfrom autotest_lib.cli import topic_common
239f364a6fe1fedaecc652f111cd143ab2ee7ca825Simran Basifrom autotest_lib.server import utils
24be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
25be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
26be630eb07ddb84310fcc580acffcaa362a2dc606mblighdef main():
27be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    """
28be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    The generic syntax is:
29be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    atest <topic> <action> <options>
30be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    atest-<topic> <action> <options>
31be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    atest --help
32be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    """
339f364a6fe1fedaecc652f111cd143ab2ee7ca825Simran Basi    utils.verify_not_root_user()
34be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    cli = os.path.basename(sys.argv[0])
35be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    syntax_obj = topic_common.atest()
36be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
37be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # Normalize the various --help, -h and help to -h
38be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    sys.argv = [re.sub('--help|help', '-h', arg) for arg in sys.argv]
39be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
40be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    match = re.search('^atest-(\w+)$', cli)
41be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    if match:
42be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        topic = match.group(1)
43be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    else:
44be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        if len(sys.argv) > 1:
45be630eb07ddb84310fcc580acffcaa362a2dc606mbligh            topic = sys.argv.pop(1)
46be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        else:
47be630eb07ddb84310fcc580acffcaa362a2dc606mbligh            syntax_obj.invalid_syntax('No topic argument')
48be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
49be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
50be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    if topic == '-h':
51be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        sys.argv.insert(1, '-h')
52be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        syntax_obj.parse()
53be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
54be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # The ignore flag should *only* be used by unittests.
55be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    ignore_site = '--ignore_site_file' in sys.argv
56be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    if ignore_site:
57be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        sys.argv.remove('--ignore_site_file')
58be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
59be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # Import the topic specific file
60be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    cli_dir = os.path.abspath(os.path.dirname(__file__))
61be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    if (not ignore_site and
62be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        os.path.exists(os.path.join(cli_dir, 'site_%s.py' % topic))):
63be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        topic = 'site_%s' % topic
64be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    elif not os.path.exists(os.path.join(cli_dir, '%s.py' % topic)):
65be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        syntax_obj.invalid_syntax('Invalid topic %s' % topic)
66be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    topic_module = common.setup_modules.import_module(topic,
67be630eb07ddb84310fcc580acffcaa362a2dc606mbligh                                                      'autotest_lib.cli')
68be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
69be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # If we have a syntax error now, it should
70be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # refer to the topic class.
715a496088c7daa10aad13580615bfd157c4cd8180mbligh    topic_class = getattr(topic_module, topic)
725a496088c7daa10aad13580615bfd157c4cd8180mbligh    topic_obj = topic_class()
73be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
74be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    if len(sys.argv) > 1:
75be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        action = sys.argv.pop(1)
76be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
77be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        if action == '-h':
78be630eb07ddb84310fcc580acffcaa362a2dc606mbligh            action = 'help'
79be630eb07ddb84310fcc580acffcaa362a2dc606mbligh            sys.argv.insert(1, '-h')
80be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    else:
815a496088c7daa10aad13580615bfd157c4cd8180mbligh        topic_obj.invalid_syntax('No action argument')
825a496088c7daa10aad13580615bfd157c4cd8180mbligh
835a496088c7daa10aad13580615bfd157c4cd8180mbligh    # Any backward compatibility changes?
845a496088c7daa10aad13580615bfd157c4cd8180mbligh    action = topic_obj.backward_compatibility(action, sys.argv)
85be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
86be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    # Instantiate a topic object
87be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    try:
885a496088c7daa10aad13580615bfd157c4cd8180mbligh        action_class = getattr(topic_module, topic + '_' + action)
89be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    except AttributeError:
905a496088c7daa10aad13580615bfd157c4cd8180mbligh        topic_obj.invalid_syntax('Invalid action %s' % action)
91be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
925a496088c7daa10aad13580615bfd157c4cd8180mbligh    action_obj = action_class()
93be630eb07ddb84310fcc580acffcaa362a2dc606mbligh
945a496088c7daa10aad13580615bfd157c4cd8180mbligh    action_obj.parse()
95be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    try:
96be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        try:
975a496088c7daa10aad13580615bfd157c4cd8180mbligh            results = action_obj.execute()
98be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        except topic_common.CliError:
99be630eb07ddb84310fcc580acffcaa362a2dc606mbligh            pass
100be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        except Exception, err:
101fb64e6a3a4403534ae1d136c72010036afebe8ffshoward            traceback.print_exc()
1025a496088c7daa10aad13580615bfd157c4cd8180mbligh            action_obj.generic_error("Unexpected exception: %s" % err)
103be630eb07ddb84310fcc580acffcaa362a2dc606mbligh        else:
104cae0da72467e9e3e2b45efb6a631b560e3459d9embligh            try:
1055a496088c7daa10aad13580615bfd157c4cd8180mbligh                action_obj.output(results)
106cae0da72467e9e3e2b45efb6a631b560e3459d9embligh            except Exception:
107cae0da72467e9e3e2b45efb6a631b560e3459d9embligh                traceback.print_exc()
108be630eb07ddb84310fcc580acffcaa362a2dc606mbligh    finally:
1095a496088c7daa10aad13580615bfd157c4cd8180mbligh        return action_obj.show_all_failures()
110