4 # --md5sum - display md5 of each file as read from disk
8 import arvados.collection
25 from apiclient import errors as apiclient_errors
26 from arvados._version import __version__
28 import arvados.commands._util as arv_cmd
30 CAUGHT_SIGNALS = [signal.SIGINT, signal.SIGQUIT, signal.SIGTERM]
33 upload_opts = argparse.ArgumentParser(add_help=False)
35 upload_opts.add_argument('--version', action='version',
36 version="%s %s" % (sys.argv[0], __version__),
37 help='Print version and exit.')
38 upload_opts.add_argument('paths', metavar='path', type=str, nargs='*',
40 Local file or directory. Default: read from standard input.
43 _group = upload_opts.add_mutually_exclusive_group()
45 _group.add_argument('--max-manifest-depth', type=int, metavar='N',
47 Maximum depth of directory tree to represent in the manifest
48 structure. A directory structure deeper than this will be represented
49 as a single stream in the manifest. If N=0, the manifest will contain
50 a single stream. Default: -1 (unlimited), i.e., exactly one manifest
51 stream per filesystem directory that contains files.
54 _group.add_argument('--normalize', action='store_true',
56 Normalize the manifest by re-ordering files and streams after writing
60 _group = upload_opts.add_mutually_exclusive_group()
62 _group.add_argument('--as-stream', action='store_true', dest='stream',
67 _group.add_argument('--stream', action='store_true',
69 Store the file content and display the resulting manifest on
70 stdout. Do not write the manifest to Keep or save a Collection object
74 _group.add_argument('--as-manifest', action='store_true', dest='manifest',
76 Synonym for --manifest.
79 _group.add_argument('--in-manifest', action='store_true', dest='manifest',
81 Synonym for --manifest.
84 _group.add_argument('--manifest', action='store_true',
86 Store the file data and resulting manifest in Keep, save a Collection
87 object in Arvados, and display the manifest locator (Collection uuid)
88 on stdout. This is the default behavior.
91 _group.add_argument('--as-raw', action='store_true', dest='raw',
96 _group.add_argument('--raw', action='store_true',
98 Store the file content and display the data block locators on stdout,
99 separated by commas, with a trailing newline. Do not store a
103 upload_opts.add_argument('--use-filename', type=str, default=None,
104 dest='filename', help="""
105 Synonym for --filename.
108 upload_opts.add_argument('--filename', type=str, default=None,
110 Use the given filename in the manifest, instead of the name of the
111 local file. This is useful when "-" or "/dev/stdin" is given as an
112 input file. It can be used only if there is exactly one path given and
113 it is not a directory. Implies --manifest.
116 upload_opts.add_argument('--portable-data-hash', action='store_true',
118 Print the portable data hash instead of the Arvados UUID for the collection
119 created by the upload.
122 upload_opts.add_argument('--replication', type=int, metavar='N', default=None,
124 Set the replication level for the new collection: how many different
125 physical storage devices (e.g., disks) should have a copy of each data
126 block. Default is to use the server-provided default (if any) or 2.
129 run_opts = argparse.ArgumentParser(add_help=False)
131 run_opts.add_argument('--project-uuid', metavar='UUID', help="""
132 Store the collection in the specified project, instead of your Home
136 run_opts.add_argument('--name', help="""
137 Save the collection with the specified name.
140 _group = run_opts.add_mutually_exclusive_group()
141 _group.add_argument('--progress', action='store_true',
143 Display human-readable progress on stderr (bytes and, if possible,
144 percentage of total data size). This is the default behavior when
148 _group.add_argument('--no-progress', action='store_true',
150 Do not display human-readable progress on stderr, even if stderr is a
154 _group.add_argument('--batch-progress', action='store_true',
156 Display machine-readable progress on stderr (bytes and, if known,
160 _group = run_opts.add_mutually_exclusive_group()
161 _group.add_argument('--resume', action='store_true', default=True,
163 Continue interrupted uploads from cached state (default).
165 _group.add_argument('--no-resume', action='store_false', dest='resume',
167 Do not continue interrupted uploads from cached state.
170 arg_parser = argparse.ArgumentParser(
171 description='Copy data from the local filesystem to Keep.',
172 parents=[upload_opts, run_opts, arv_cmd.retry_opt])
174 def parse_arguments(arguments):
175 args = arg_parser.parse_args(arguments)
177 if len(args.paths) == 0:
180 args.paths = map(lambda x: "-" if x == "/dev/stdin" else x, args.paths)
182 if len(args.paths) != 1 or os.path.isdir(args.paths[0]):
185 --filename argument cannot be used when storing a directory or
189 # Turn on --progress by default if stderr is a tty.
190 if (not (args.batch_progress or args.no_progress)
191 and os.isatty(sys.stderr.fileno())):
194 if args.paths == ['-']:
196 if not args.filename:
197 args.filename = 'stdin'
201 class ResumeCacheConflict(Exception):
205 class ResumeCache(object):
206 CACHE_DIR = '.cache/arvados/arv-put'
208 def __init__(self, file_spec):
209 self.cache_file = open(file_spec, 'a+')
210 self._lock_file(self.cache_file)
211 self.filename = self.cache_file.name
214 def make_path(cls, args):
216 md5.update(arvados.config.get('ARVADOS_API_HOST', '!nohost'))
217 realpaths = sorted(os.path.realpath(path) for path in args.paths)
218 md5.update('\0'.join(realpaths))
219 if any(os.path.isdir(path) for path in realpaths):
220 md5.update(str(max(args.max_manifest_depth, -1)))
222 md5.update(args.filename)
224 arv_cmd.make_home_conf_dir(cls.CACHE_DIR, 0o700, 'raise'),
227 def _lock_file(self, fileobj):
229 fcntl.flock(fileobj, fcntl.LOCK_EX | fcntl.LOCK_NB)
231 raise ResumeCacheConflict("{} locked".format(fileobj.name))
234 self.cache_file.seek(0)
235 return json.load(self.cache_file)
237 def check_cache(self, api_client=None, num_retries=0):
242 if "_finished_streams" in state and len(state["_finished_streams"]) > 0:
243 locator = state["_finished_streams"][0][1][0]
244 elif "_current_stream_locators" in state and len(state["_current_stream_locators"]) > 0:
245 locator = state["_current_stream_locators"][0]
246 if locator is not None:
247 kc = arvados.keep.KeepClient(api_client=api_client)
248 kc.head(locator, num_retries=num_retries)
249 except Exception as e:
254 def save(self, data):
256 new_cache_fd, new_cache_name = tempfile.mkstemp(
257 dir=os.path.dirname(self.filename))
258 self._lock_file(new_cache_fd)
259 new_cache = os.fdopen(new_cache_fd, 'r+')
260 json.dump(data, new_cache)
261 os.rename(new_cache_name, self.filename)
262 except (IOError, OSError, ResumeCacheConflict) as error:
264 os.unlink(new_cache_name)
265 except NameError: # mkstemp failed.
268 self.cache_file.close()
269 self.cache_file = new_cache
272 self.cache_file.close()
276 os.unlink(self.filename)
277 except OSError as error:
278 if error.errno != errno.ENOENT: # That's what we wanted anyway.
284 self.__init__(self.filename)
287 class ArvPutUploadJob(object):
288 CACHE_DIR = '.cache/arvados/arv-put'
290 'manifest' : None, # Last saved manifest checkpoint
291 'files' : {} # Previous run file list: {path : {size, mtime}}
294 def __init__(self, paths, resume=True, reporter=None, bytes_expected=None,
295 name=None, owner_uuid=None, ensure_unique_name=False,
296 num_retries=None, replication_desired=None,
297 filename=None, update_time=1.0):
300 self.reporter = reporter
301 self.bytes_expected = bytes_expected
302 self.bytes_written = 0
303 self.bytes_skipped = 0
305 self.owner_uuid = owner_uuid
306 self.ensure_unique_name = ensure_unique_name
307 self.num_retries = num_retries
308 self.replication_desired = replication_desired
309 self.filename = filename
310 self._state_lock = threading.Lock()
311 self._state = None # Previous run state (file list & manifest)
312 self._current_files = [] # Current run file list
313 self._cache_file = None
314 self._collection = None
315 self._collection_lock = threading.Lock()
316 self._stop_checkpointer = threading.Event()
317 self._checkpointer = threading.Thread(target=self._update_task)
318 self._update_task_time = update_time # How many seconds wait between update runs
319 self.logger = logging.getLogger('arvados.arv_put')
320 # Load cached data if any and if needed
325 Start supporting thread & file uploading
327 self._checkpointer.daemon = True
328 self._checkpointer.start()
330 for path in self.paths:
331 # Test for stdin first, in case some file named '-' exist
333 self._write_stdin(self.filename or 'stdin')
334 elif os.path.isdir(path):
335 self._write_directory_tree(path)
337 self._write_file(path, self.filename or os.path.basename(path))
339 # Stop the thread before doing anything else
340 self._stop_checkpointer.set()
341 self._checkpointer.join()
342 # Commit all & one last _update()
346 self._cache_file.close()
347 # Correct the final written bytes count
348 self.bytes_written -= self.bytes_skipped
350 def save_collection(self):
351 with self._collection_lock:
352 self._my_collection().save_new(
353 name=self.name, owner_uuid=self.owner_uuid,
354 ensure_unique_name=self.ensure_unique_name,
355 num_retries=self.num_retries)
357 def destroy_cache(self):
360 os.unlink(self._cache_filename)
361 except OSError as error:
362 # That's what we wanted anyway.
363 if error.errno != errno.ENOENT:
365 self._cache_file.close()
367 def _collection_size(self, collection):
369 Recursively get the total size of the collection
372 for item in collection.values():
373 if isinstance(item, arvados.collection.Collection) or isinstance(item, arvados.collection.Subcollection):
374 size += self._collection_size(item)
379 def _update_task(self):
381 Periodically called support task. File uploading is
382 asynchronous so we poll status from the collection.
384 while not self._stop_checkpointer.wait(self._update_task_time):
389 Update cached manifest text and report progress.
391 with self._collection_lock:
392 self.bytes_written = self._collection_size(self._my_collection())
393 # Update cache, if resume enabled
395 with self._state_lock:
396 # Get the manifest text without comitting pending blocks
397 self._state['manifest'] = self._my_collection()._get_manifest_text(".", strip=False, normalize=False, only_committed=True)
399 # Call the reporter, if any
400 self.report_progress()
402 def report_progress(self):
403 if self.reporter is not None:
404 self.reporter(self.bytes_written, self.bytes_expected)
406 def _write_directory_tree(self, path, stream_name="."):
407 # TODO: Check what happens when multiple directories are passed as
409 # If the code below is uncommented, integration test
410 # test_ArvPutSignedManifest (tests.test_arv_put.ArvPutIntegrationTest)
411 # fails, I suppose it is because the manifest_uuid changes because
412 # of the dir addition to stream_name.
414 # if stream_name == '.':
415 # stream_name = os.path.join('.', os.path.basename(path))
416 for item in os.listdir(path):
417 if os.path.isdir(os.path.join(path, item)):
418 self._write_directory_tree(os.path.join(path, item),
419 os.path.join(stream_name, item))
421 self._write_file(os.path.join(path, item),
422 os.path.join(stream_name, item))
424 def _write_stdin(self, filename):
425 with self._collection_lock:
426 output = self._my_collection().open(filename, 'w')
427 self._write(sys.stdin, output)
430 def _write_file(self, source, filename):
433 # Check if file was already uploaded (at least partially)
434 with self._collection_lock:
436 file_in_collection = self._my_collection().find(filename)
439 file_in_collection = None
440 # If no previous cached data on this file, store it for an eventual
442 if source not in self._state['files']:
443 with self._state_lock:
444 self._state['files'][source] = {
445 'mtime': os.path.getmtime(source),
446 'size' : os.path.getsize(source)
448 with self._state_lock:
449 cached_file_data = self._state['files'][source]
450 # See if this file was already uploaded at least partially
451 if file_in_collection:
452 if cached_file_data['mtime'] == os.path.getmtime(source) and cached_file_data['size'] == os.path.getsize(source):
453 if cached_file_data['size'] == file_in_collection.size():
454 # File already there, skip it.
455 self.bytes_skipped += cached_file_data['size']
457 elif cached_file_data['size'] > file_in_collection.size():
458 # File partially uploaded, resume!
459 resume_offset = file_in_collection.size()
461 # Inconsistent cache, re-upload the file
462 self.logger.warning("Uploaded version of file '{}' is bigger than local version, will re-upload it from scratch.".format(source))
464 # Local file differs from cached data, re-upload it
466 with open(source, 'r') as source_fd:
467 if resume_offset > 0:
468 # Start upload where we left off
469 with self._collection_lock:
470 output = self._my_collection().open(filename, 'a')
471 source_fd.seek(resume_offset)
472 self.bytes_skipped += resume_offset
475 with self._collection_lock:
476 output = self._my_collection().open(filename, 'w')
477 self._write(source_fd, output)
478 output.close(flush=False)
480 def _write(self, source_fd, output):
483 data = source_fd.read(arvados.config.KEEP_BLOCK_SIZE)
484 # Allow an empty file to be written
485 if not data and not first_read:
491 def _my_collection(self):
493 Create a new collection if none cached. Load it from cache otherwise.
495 if self._collection is None:
496 with self._state_lock:
497 manifest = self._state['manifest']
498 if self.resume and manifest is not None:
499 # Create collection from saved state
500 self._collection = arvados.collection.Collection(
502 replication_desired=self.replication_desired)
504 # Create new collection
505 self._collection = arvados.collection.Collection(
506 replication_desired=self.replication_desired)
507 return self._collection
509 def _setup_state(self):
511 Create a new cache file or load a previously existing one.
515 md5.update(arvados.config.get('ARVADOS_API_HOST', '!nohost'))
516 realpaths = sorted(os.path.realpath(path) for path in self.paths)
517 md5.update('\0'.join(realpaths))
519 md5.update(self.filename)
520 cache_filename = md5.hexdigest()
521 self._cache_file = open(os.path.join(
522 arv_cmd.make_home_conf_dir(self.CACHE_DIR, 0o700, 'raise'),
523 cache_filename), 'a+')
524 self._cache_filename = self._cache_file.name
525 self._lock_file(self._cache_file)
526 self._cache_file.seek(0)
527 with self._state_lock:
529 self._state = json.load(self._cache_file)
530 if not set(['manifest', 'files']).issubset(set(self._state.keys())):
531 # Cache at least partially incomplete, set up new cache
532 self._state = copy.deepcopy(self.EMPTY_STATE)
534 # Cache file empty, set up new cache
535 self._state = copy.deepcopy(self.EMPTY_STATE)
536 # Load how many bytes were uploaded on previous run
537 with self._collection_lock:
538 self.bytes_written = self._collection_size(self._my_collection())
541 with self._state_lock:
542 self._state = copy.deepcopy(self.EMPTY_STATE)
544 def _lock_file(self, fileobj):
546 fcntl.flock(fileobj, fcntl.LOCK_EX | fcntl.LOCK_NB)
548 raise ResumeCacheConflict("{} locked".format(fileobj.name))
550 def _save_state(self):
552 Atomically save current state into cache.
555 with self._state_lock:
557 new_cache_fd, new_cache_name = tempfile.mkstemp(
558 dir=os.path.dirname(self._cache_filename))
559 self._lock_file(new_cache_fd)
560 new_cache = os.fdopen(new_cache_fd, 'r+')
561 json.dump(state, new_cache)
564 os.rename(new_cache_name, self._cache_filename)
565 except (IOError, OSError, ResumeCacheConflict) as error:
566 self.logger.error("There was a problem while saving the cache file: {}".format(error))
568 os.unlink(new_cache_name)
569 except NameError: # mkstemp failed.
572 self._cache_file.close()
573 self._cache_file = new_cache
575 def collection_name(self):
576 with self._collection_lock:
577 name = self._my_collection().api_response()['name'] if self._my_collection().api_response() else None
580 def manifest_locator(self):
581 with self._collection_lock:
582 locator = self._my_collection().manifest_locator()
585 def portable_data_hash(self):
586 with self._collection_lock:
587 datahash = self._my_collection().portable_data_hash()
590 def manifest_text(self, stream_name=".", strip=False, normalize=False):
591 with self._collection_lock:
592 manifest = self._my_collection().manifest_text(stream_name, strip, normalize)
595 def _datablocks_on_item(self, item):
597 Return a list of datablock locators, recursively navigating
598 through subcollections
600 if isinstance(item, arvados.arvfile.ArvadosFile):
603 return ["d41d8cd98f00b204e9800998ecf8427e+0"]
606 for segment in item.segments():
607 loc = segment.locator
610 elif isinstance(item, arvados.collection.Collection):
611 l = [self._datablocks_on_item(x) for x in item.values()]
612 # Fast list flattener method taken from:
613 # http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
614 return [loc for sublist in l for loc in sublist]
618 def data_locators(self):
619 with self._collection_lock:
620 # Make sure all datablocks are flushed before getting the locators
621 self._my_collection().manifest_text()
622 datablocks = self._datablocks_on_item(self._my_collection())
626 def expected_bytes_for(pathlist):
627 # Walk the given directory trees and stat files, adding up file sizes,
628 # so we can display progress as percent
630 for path in pathlist:
631 if os.path.isdir(path):
632 for filename in arvados.util.listdir_recursive(path):
633 bytesum += os.path.getsize(os.path.join(path, filename))
634 elif not os.path.isfile(path):
637 bytesum += os.path.getsize(path)
640 _machine_format = "{} {}: {{}} written {{}} total\n".format(sys.argv[0],
642 def machine_progress(bytes_written, bytes_expected):
643 return _machine_format.format(
644 bytes_written, -1 if (bytes_expected is None) else bytes_expected)
646 def human_progress(bytes_written, bytes_expected):
648 return "\r{}M / {}M {:.1%} ".format(
649 bytes_written >> 20, bytes_expected >> 20,
650 float(bytes_written) / bytes_expected)
652 return "\r{} ".format(bytes_written)
654 def progress_writer(progress_func, outfile=sys.stderr):
655 def write_progress(bytes_written, bytes_expected):
656 outfile.write(progress_func(bytes_written, bytes_expected))
657 return write_progress
659 def exit_signal_handler(sigcode, frame):
662 def desired_project_uuid(api_client, project_uuid, num_retries):
664 query = api_client.users().current()
665 elif arvados.util.user_uuid_pattern.match(project_uuid):
666 query = api_client.users().get(uuid=project_uuid)
667 elif arvados.util.group_uuid_pattern.match(project_uuid):
668 query = api_client.groups().get(uuid=project_uuid)
670 raise ValueError("Not a valid project UUID: {}".format(project_uuid))
671 return query.execute(num_retries=num_retries)['uuid']
673 def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
676 args = parse_arguments(arguments)
678 if api_client is None:
679 api_client = arvados.api('v1')
681 # Determine the name to use
683 if args.stream or args.raw:
684 print >>stderr, "Cannot use --name with --stream or --raw"
686 collection_name = args.name
688 collection_name = "Saved at {} by {}@{}".format(
689 datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"),
690 pwd.getpwuid(os.getuid()).pw_name,
691 socket.gethostname())
693 if args.project_uuid and (args.stream or args.raw):
694 print >>stderr, "Cannot use --project-uuid with --stream or --raw"
697 # Determine the parent project
699 project_uuid = desired_project_uuid(api_client, args.project_uuid,
701 except (apiclient_errors.Error, ValueError) as error:
702 print >>stderr, error
706 reporter = progress_writer(human_progress)
707 elif args.batch_progress:
708 reporter = progress_writer(machine_progress)
712 bytes_expected = expected_bytes_for(args.paths)
714 writer = ArvPutUploadJob(paths = args.paths,
715 resume = args.resume,
716 filename = args.filename,
718 bytes_expected = bytes_expected,
719 num_retries = args.retries,
720 replication_desired = args.replication,
721 name = collection_name,
722 owner_uuid = project_uuid,
723 ensure_unique_name = True)
724 except ResumeCacheConflict:
725 print >>stderr, "\n".join([
726 "arv-put: Another process is already uploading this data.",
727 " Use --no-resume if this is really what you want."])
730 # Install our signal handler for each code in CAUGHT_SIGNALS, and save
732 orig_signal_handlers = {sigcode: signal.signal(sigcode, exit_signal_handler)
733 for sigcode in CAUGHT_SIGNALS}
735 if args.resume and writer.bytes_written > 0:
736 print >>stderr, "\n".join([
737 "arv-put: Resuming previous upload from last checkpoint.",
738 " Use the --no-resume option to start over."])
740 writer.report_progress()
743 if args.progress: # Print newline to split stderr from stdout for humans.
748 output = writer.manifest_text(normalize=True)
750 output = writer.manifest_text()
752 output = ','.join(writer.data_locators())
755 writer.save_collection()
756 print >>stderr, "Collection saved as '%s'" % writer.collection_name()
757 if args.portable_data_hash:
758 output = writer.portable_data_hash()
760 output = writer.manifest_locator()
761 except apiclient_errors.Error as error:
763 "arv-put: Error creating Collection on project: {}.".format(
767 # Print the locator (uuid) of the new collection.
772 if not output.endswith('\n'):
775 for sigcode, orig_handler in orig_signal_handlers.items():
776 signal.signal(sigcode, orig_handler)
782 writer.destroy_cache()
786 if __name__ == '__main__':