Merge from trunk
[plcapi.git] / plcsh
1 #!/usr/bin/python
2 #
3 # Interactive shell for testing PLCAPI
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
7 #
8 # $Id$
9 #
10
11 import os
12 import sys
13 from socket import gethostname
14 from optparse import OptionParser
15 from getpass import getpass
16 from traceback import print_exc
17
18 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
19 from PLC.Shell import Shell
20
21 parser = OptionParser(add_help_option = False)
22 parser.add_option("-f", "--config", help = "PLC configuration file")
23 parser.add_option("-h", "--url", help = "API URL")
24 parser.add_option("-c", "--cacert", help = "API SSL certificate")
25 parser.add_option("-k", "--insecure", help = "Do not check SSL certificate")
26 parser.add_option("-m", "--method", help = "API authentication method")
27 parser.add_option("-s", "--session", help = "API session key")
28 parser.add_option("-u", "--user", help = "API user name")
29 parser.add_option("-p", "--password", help = "API password")
30 parser.add_option("-r", "--role", help = "API role")
31 parser.add_option("-x", "--xmlrpc", action = "store_true", default = False, help = "Use XML-RPC interface")
32 parser.add_option("--help", action = "help", help = "show this help message and exit")
33 (options, args) = parser.parse_args()
34
35 # If user is specified but password is not
36 if options.user is not None and options.password is None:
37     try:
38         options.password = getpass()
39     except (EOFError, KeyboardInterrupt):
40         print
41         sys.exit(0)
42
43 # Initialize a single global instance (scripts may re-initialize
44 # this instance and/or create additional instances).
45 try:
46     shell = Shell(globals = globals(),
47                   config = options.config,
48                   url = options.url, xmlrpc = options.xmlrpc, cacert = options.cacert,
49                   method = options.method, role = options.role,
50                   user = options.user, password = options.password,
51                   session = options.session)
52     # Register a few more globals for backward compatibility
53     auth = shell.auth
54     api = shell.api
55     config = shell.config
56 except Exception, err:
57     print_exc() 
58     print "Error:", err
59     print
60     parser.print_help()
61     sys.exit(1)
62
63 # If called by a script
64 if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
65     # Pop us off the argument stack
66     sys.argv.pop(0)
67     execfile(sys.argv[0])
68
69 # Otherwise, run an interactive shell environment
70 else:
71     if shell.server is None:
72         print "PlanetLab Central Direct API Access"
73         prompt = ""
74     elif shell.auth['AuthMethod'] == "anonymous":
75         prompt = "[anonymous]"
76         print "Connected anonymously"
77     elif shell.auth['AuthMethod'] == "session":
78         # XXX No way to tell node and user sessions apart from the
79         # client point of view.
80         prompt = "[%s]" % gethostname()
81         print "%s connected using session authentication" % gethostname()
82     else:
83         prompt = "[%s]" % shell.auth['Username']
84         print "%s connected using %s authentication" % \
85               (shell.auth['Username'], shell.auth['AuthMethod'])
86
87     # Readline and tab completion support
88     import atexit
89     import readline
90     import rlcompleter
91
92     print 'Type "system.listMethods()" or "help(method)" for more information.'
93     # Load command history
94     history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
95     try:
96         file(history_path, 'a').close()
97         readline.read_history_file(history_path)
98         atexit.register(readline.write_history_file, history_path)
99     except IOError:
100         pass
101
102     # Enable tab completion
103     readline.parse_and_bind("tab: complete")
104
105     try:
106         while True:
107             command = ""
108             while True:
109                 # Get line
110                 try:
111                     if command == "":
112                         sep = ">>> "
113                     else:
114                         sep = "... "
115                     line = raw_input(prompt + sep)
116                 # Ctrl-C
117                 except KeyboardInterrupt:
118                     command = ""
119                     print
120                     break
121
122                 # Build up multi-line command
123                 command += line
124
125                 # Blank line or first line does not end in :
126                 if line == "" or (command == line and line[-1] != ':'):
127                     break
128
129                 command += os.linesep
130
131             # Blank line
132             if command == "":
133                 continue
134             # Quit
135             elif command in ["q", "quit", "exit"]:
136                 break
137
138             try:
139                 try:
140                     # Try evaluating as an expression and printing the result
141                     result = eval(command)
142                     if result is not None:
143                         print result
144                 except SyntaxError:
145                     # Fall back to executing as a statement
146                     exec command
147             except Exception, err:
148                 print_exc()
149
150     except EOFError:
151         print
152         pass