Back to index

nordugrid-arc-nox  1.1.0~rc6
shepherd_client.py
Go to the documentation of this file.
00001 import arc, sys, time, StringIO, os
00002 from arcom.xmltree import XMLTree
00003 from storage.client import ShepherdClient, ByteIOClient
00004 from storage.common import create_checksum
00005 args = sys.argv[1:]
00006 if len(args) > 0 and args[0] == '-x':
00007     args.pop(0)
00008     print_xml = True
00009 else:
00010     print_xml = False
00011 try:
00012     shepherd_url = os.environ['ARC_SHEPHERD_URL']
00013     # print '- The URL of the Shepherd:', shepherd_url
00014 except:
00015     shepherd_url = 'http://localhost:60000/Shepherd'
00016     print '- ARC_SHEPHERD_URL environment variable not found, using', shepherd_url
00017 ssl_config = {}
00018 if shepherd_url.startswith('https'):
00019     try:
00020         ssl_config['key_file'] = os.environ['ARC_KEY_FILE']
00021         ssl_config['cert_file'] = os.environ['ARC_CERT_FILE']
00022         # print '- The key file:', ssl_config['key_file']
00023         # print '- The cert file:', ssl_config['cert_file']
00024     except:
00025         ssl_config = {}
00026         print '- ARC_KEY_FILE or ARC_CERT_FILE environment variable not found, SSL disabled'
00027 shepherd = ShepherdClient(shepherd_url, print_xml, ssl_config = ssl_config)    
00028 if len(args) == 0 or args[0] not in ['get', 'put', 'stat', 'delete', 'reporting']:
00029     print 'Supported methods: get put stat delete reporting'
00030 else:
00031     command = args.pop(0)
00032     if command == 'get':
00033         if len(args) < 1:
00034             print 'Usage: get <referenceID>'
00035         else:
00036             request = {'0' : [('referenceID', args[0]), ('protocol', 'byteio')]} 
00037             print 'get', request
00038             response = dict(shepherd.get(request)['0'])
00039             if response.has_key('error'):
00040                 print 'ERROR', response['error']
00041             else:
00042                 turl = response['TURL']
00043                 print 'Downloading:', turl
00044                 print 'Checksum is', response['checksum']
00045                 print '***'
00046                 data = ByteIOClient(turl).read()
00047                 print data
00048                 print '***'
00049                 print 'actual checksum is', create_checksum(StringIO.StringIO(data), response['checksumType'])
00050     elif command == 'put':
00051         if len(args) < 1:
00052             print 'Usage: put <data>'
00053         else:
00054             data = ' '.join(args)
00055             checksum = create_checksum(StringIO.StringIO(data), 'md5')
00056             request = {'0' : [('size', str(len(data))), ('protocol', 'byteio'), ('checksumType', 'md5'), ('checksum', checksum)]} 
00057             print 'put', request
00058             response = dict(shepherd.put(request)['0'])
00059             if response.has_key('error'):
00060                 print 'ERROR', response['error']
00061             else:
00062                 print 'referenceID:', response['referenceID']
00063                 turl = response['TURL']
00064                 print 'Uploading:', turl
00065                 ByteIOClient(turl).write(data)
00066     elif command == 'stat':
00067         if len(args) < 1:
00068             print 'Usage: stat <referenceID>'
00069         else:
00070             request = {'0' : args[0]}
00071             print 'stat', request
00072             print shepherd.stat(request)
00073     elif command == 'delete':
00074         if len(args) < 1:
00075             print 'Usage: delete <referenceID>'
00076         else:
00077             request = {'0' : args[0]}
00078             print 'delete', request
00079             print shepherd.delete(request)
00080     elif command == 'reporting':
00081         if len(args) < 1 or args[0] not in ['on', 'off']:
00082             print 'Usage: reporting on|off'
00083         else:
00084             doReporting = args[0] == 'on'
00085             print 'toggleReport', doReporting
00086             print shepherd.toggleReport(doReporting)