5bf747e76861c5b99be76f52eebe1f4786ff148d
[tests.git] / qaapi / qash
1 #!/usr/bin/python
2 #
3 # Interactive shell for using QA test suite
4
5
6
7 import os, sys
8 from optparse import OptionParser
9 from traceback import print_exc
10 from qa import utils
11 from qa.QAAPI import QAAPI
12 from qa.Test import Test
13
14 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
15
16 parser = OptionParser(add_help_option = False)
17 parser.add_option("-f", "--config", help = "configuration file")
18 parser.add_option("-l", "--log", help="enable logging")
19 parser.add_option("-v", "--verbose", help="be verbose")
20 parser.add_option("--help", action = "help", help = "show this help message and exit")
21 (options, args) = parser.parse_args()
22
23 # XX Load callable tests
24 try:
25     qaapi = QAAPI(globals(), 
26          config = options.config, 
27          logging = options.log,
28          verbose = options.verbose)
29     config = qaapi.config
30     plcapi = config.api
31     auth = config.auth
32
33 except Exception, err:
34     raise
35
36 # If called by a script
37 if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
38     # Pop us off the argument stack
39     sys.argv.pop(0)
40     execfile(sys.argv[0])
41
42 else:
43     prompt = "[QA]"
44     
45      # Readline and tab completion support
46     import atexit
47     import readline
48     import rlcompleter
49
50     print 'Type "help" for more information.'
51     # Load command history
52     history_path = os.path.join(os.environ["HOME"], ".qa_history")
53     try:
54         file(history_path, 'a').close()
55         readline.read_history_file(history_path)
56         atexit.register(readline.write_history_file, history_path)
57     except IOError:
58         pass
59
60     # Enable tab completion
61     readline.parse_and_bind("tab: complete")
62
63     try:
64         while True:
65             command = ""
66             while True:
67                 # Get line
68                 try:
69                     if command == "":
70                         sep = ">>> "
71                     else:
72                         sep = "... "
73                     line = raw_input(prompt + sep)
74                 # Ctrl-C
75                 except KeyboardInterrupt:
76                     command = ""
77                     print
78                     break
79
80                 # Build up multi-line command
81                 command += line
82
83                 # Blank line or first line does not end in :
84                 if line == "" or (command == line and line[-1] != ':'):
85                     break
86
87                 command += os.linesep
88
89             # Blank line
90             if command == "":
91                 continue
92             # Quit
93             elif command in ["q", "quit", "exit"]:
94                 break
95
96             try:
97                 try:
98                     # Try evaluating as an expression and printing the result
99                     result = eval(command)
100                     if result is not None:
101                         print result
102                 except SyntaxError:
103                     # Fall back to executing as a statement
104                     exec command
105             except Exception, err:
106                 print_exc()
107
108     except EOFError:
109         print
110         pass