def action(parser, args): group = optparse.OptionGroup(parser, 'Download options') group.add_option('--version-id', dest='version_id', action='store', default=None, help='download object given version id') group.add_option('-q', '--quite', dest='quite', action='store_true', default=False, help='hide progress bar') group.add_option('--only-manifest', dest='only_manifest', default=False, action='store_true', help='download only manifest') group.add_option('--only-chunk', dest='only_chunk', default=False, action='store_true', help='download only given chunk') parser.add_option_group(group) parser.usage = USAGE (options, args) = parser.parse_args(args) if not args or len(args) < 2: print >> sys.stderr, 'ERROR: download allowed only for given object ' \ 'or chunk.' return ERROR_CODE if options.only_manifest and options.only_chunk: print >> sys.stderr, 'ERROR: --only-manifest and --only-chunk ' \ 'options not allowed together.' return ERROR_CODE if options.only_chunk and len(args) != 3: print >> sys.stderr, 'ERROR: for --only-chunk required chunk.' return ERROR_CODE if not options.only_chunk and len(args) >= 3: print >> sys.stderr, 'ERROR: chunk allowed if --only-chunk presented' return ERROR_CODE config = Config(options.cfg_file, options) client = NSclient( auth_url=config.auth_url, auth_version=config.auth_version, user=config.user, key=config.key, proxy_host=config.proxy_host, proxy_port=config.proxy_port, proxy_user=config.proxy_user, proxy_pass=config.proxy_pass, debug=False) if options.only_manifest: manifest = client.get_manifest(*args, version_id=options.version_id)[2] print >> sys.stdout, manifest return SUCCESS_CODE elif options.only_chunk: resp = client.get_chunk(args[0], args[1], args[2])[2] data_source = iter(lambda: resp.read(config.network_chunk_size), '') filename = args[2] typ = 'Chunk' title = '%s/%s' % (args[1], args[2]) etag = args[2] hash = sha256() content_length = resp.length content_type = None else: status, resp_headers, resp = \ client.get_object(args[0], args[1], version_id=options.version_id) data_source = resp filename = args[1] typ = 'Object' title = args[1] etag = resp.etag hash = md5() content_length = resp.content_length content_type = resp_headers.get('content-type') if content_type == 'text/directory': if not os.path.isdir(filename): mkdirs(filename) return SUCCESS_CODE dirpath = os.path.dirname(filename) if dirpath and not os.path.isdir(dirpath): mkdirs(filename) if os.path.isdir(filename): return SUCCESS_CODE bar = ProgressBar(content_length, title=title, quite=options.quite) bar.start() with open(filename, 'wb') as fdp: for chunk in data_source: fdp.write(chunk) if bar: bar.value += len(chunk) hash.update(chunk) bar.finish() if etag != hash.hexdigest(): print >> sys.stderr, 'ERROR: %s %s download failed, hashes are '\ 'not equals' % (typ, title) return ERROR_CODE return SUCCESS_CODE
def action(parser, args): group = optparse.OptionGroup(parser, 'List options') group.add_option('-f', '--full', dest='full', default=False, action='store_true', help='full listing of objects') group.add_option('-l', '--limit', dest='limit', default=None, action='store', help='limit for items to show') group.add_option('-p', '--prefix', dest='prefix', default=None, action='store', help='shows only list items beginning ' 'with that prefix') group.add_option('-d', '--delimiter', dest='delimiter', default=None, action='store', help='rolls up items with the given ' 'delimiter') group.add_option('-m', '--marker', dest='marker', default=None, action='store', help='rolls up items with the given ' 'marker') group.add_option('--end-marker', dest='end_marker', default=None, action='store', help='rolls up items which less then ' 'the given marker') group.add_option('--versions', dest='versions', action='store_true', default=False, help='shows only list items having ' 'version_id (for container listings only)') group.add_option('--vmarker', dest='vmarker', default=None, action='store', help='rolls up items with the given ' 'vmarker for version id (for container listings only)') group.add_option('--end-vmarker', dest='end_vmarker', default=None, action='store', help='rolls up items which version id ' 'less then the given vmarker (for container listings ' 'only)') group.add_option('--version-id', dest='version_id', action='store', default=None, help='list chunks of given version id') parser.add_option_group(group) parser.usage = USAGE (options, args) = parser.parse_args(args) opts = [options.limit, options.prefix, options.delimiter, options.marker, options.end_marker, options.versions, options.vmarker, options.end_vmarker] if len(args) >= 3 and any(opts): print >> sys.stderr, 'ERROR: options not allowed for manifest' return ERROR_CODE if len(args) != 1 and any(opts[:-3]): print >> sys.stderr, 'ERROR: version options allowed only for ' \ 'container' return ERROR_CODE if any(opts[-2:]) and not options.versions: print >> sys.stderr, 'ERROR: --vmarker and --end-vmarker allowed ' \ 'only with --versions' return ERROR_CODE config = Config(options.cfg_file, options) client = NSclient( auth_url=config.auth_url, auth_version=config.auth_version, user=config.user, key=config.key, proxy_host=config.proxy_host, proxy_port=config.proxy_port, proxy_user=config.proxy_user, proxy_pass=config.proxy_pass, debug=False) marker = '' kwargs = {} if len(args) <= 1: kwargs.update({ 'marker': options.marker, 'end_marker': options.end_marker, 'limit': options.limit, 'prefix': options.prefix, 'delimiter': options.delimiter, 'full_listing': options.full}) if options.versions: kwargs.update({'vmarker': options.vmarker, 'end_vmarker': options.end_vmarker}) out = [] if len(args) == 0: containers = client.get_account(**kwargs)[2] for container in containers: out.append('%(name)s' % container) elif len(args) == 1: if options.versions: func = client.get_versions_list else: func = client.get_container manifests = func(args[0], **kwargs)[2] for manifest in manifests: s = manifest.get('name', manifest.get('subdir')) if 'version_id' in manifest: s += ' ' + manifest['version_id'] if manifest.get('deleted'): s += ' deleted' out.append(s) else: kwargs['version_id'] = options.version_id manifest = client.get_manifest(args[0], args[1], **kwargs)[2] chunk_size = manifest.get('chunk_size') for chunk in manifest.get('chunks', []): hash, size = chunk['hash'], chunk.get('size', chunk_size) out.append('%s %d' % (hash, size)) if out: print >> sys.stdout, '\n'.join(out) return SUCCESS_CODE