Brett /
SetDefaultProject(:source lang=python:)
import xmlrpclib import sys import os import pwd import getpass import getopt url = 'https://help.vlsci.unimelb.edu.au/accounts/xmlrpc/' username = pwd.getpwuid(os.getuid())[0] def usage(): print "Usage: set_default_project [-l] [-h] | <project id>" print " -h: Print help" print " -l: List projects user is a member of" def list_projects(username): server = xmlrpclib.Server(url) try: return_value, message = server.list_projects(username) except Exception, e: if hasattr(e, 'faultCode'): if e.faultCode == 1: print >> sys.stderr, "ERROR: Function not supported yet" sys.exit(-1) print >> sys.stderr, "ERROR: Can't contact the VLSCI accounts system. Sorry, but projects cannot be altered at present.\nPlease try again later" print >> sys.stderr, "ERROR: (e) sys.exit(-1) if return_value != 0: print >> sys.stderr, "ERROR: (message) sys.exit(-1) else: print >> sys.stdout, " (message) return return_value def set_default_project(username, password, project): server = xmlrpclib.Server(url)
try:
return_value, message = server.change_default_project(username, password, project)
except Exception, e:
if hasattr(e, 'faultCode'):
if e.faultCode == 81:
print >> sys.stderr, "ERROR: Incorrect password"
sys.exit(-1)
print >> sys.stderr, "ERROR: Can't contact the VLSCI accounts system. Sorry, but projects cannot be altered at present.\nPlease try again later"
print >> sys.stderr, "ERROR: (e)
sys.exit(-1)
if return_value != 0:
print >> sys.stderr, "ERROR: " + message
sys.exit(-1)
else:
print >> sys.stdout, "Default project successfully changed to " + project
return return_value
if __name__ == "__main__": try: opts, args = getopt.getopt(sys.argv[1:], 'hl') except getopt.GetoptError, err: print err usage() sys.exit(-1) opts = dict(opts) if '-l' in opts: return_value = list_projects(username) sys.exit(return_value) if '-h' in opts: usage() sys.exit(0) if len(args) != 1: usage() sys.exit(-1) project = args[0] password = getpass.getpass() return_value = set_default_project(username, password, project) sys.exit(return_value) (:sourceend:) |