Back to index

nordugrid-arc-nox  1.1.0~rc6
ahash_client.py
Go to the documentation of this file.
00001 import arc, sys, os
00002 from arcom.xmltree import XMLTree
00003 from storage.client import AHashClient
00004 print ":".join(sys.argv)
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     ahash_url = os.environ['ARC_AHASH_URL']
00013     # print '- The URL of the A-Hash:', ahash_url
00014 except:
00015     ahash_url = 'http://localhost:60000/AHash'
00016     print '- ARC_AHASH_URL environment variable not found, using', ahash_url
00017 ssl_config = {}
00018 if ahash_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         ssl_config['ca_dir'] = os.environ['ARC_CA_DIR']
00023         # print '- The key file:', ssl_config['key_file']
00024         # print '- The cert file:', ssl_config['cert_file']
00025     except:
00026         ssl_config = {}
00027         print '- ARC_KEY_FILE or ARC_CERT_FILE environment variable not found, SSL disabled'
00028 ahash = AHashClient(ahash_url, print_xml, ssl_config = ssl_config)    
00029 if len(args) == 0 or args[0] not in ['get', 'change', 'changeIf']:
00030     print 'Supported methods: get, change, changeIf'
00031 else:
00032     command = args.pop(0)
00033     if command == 'get':
00034         if len(args) < 1:
00035             print 'Usage: get <ID> [<ID> ...] neededMetadata [<section> [<property>] ...] '
00036         else:
00037             IDs = []
00038             while len(args) > 0 and args[0] != 'neededMetadata':
00039                 IDs.append(args.pop(0))
00040             neededMetadata = []
00041             if len(args) > 0:
00042                 args.pop(0)
00043             while len(args) > 0:
00044                 section = args.pop(0)
00045                 if len(args) > 0:
00046                     property = args.pop(0)
00047                 else:
00048                     property = ''
00049                 neededMetadata.append((section, property))
00050             print 'get', IDs, neededMetadata
00051             print ahash.get(IDs, neededMetadata)
00052     elif command == 'change':
00053         if len(args) < 5:
00054             print 'Usage: change <ID> <changeType> <section> <property> <value>'
00055             print '   changeType: set | unset | delete'
00056         else:
00057             changes = {'0' : args + [{}]}
00058             print 'change', changes
00059             print ahash.change(changes)
00060     elif command == 'changeIf':
00061         if len(args) < 9:
00062             print 'Usage: changeIf <ID> <changeType> <section> <property> <value> <conditionType> <section> <property> <value>'
00063             print '   changeType: set | unset | delete'
00064             print '   conditionType: is | isnot | isset | unset'
00065         else:
00066             changes = {'0' : args[0:5] + [{'0' : args[5:9]}]}
00067             print 'change', changes
00068             print ahash.change(changes)