X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FShell.py;h=25bb0d597a84c1d42bafff2dea07144f0ed5b9bf;hb=2d763c5797f31cc782ae63901b1dd3e48ba41173;hp=e0d7fbdfae0840ddc57adaf84b71105e1c518578;hpb=96d51f595475375c3265f09e5c7d90056fc3d6f9;p=plcapi.git diff --git a/PLC/Shell.py b/PLC/Shell.py index e0d7fbd..25bb0d5 100644 --- a/PLC/Shell.py +++ b/PLC/Shell.py @@ -1,15 +1,14 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # # Interactive shell for testing PLCAPI # # Mark Huang # Copyright (C) 2005 The Trustees of Princeton University # -# $Id: Shell.py,v 1.18 2006/12/15 18:36:16 mlhuang Exp $ -# +import os import pydoc -import xmlrpclib +import xmlrpc.client from PLC.API import PLCAPI from PLC.Parameter import Mixed @@ -39,8 +38,8 @@ class Callable: if self.auth and \ (not args or not isinstance(args[0], dict) or \ - (not args[0].has_key('AuthMethod') and \ - not args[0].has_key('session'))): + ('AuthMethod' not in args[0] and \ + 'session' not in args[0])): args = (self.auth,) + args if self.shell.multi: @@ -57,8 +56,12 @@ class Shell: config = None, # XML-RPC server url = None, xmlrpc = False, cacert = None, - # API authentication - method = None, role = None, user = None, password = None): + # API authentication method + method = None, + # Password authentication + role = None, user = None, password = None, + # Session authentication + session = None): """ Initialize a new shell instance. Re-initializes globals. """ @@ -80,39 +83,48 @@ class Shell: else: self.api = PLCAPI(config) self.config = self.api.config + self.url = None self.server = None - except Exception, err: + except Exception as err: # Try connecting to the API server via XML-RPC self.api = PLCAPI(None) - if config is None: - self.config = Config() - else: - try: + try: + if config is None: + self.config = Config() + else: self.config = Config(config) - except Exception, err: - # Try to continue if no configuration file is available - self.config = None - pass + except Exception as err: + # Try to continue if no configuration file is available + self.config = None if url is None: if self.config is None: - raise Exception, "Must specify API URL" + raise Exception("Must specify API URL") url = "https://" + self.config.PLC_API_HOST + \ ":" + str(self.config.PLC_API_PORT) + \ "/" + self.config.PLC_API_PATH + "/" - if cacert is None and self.config is not None: - cacert = self.config.PLC_API_CA_SSL_CRT + if cacert is None: + cacert = self.config.PLC_API_CA_SSL_CRT - self.server = xmlrpclib.ServerProxy(url, PyCurlTransport(url, cacert), allow_none = 1) + self.url = url + if cacert is not None: + self.server = xmlrpc.client.ServerProxy(url, PyCurlTransport(url, cacert), allow_none = 1) + else: + self.server = xmlrpc.client.ServerProxy(url, allow_none = 1) # Set up authentication structure - # Default is to use capability authentication + # Default is to use session or capability authentication if (method, user, password) == (None, None, None): - method = "capability" + if session is not None or os.path.exists("/etc/planetlab/session"): + method = "session" + if session is None: + session = "/etc/planetlab/session" + else: + method = "capability" if method == "capability": # Load defaults from configuration file if using capability @@ -129,12 +141,21 @@ class Shell: if role == "anonymous" or method == "anonymous": self.auth = {'AuthMethod': "anonymous"} + elif method == "session": + if session is None: + raise Exception("Must specify session") + + if os.path.exists(session): + with open(session) as feed: + session = feed.read() + + self.auth = {'AuthMethod': "session", 'session': session} else: if user is None: - raise Exception, "Must specify username" + raise Exception("Must specify username") if password is None: - raise Exception, "Must specify password" + raise Exception("Must specify password") self.auth = {'AuthMethod': method, 'Username': user, @@ -143,7 +164,7 @@ class Shell: if role is not None: self.auth['Role'] = role - for method in PLC.Methods.methods: + for method in PLC.API.PLCAPI.all_methods: api_function = self.api.callable(method) if self.server is None: @@ -158,7 +179,7 @@ class Shell: if api_function.accepts and \ (isinstance(api_function.accepts[0], Auth) or \ (isinstance(api_function.accepts[0], Mixed) and \ - filter(lambda param: isinstance(param, Auth), api_function.accepts[0]))): + [param for param in api_function.accepts[0] if isinstance(param, Auth)])): auth = self.auth else: auth = None @@ -213,7 +234,7 @@ class Shell: def begin(self): if self.calls: - raise Exception, "multicall already in progress" + raise Exception("multicall already in progress") self.multi = True @@ -224,11 +245,11 @@ class Shell: results = self.system.multicall(self.calls) for result in results: if type(result) == type({}): - raise xmlrpclib.Fault(result['faultCode'], result['faultString']) + raise xmlrpc.client.Fault(result['faultCode'], result['faultString']) elif type(result) == type([]): ret.append(result[0]) else: - raise ValueError, "unexpected type in multicall result" + raise ValueError("unexpected type in multicall result") else: ret = None