3 # Interactive shell for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
8 # $Id: plcsh,v 1.2 2007/01/11 05:28:49 mlhuang Exp $
13 from optparse import OptionParser
14 from traceback import print_exc
16 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
17 from PLC.Shell import Shell
19 parser = OptionParser(add_help_option = False)
20 parser.add_option("-f", "--config", help = "PLC configuration file")
21 parser.add_option("-h", "--url", help = "API URL")
22 parser.add_option("-c", "--cacert", help = "API SSL certificate")
23 parser.add_option("-m", "--method", help = "API authentication method")
24 parser.add_option("-u", "--user", help = "API user name")
25 parser.add_option("-p", "--password", help = "API password")
26 parser.add_option("-r", "--role", help = "API role")
27 parser.add_option("-x", "--xmlrpc", action = "store_true", default = False, help = "Use XML-RPC interface")
28 parser.add_option("--help", action = "help", help = "show this help message and exit")
29 (options, args) = parser.parse_args()
31 # If user is specified but password is not
32 if options.user is not None and options.password is None:
34 options.password = getpass.getpass()
35 except (EOFError, KeyboardInterrupt):
39 # Initialize a single global instance (scripts may re-initialize
40 # this instance and/or create additional instances).
42 shell = Shell(globals = globals(),
43 config = options.config,
44 url = options.url, xmlrpc = options.xmlrpc, cacert = options.cacert,
45 method = options.method, role = options.role,
46 user = options.user, password = options.password)
47 # Register a few more globals for backward compatibility
51 except Exception, err:
57 # If called by a script
58 if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
59 # Pop us off the argument stack
63 # Otherwise, run an interactive shell environment
65 if shell.server is None:
66 print "PlanetLab Central Direct API Access"
68 elif shell.auth['AuthMethod'] == "anonymous":
69 prompt = "[anonymous]"
70 print "Connected anonymously"
72 prompt = "[%s]" % shell.auth['Username']
73 print "%s connected using %s authentication" % \
74 (shell.auth['Username'], shell.auth['AuthMethod'])
76 # Readline and tab completion support
81 print 'Type "system.listMethods()" or "help(method)" for more information.'
82 # Load command history
83 history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
85 file(history_path, 'a').close()
86 readline.read_history_file(history_path)
87 atexit.register(readline.write_history_file, history_path)
91 # Enable tab completion
92 readline.parse_and_bind("tab: complete")
104 line = raw_input(prompt + sep)
106 except KeyboardInterrupt:
111 # Build up multi-line command
114 # Blank line or first line does not end in :
115 if line == "" or (command == line and line[-1] != ':'):
118 command += os.linesep
124 elif command in ["q", "quit", "exit"]:
129 # Try evaluating as an expression and printing the result
130 result = eval(command)
131 if result is not None:
134 # Fall back to executing as a statement
136 except Exception, err: