def action(parser, args): group = optparse.OptionGroup(parser, 'Delete options') group.add_option('--version-id', dest='version_id', action='store', default=None, help='delete given manifest version id') parser.add_option_group(group) parser.usage = USAGE (options, args) = parser.parse_args(args) cfg_file_path = validate_path(options.cfg_file) config = Config(cfg_file_path, 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 not args or len(args) > 2: parser.print_usage() return ERROR_CODE if len(args) != 2 and options.version_id: print >> sys.stderr, 'ERROR: version id allowed only if manifest ' \ 'specified' return ERROR_CODE if len(args) == 1: client.delete_container(args[0]) typ, name = 'Container', args[0] elif len(args) == 2: client.delete_manifest(args[0], args[1], version_id=options.version_id) typ, name = 'Manifest', '%s/%s' % (args[0], args[1]) if options.version_id: name += ' version id %s' % options.version_id print >> sys.stdout, '%s was successfully deleted' % name return SUCCESS_CODE
def action(parser, args): parser.usage = USAGE (options, args) = parser.parse_args(args) 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 not args: parser.print_usage() return ERROR_CODE elif len(args) == 1: _junk, _junk, _junk, versioning = client.get_versioning(args[0]) print >> sys.stdout, 'Container %s versioning is %s' % \ (args[0], versioning) elif len(args) == 2: if args[1] not in VERSIONING_VALUES: print >> sys.stderr, 'ERROR: Invalid versioning status: %s' % \ args[1] return ERROR_CODE client.set_versioning(args[0], args[1])[0] return SUCCESS_CODE
def action(parser, args): group = optparse.OptionGroup(parser, 'Upload options') group.add_option('--only-placeholder', dest='only_placeholder', help='upload only manifest placeholder', default=False, action='store_true') group.add_option('--only-manifest', dest='only_manifest', default=False, help='upload only manifest', action='store_true') group.add_option('--only-chunk', dest='only_chunk', default=False, help='upload only chunk', action='store_true') group.add_option('--chunk', dest='chunk', action='store', help='path to chunk file') group.add_option('--session-id', dest='session_id', action='store', help='manifest session ID') group.add_option('--session-timestamp', dest='session_timestamp', action='store', help='manifest session timestamp') group.add_option('--chunk-size', dest='chunk_size', type='int', action='store', help='chunk size', default=MiB) group.add_option('-w', '--workers', dest='workers', type='int', action='store', default=5, help='number of workers threads') group.add_option('-q', '--quite', dest='quite', action='store_true', default=False, help='hide progress bar') parser.add_option_group(group) parser.usage = USAGE (options, args) = parser.parse_args(args) 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) conn, path, req_url, req_auth_token = client.validate_conn() # PUT account if not args: client.put_account(http_conn=conn, req_url=req_url, req_auth_token=req_auth_token) return SUCCESS_CODE headers = {} # PUT container if len(args) == 1: client.put_container(args[0], headers=headers, http_conn=conn, req_url=req_url, req_auth_token=req_auth_token) return SUCCESS_CODE container_name, object_name = args[0], args[1] try: object_name = validate_obj_name(object_name) except ValueError: print >> sys.stderr, 'ERROR: Invalid object name: %s' % args[1] return ERROR_CODE # PUT placeholder if options.only_placeholder: session_id, session_timestamp =\ client.put_placeholder( container_name, object_name, http_conn=conn, req_url=req_url, req_auth_token=req_auth_token) print >> sys.stdout, 'Session id: %s\nSession timestamp: %s' % \ (session_id, session_timestamp) return SUCCESS_CODE filepath = validate_path(args[1]) if not os.path.exists(filepath): print >> sys.stderr, 'ERROR: File %s does not exists' % filepath return ERROR_CODE for isdir, path, name in filepath_generator(filepath, object_name): if isdir: client.put_directory(container_name, name + '/') continue kwargs = {'chunk_size': options.chunk_size, 'workers': options.workers} info = os.stat(path) bar = ProgressBar(max_value=info.st_size, title=name, quite=options.quite) bar.start() kwargs['callback'] = bar.callback kwargs['error_callback'] = bar.clear try: with open(path, 'rb') as fp: client.put_object(container_name, name, fp, headers=headers, http_conn=conn, req_url=req_url, req_auth_token=req_auth_token, **kwargs) except Exception, e: bar.clear() raise e bar.finish()
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