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.
17 __author__
= 'jmeurin@google.com (Jean-Marc Eurin)'
19 import os
, sys
, optparse
, re
, traceback
22 from autotest_lib
.cli
import topic_common
27 The generic syntax is:
28 atest <topic> <action> <options>
29 atest-<topic> <action> <options>
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
)
40 topic
= match
.group(1)
43 topic
= sys
.argv
.pop(1)
45 syntax_obj
.invalid_syntax('No topic argument')
49 sys
.argv
.insert(1, '-h')
52 # The ignore flag should *only* be used by unittests.
53 ignore_site
= '--ignore_site_file' in sys
.argv
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
,
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()
73 action
= sys
.argv
.pop(1)
77 sys
.argv
.insert(1, '-h')
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
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()
95 results
= action_obj
.execute()
96 except topic_common
.CliError
:
98 except Exception, err
:
100 action_obj
.generic_error("Unexpected exception: %s" % err
)
103 action_obj
.output(results
)
105 traceback
.print_exc()
107 return action_obj
.show_all_failures()