def main(): username = password = None parameters = {} begin = 0 rawargs = sys.argv[1:] for i,o in enumerate(rawargs): if o == '-u': username = rawargs[i+1] begin = i+2 elif o == '-p': password = rawargs[i+1] begin = i+2 elif o == '-h': usage() sys.exit(0) elif o == '-v': print "CLAM Client version " + str(VERSION) sys.exit(0) elif o[0] == '-' and len(o) > 1 and o[1] != '-': usage() print "ERROR: Unknown option: ", o sys.exit(2) elif o[:2] == '--': if len(rawargs) > i + 1: parameters[o[2:]] = rawargs[i+1] begin = i+2 else: parameters[o[2:]] = True begin = i+1 if len(rawargs) > begin: url = rawargs[begin] else: usage() sys.exit(2) print "ERROR: URL expected" if url[:4] != 'http': print "ERROR: URL expected" usage() sys.exit(2) client = CLAMClient(url, username,password) if len(rawargs) > begin + 1: command = rawargs[begin+1] args = rawargs[begin+2:] else: command = 'info' args = [] if command == 'info' and len(args) > 1: command = 'get' try: data = None if command in ['info','index','projects','inputtemplates','parameters','profiles']: data = client.index() elif command in ['get','input','output','status','inputtemplate']: if len(args) != 1: print >>sys.stderr, "Expected project ID" sys.exit(2) data = client.get(args[0]) elif command == 'create': if len(args) != 1: print >>sys.stderr, "Expected project ID" sys.exit(2) client.create(args[0]) elif command == 'delete' or command == 'abort': if len(args) != 1: print >>sys.stderr, "Expected project ID" sys.exit(2) client.delete(args[0]) #elif command == 'reset': # if len(args) != 1: # print >>sys.stderr, "Expected project ID" # sys.exit(2) # client.reset(args[0]) elif command == 'xml': if len(args) ==1: data = client.get(args[0]) else: data = client.index() elif command == 'upload': if len(args) < 3: print >>sys.stderr, "Expected: project inputtemplate file " sys.exit(2) project = args[0] data = client.get(project) try: inputtemplate = data.inputtemplate(args[1]) except: print >>sys.stderr, "No such input template: " + args[1] sys.exit(2) filepath = args[2] if not os.path.isfile(filepath): print >>sys.stderr, "File does not exist: " + filepath sys.exit(2) client.upload(project,inputtemplate, filepath, **parameters) elif command == 'download': if len(args) < 2: print >>sys.stderr, "Expected: project file " sys.exit(2) project = args[0] filepath = args[1] if len(args) == 3: targetfile = args[2] else: targetfile = os.path.basename(filepath) client.download(project, filepath, targetfile) else: print >>sys.stderr,"Unknown command: " + command sys.exit(1) if data: if command == 'xml': print data.xml if command in ['info','get']: print "General Information" print "\tSystem ID: " + data.system_id print "\tSystem Name: " + data.system_name print "\tSystem URL: " + data.baseurl if username: print "\tUser: "******"\tProject: " + data.project if command in ['info','projects','index']: print "Projects" for project in data.projects: print "\t" + project if command in ['get','status']: print "Status Information" print "\tStatus: " + str(data.status) #TODO: nicer messages print "\tStatus Message: " + data.statusmessage print "\tCompletion: " + str(data.completion) + "%" if command in ['info','profiles']: print "Profiles:" #TODO: Implement for i, profile in enumerate(data.profiles): print "\tProfile " + str(i+1) print "\t Input" for template in profile.input: print "\t\t" + template.id + " - " + template.label print "\t Output" for template in profile.output: if isinstance(template, ParameterCondition): for t in template.allpossibilities(): print "\t\t(CONDITIONAL!) " + t.id + " - " + t.label else: print "\t\t" + template.id + " - " + template.label if command == 'inputtemplates': print "Input templates:" for template in data.input: print "\t\t" + template.id + " - " + template.label if command == 'inputtemplate': try: inputtemplate = data.inputtemplate(args[0]) except: print >>sys.stderr, "No such inputtemplate" sys.exit(1) print "Inputtemplate parameters:" for parameter in inputtemplate.parameters: print "\t\t" + str(parameter) #VERIFY: unicode support? print "Inputtemplate converters:" for c in inputtemplate.converters: print "\t\t" + c.id + " - " + c.label #VERIFY: unicode support? if command in ['info','parameters']: print "Global Parameters:" for group, parameters in data.parameters: print "\t" + group for parameter in parameters: print "\t\t" + str(parameter) #VERIFY: unicode support? if command in ['get','input'] and data.input: print "Input files:" for f in data.input: print "\t" + f.filename + "\t" + str(f), if f.metadata and f.metadata.inputtemplate: print "\t" + f.metadata.inputtemplate else: print if command in ['get','output'] and data.output: print "Output files:" for f in data.output: print "\t" + f.filename + "\t" + str(f), if f.metadata and f.metadata.provenance and f.metadata.provenance.outputtemplate_id: print "\t" + f.metadata.provenance.outputtemplate_id else: print except NotFound: print >>sys.stderr, "Not Found (404)" except PermissionDenied: print >>sys.stderr, "Permission Denied (403)" except ServerError: print >>sys.stderr, "Server Error! (500)" except AuthRequired: print >>sys.stderr, "Authorization required (401)"
def main(): username = password = None parameters = {} begin = 0 rawargs = sys.argv[1:] for i, o in enumerate(rawargs): if o == '-u': username = rawargs[i + 1] begin = i + 2 elif o == '-p': password = rawargs[i + 1] begin = i + 2 elif o == '-h': usage() sys.exit(0) elif o == '-v': print("CLAM Client version " + str(VERSION), file=sys.stderr) sys.exit(0) elif o[0] == '-' and len(o) > 1 and o[1] != '-': usage() print("ERROR: Unknown option: ", o, file=sys.stderr) sys.exit(2) elif o[:2] == '--': if len(rawargs) > i + 1: parameters[o[2:]] = rawargs[i + 1] begin = i + 2 else: parameters[o[2:]] = True begin = i + 1 if len(rawargs) > begin: url = rawargs[begin] else: usage() sys.exit(2) print("ERROR: URL expected", file=sys.stderr) if url[:4] != 'http': print("ERROR: URL expected", file=sys.stderr) usage() sys.exit(2) client = CLAMClient(url, username, password) if len(rawargs) > begin + 1: command = rawargs[begin + 1] args = rawargs[begin + 2:] else: command = 'info' args = [] if command == 'info' and len(args) > 1: command = 'get' try: data = None if command in [ 'info', 'index', 'projects', 'inputtemplates', 'parameters', 'profiles' ]: data = client.index() elif command in ['get', 'input', 'output', 'status', 'inputtemplate']: if len(args) != 1: print("Expected project ID", file=sys.stderr) sys.exit(2) data = client.get(args[0]) elif command == 'create': if len(args) != 1: print("Expected project ID", file=sys.stderr) sys.exit(2) client.create(args[0]) elif command == 'delete' or command == 'abort': if len(args) != 1: print("Expected project ID", file=sys.stderr) sys.exit(2) client.delete(args[0]) #elif command == 'reset': # if len(args) != 1: # print("Expected project ID" # sys.exit(2) # client.reset(args[0]) elif command == 'start': if len(args) < 1: print("Expected project ID", file=sys.stderr) sys.exit(2) client.start(args[0]) elif command == 'xml': if len(args) == 1: data = client.get(args[0]) else: data = client.index() elif command == 'upload': if len(args) < 3: print("Expected: project inputtemplate file ", file=sys.stderr) sys.exit(2) project = args[0] data = client.get(project) try: inputtemplate = data.inputtemplate(args[1]) except: print("No such input template: " + args[1], file=sys.stderr) sys.exit(2) filepath = args[2] if not os.path.isfile(filepath): print("File does not exist: " + filepath, file=sys.stderr) sys.exit(2) client.upload(project, inputtemplate, filepath, **parameters) elif command == 'download': if len(args) < 2: print("Expected: project file ", file=sys.stderr) sys.exit(2) project = args[0] filepath = args[1] if len(args) == 3: targetfile = args[2] else: targetfile = os.path.basename(filepath) client.download(project, filepath, targetfile) else: print("Unknown command: " + command, file=sys.stderr) sys.exit(1) if data: if command == 'xml': print(data.xml) if command in ['info', 'get']: print("General Information") print("\tSystem ID: " + data.system_id) print("\tSystem Name: " + data.system_name) print("\tSystem URL: " + data.baseurl) if username: print("\tUser: "******"\tProject: " + data.project) if command in ['info', 'projects', 'index']: print("Projects") for project in data.projects: print("\t" + project) if command in ['get', 'status']: print("Status Information") print("\tStatus: " + str(data.status)) #TODO: nicer messages print("\tStatus Message: " + data.statusmessage) print("\tCompletion: " + str(data.completion) + "%") if command in ['info', 'profiles']: print("Profiles:") #TODO: Implement for i, profile in enumerate(data.profiles): print("\tProfile " + str(i + 1)) print("\t Input") for template in profile.input: print("\t\t" + template.id + " - " + template.label) print("\t Output") for template in profile.output: if isinstance(template, ParameterCondition): for t in template.allpossibilities(): print("\t\t(CONDITIONAL!) " + t.id + " - " + t.label) else: print("\t\t" + template.id + " - " + template.label) if command == 'inputtemplates': print("Input templates:") for template in data.input: print("\t\t" + template.id + " - " + template.label) if command == 'inputtemplate': try: inputtemplate = data.inputtemplate(args[0]) except: print("No such inputtemplate", file=sys.stderr) sys.exit(1) print("Inputtemplate parameters:") for parameter in inputtemplate.parameters: print("\t\t" + str(parameter)) #VERIFY: unicode support? print("Inputtemplate converters:") for c in inputtemplate.converters: print("\t\t" + c.id + " - " + c.label) if command in ['info', 'parameters']: print("Global Parameters:") for group, parameters in data.parameters: print("\t" + group) for parameter in parameters: print("\t\t" + str(parameter)) #VERIFY: unicode support? if command in ['get', 'input'] and data.input: print("Input files:") for f in data.input: print("\t" + f.filename + "\t" + str(f), end="") if f.metadata and f.metadata.inputtemplate: print("\t" + f.metadata.inputtemplate) else: print if command in ['get', 'output'] and data.output: print("Output files:") for f in data.output: print("\t" + f.filename + "\t" + str(f), end="") if f.metadata and f.metadata.provenance and f.metadata.provenance.outputtemplate_id: print("\t" + f.metadata.provenance.outputtemplate_id) else: print except NotFound: print("Not Found (404)", file=sys.stderr) except PermissionDenied: print("Permission Denied (403)", file=sys.stderr) except ServerError: print("Server Error! (500)", file=sys.stderr) except AuthRequired: print("Authorization required (401)", file=sys.stderr)
else: parameters[o[2:]] = True begin = i+1 if len(rawargs) > begin: url = rawargs[begin] else: usage() sys.exit(2) print "ERROR: URL expected" if url[:4] != 'http': print "ERROR: URL expected" usage() sys.exit(2) client = CLAMClient(url, username,password) if len(rawargs) > begin + 1: command = rawargs[begin+1] args = rawargs[begin+2:] else: command = 'info' args = [] if command == 'info' and len(args) > 1: command = 'get' try: data = None if command in ['info','index','projects','inputtemplates','parameters','profiles']: