X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/78700dbcc4f5d34f0a4cfee5c040e716d684ed62..d22b2efd246ec82a5888d500bcbc7669654b842d:/sdk/python/arvados/commands/put.py?ds=inline diff --git a/sdk/python/arvados/commands/put.py b/sdk/python/arvados/commands/put.py index 95ba172801..7ca6e7ca23 100644 --- a/sdk/python/arvados/commands/put.py +++ b/sdk/python/arvados/commands/put.py @@ -5,6 +5,7 @@ import argparse import arvados +import arvados.collection import base64 import datetime import errno @@ -110,6 +111,13 @@ Print the portable data hash instead of the Arvados UUID for the collection created by the upload. """) +upload_opts.add_argument('--replication', type=int, metavar='N', default=None, + help=""" +Set the replication level for the new collection: how many different +physical storage devices (e.g., disks) should have a copy of each data +block. Default is to use the server-provided default (if any) or 2. +""") + run_opts = argparse.ArgumentParser(add_help=False) run_opts.add_argument('--project-uuid', metavar='UUID', help=""" @@ -159,7 +167,9 @@ def parse_arguments(arguments): args = arg_parser.parse_args(arguments) if len(args.paths) == 0: - args.paths += ['/dev/stdin'] + args.paths = ['-'] + + args.paths = map(lambda x: "-" if x == "/dev/stdin" else x, args.paths) if len(args.paths) != 1 or os.path.isdir(args.paths[0]): if args.filename: @@ -174,9 +184,9 @@ def parse_arguments(arguments): args.progress = True if args.paths == ['-']: - args.paths = ['/dev/stdin'] + args.resume = False if not args.filename: - args.filename = '-' + args.filename = 'stdin' return args @@ -253,24 +263,23 @@ class ArvPutCollectionWriter(arvados.ResumableCollectionWriter): STATE_PROPS = (arvados.ResumableCollectionWriter.STATE_PROPS + ['bytes_written', '_seen_inputs']) - def __init__(self, cache=None, reporter=None, bytes_expected=None, - api_client=None, num_retries=0): + def __init__(self, cache=None, reporter=None, bytes_expected=None, **kwargs): self.bytes_written = 0 self._seen_inputs = [] self.cache = cache self.reporter = reporter self.bytes_expected = bytes_expected - super(ArvPutCollectionWriter, self).__init__( - api_client, num_retries=num_retries) + super(ArvPutCollectionWriter, self).__init__(**kwargs) @classmethod def from_cache(cls, cache, reporter=None, bytes_expected=None, - num_retries=0): + num_retries=0, replication=0): try: state = cache.load() state['_data_buffer'] = [base64.decodestring(state['_data_buffer'])] writer = cls.from_state(state, cache, reporter, bytes_expected, - num_retries=num_retries) + num_retries=num_retries, + replication=replication) except (TypeError, ValueError, arvados.errors.StaleWriterStateError) as error: return cls(cache, reporter, bytes_expected, num_retries=num_retries) @@ -402,6 +411,19 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr): print >>stderr, error sys.exit(1) + # write_copies diverges from args.replication here. + # args.replication is how many copies we will instruct Arvados to + # maintain (by passing it in collections().create()) after all + # data is written -- and if None was given, we'll use None there. + # Meanwhile, write_copies is how many copies of each data block we + # write to Keep, which has to be a number. + # + # If we simply changed args.replication from None to a default + # here, we'd end up erroneously passing the default replication + # level (instead of None) to collections().create(). + write_copies = (args.replication or + api_client._rootDesc.get('defaultCollectionReplication', 2)) + if args.progress: reporter = progress_writer(human_progress) elif args.batch_progress: @@ -423,11 +445,15 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr): sys.exit(1) if resume_cache is None: - writer = ArvPutCollectionWriter(resume_cache, reporter, bytes_expected, - num_retries=args.retries) + writer = ArvPutCollectionWriter( + resume_cache, reporter, bytes_expected, + num_retries=args.retries, + replication=write_copies) else: writer = ArvPutCollectionWriter.from_cache( - resume_cache, reporter, bytes_expected, num_retries=args.retries) + resume_cache, reporter, bytes_expected, + num_retries=args.retries, + replication=write_copies) # Install our signal handler for each code in CAUGHT_SIGNALS, and save # the originals. @@ -442,7 +468,16 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr): writer.report_progress() writer.do_queued_work() # Do work resumed from cache. for path in args.paths: # Copy file data to Keep. - if os.path.isdir(path): + if path == '-': + writer.start_new_stream() + writer.start_new_file(args.filename) + r = sys.stdin.read(64*1024) + while r: + # Need to bypass _queued_file check in ResumableCollectionWriter.write() to get + # CollectionWriter.write(). + super(arvados.collection.ResumableCollectionWriter, writer).write(r) + r = sys.stdin.read(64*1024) + elif os.path.isdir(path): writer.write_directory_tree( path, max_manifest_depth=args.max_manifest_depth) else: @@ -456,20 +491,25 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr): if args.stream: output = writer.manifest_text() if args.normalize: - output = CollectionReader(output).manifest_text(normalize=True) + output = arvados.collection.CollectionReader(output).manifest_text(normalize=True) elif args.raw: output = ','.join(writer.data_locators()) else: try: manifest_text = writer.manifest_text() if args.normalize: - manifest_text = CollectionReader(manifest_text).manifest_text(normalize=True) + manifest_text = arvados.collection.CollectionReader(manifest_text).manifest_text(normalize=True) + replication_attr = 'replication_desired' + if api_client._schema.schemas['Collection']['properties'].get(replication_attr, None) is None: + # API called it 'redundancy' before #3410. + replication_attr = 'redundancy' # Register the resulting collection in Arvados. collection = api_client.collections().create( body={ 'owner_uuid': project_uuid, 'name': collection_name, - 'manifest_text': manifest_text + 'manifest_text': manifest_text, + replication_attr: args.replication, }, ensure_unique_name=True ).execute(num_retries=args.retries)