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