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