4 # --md5sum - display md5 of each file as read from disk
8 import arvados.collection
23 from apiclient import errors as apiclient_errors
25 import arvados.commands._util as arv_cmd
27 CAUGHT_SIGNALS = [signal.SIGINT, signal.SIGQUIT, signal.SIGTERM]
30 upload_opts = argparse.ArgumentParser(add_help=False)
32 upload_opts.add_argument('paths', metavar='path', type=str, nargs='*',
34 Local file or directory. Default: read from standard input.
37 _group = upload_opts.add_mutually_exclusive_group()
39 _group.add_argument('--max-manifest-depth', type=int, metavar='N',
41 Maximum depth of directory tree to represent in the manifest
42 structure. A directory structure deeper than this will be represented
43 as a single stream in the manifest. If N=0, the manifest will contain
44 a single stream. Default: -1 (unlimited), i.e., exactly one manifest
45 stream per filesystem directory that contains files.
48 _group.add_argument('--normalize', action='store_true',
50 Normalize the manifest by re-ordering files and streams after writing
54 _group = upload_opts.add_mutually_exclusive_group()
56 _group.add_argument('--as-stream', action='store_true', dest='stream',
61 _group.add_argument('--stream', action='store_true',
63 Store the file content and display the resulting manifest on
64 stdout. Do not write the manifest to Keep or save a Collection object
68 _group.add_argument('--as-manifest', action='store_true', dest='manifest',
70 Synonym for --manifest.
73 _group.add_argument('--in-manifest', action='store_true', dest='manifest',
75 Synonym for --manifest.
78 _group.add_argument('--manifest', action='store_true',
80 Store the file data and resulting manifest in Keep, save a Collection
81 object in Arvados, and display the manifest locator (Collection uuid)
82 on stdout. This is the default behavior.
85 _group.add_argument('--as-raw', action='store_true', dest='raw',
90 _group.add_argument('--raw', action='store_true',
92 Store the file content and display the data block locators on stdout,
93 separated by commas, with a trailing newline. Do not store a
97 upload_opts.add_argument('--use-filename', type=str, default=None,
98 dest='filename', help="""
99 Synonym for --filename.
102 upload_opts.add_argument('--filename', type=str, default=None,
104 Use the given filename in the manifest, instead of the name of the
105 local file. This is useful when "-" or "/dev/stdin" is given as an
106 input file. It can be used only if there is exactly one path given and
107 it is not a directory. Implies --manifest.
110 upload_opts.add_argument('--portable-data-hash', action='store_true',
112 Print the portable data hash instead of the Arvados UUID for the collection
113 created by the upload.
116 upload_opts.add_argument('--replication', type=int, metavar='N', default=None,
118 Set the replication level for the new collection: how many different
119 physical storage devices (e.g., disks) should have a copy of each data
120 block. Default is to use the server-provided default (if any) or 2.
123 run_opts = argparse.ArgumentParser(add_help=False)
125 run_opts.add_argument('--project-uuid', metavar='UUID', help="""
126 Store the collection in the specified project, instead of your Home
130 run_opts.add_argument('--name', help="""
131 Save the collection with the specified name.
134 _group = run_opts.add_mutually_exclusive_group()
135 _group.add_argument('--progress', action='store_true',
137 Display human-readable progress on stderr (bytes and, if possible,
138 percentage of total data size). This is the default behavior when
142 _group.add_argument('--no-progress', action='store_true',
144 Do not display human-readable progress on stderr, even if stderr is a
148 _group.add_argument('--batch-progress', action='store_true',
150 Display machine-readable progress on stderr (bytes and, if known,
154 _group = run_opts.add_mutually_exclusive_group()
155 _group.add_argument('--resume', action='store_true', default=True,
157 Continue interrupted uploads from cached state (default).
159 _group.add_argument('--no-resume', action='store_false', dest='resume',
161 Do not continue interrupted uploads from cached state.
164 arg_parser = argparse.ArgumentParser(
165 description='Copy data from the local filesystem to Keep.',
166 parents=[upload_opts, run_opts, arv_cmd.retry_opt])
168 def parse_arguments(arguments):
169 args = arg_parser.parse_args(arguments)
171 if len(args.paths) == 0:
174 args.paths = map(lambda x: "-" if x == "/dev/stdin" else x, args.paths)
176 if len(args.paths) != 1 or os.path.isdir(args.paths[0]):
179 --filename argument cannot be used when storing a directory or
183 # Turn on --progress by default if stderr is a tty.
184 if (not (args.batch_progress or args.no_progress)
185 and os.isatty(sys.stderr.fileno())):
188 if args.paths == ['-']:
190 if not args.filename:
191 args.filename = 'stdin'
195 class ResumeCacheConflict(Exception):
199 class FileUploadError(Exception):
203 class ResumeCache(object):
204 CACHE_DIR = '.cache/arvados/arv-put'
206 def __init__(self, file_spec):
207 self.cache_file = open(file_spec, 'a+')
208 self._lock_file(self.cache_file)
209 self.filename = self.cache_file.name
212 def make_path(cls, args):
214 md5.update(arvados.config.get('ARVADOS_API_HOST', '!nohost'))
215 realpaths = sorted(os.path.realpath(path) for path in args.paths)
216 md5.update('\0'.join(realpaths))
217 if any(os.path.isdir(path) for path in realpaths):
218 md5.update(str(max(args.max_manifest_depth, -1)))
220 md5.update(args.filename)
222 arv_cmd.make_home_conf_dir(cls.CACHE_DIR, 0o700, 'raise'),
225 def _lock_file(self, fileobj):
227 fcntl.flock(fileobj, fcntl.LOCK_EX | fcntl.LOCK_NB)
229 raise ResumeCacheConflict("{} locked".format(fileobj.name))
232 self.cache_file.seek(0)
233 return json.load(self.cache_file)
235 def check_cache(self, api_client=None, num_retries=0):
240 if "_finished_streams" in state and len(state["_finished_streams"]) > 0:
241 locator = state["_finished_streams"][0][1][0]
242 elif "_current_stream_locators" in state and len(state["_current_stream_locators"]) > 0:
243 locator = state["_current_stream_locators"][0]
244 if locator is not None:
245 kc = arvados.keep.KeepClient(api_client=api_client)
246 kc.head(locator, num_retries=num_retries)
247 except Exception as e:
252 def save(self, data):
254 new_cache_fd, new_cache_name = tempfile.mkstemp(
255 dir=os.path.dirname(self.filename))
256 self._lock_file(new_cache_fd)
257 new_cache = os.fdopen(new_cache_fd, 'r+')
258 json.dump(data, new_cache)
259 os.rename(new_cache_name, self.filename)
260 except (IOError, OSError, ResumeCacheConflict) as error:
262 os.unlink(new_cache_name)
263 except NameError: # mkstemp failed.
266 self.cache_file.close()
267 self.cache_file = new_cache
270 self.cache_file.close()
274 os.unlink(self.filename)
275 except OSError as error:
276 if error.errno != errno.ENOENT: # That's what we wanted anyway.
282 self.__init__(self.filename)
285 class ArvPutUploadJob(object):
286 def __init__(self, paths, resume=True, reporter=None, bytes_expected=None,
287 name=None, owner_uuid=None, ensure_unique_name=False,
288 num_retries=None, write_copies=None, replication=None,
292 self.reporter = reporter
293 self.bytes_expected = bytes_expected
294 self.bytes_written = 0
296 self.owner_uuid = owner_uuid
297 self.ensure_unique_name = ensure_unique_name
298 self.num_retries = num_retries
299 self.write_copies = write_copies
300 self.replication = replication
301 self.filename = filename
302 self._state_lock = threading.Lock()
303 self._state = None # Previous run state (file list & manifest)
304 self._current_files = [] # Current run file list
305 self._cache_hash = None # MD5 digest based on paths & filename
306 self._cache_file = None
307 self._collection = None
308 self._collection_lock = threading.Lock()
309 self._stop_checkpointer = threading.Event()
310 self._checkpointer = threading.Thread(target=self._update_task)
311 self._update_task_time = 60.0 # How many seconds wait between update runs
312 # Load cached data if any and if needed
317 Start supporting thread & file uploading
319 self._checkpointer.start()
320 for path in self.paths:
322 # Test for stdin first, in case some file named '-' exist
324 self._write_stdin(self.filename or 'stdin')
325 elif os.path.isdir(path):
326 self._write_directory_tree(path)
327 else: #if os.path.isfile(path):
328 self._write_file(path, self.filename or os.path.basename(path))
330 # raise FileUploadError('Inadequate file type, cannot upload: %s' % path)
332 # Stop the thread before continue complaining
333 self._stop_checkpointer.set()
334 self._checkpointer.join()
336 # Work finished, stop updater task
337 self._stop_checkpointer.set()
338 self._checkpointer.join()
339 # Successful upload, one last _update()
342 def save_collection(self):
343 with self._collection_lock:
344 self._my_collection().save_new(
345 name=self.name, owner_uuid=self.owner_uuid,
346 ensure_unique_name=self.ensure_unique_name,
347 num_retries=self.num_retries,
348 replication_desired=self.replication)
350 # Delete cache file upon successful collection saving
352 os.unlink(self._cache_file.name)
353 except OSError as error:
354 if error.errno != errno.ENOENT: # That's what we wanted anyway.
356 self._cache_file.close()
358 def _collection_size(self, collection):
360 Recursively get the total size of the collection
363 for item in collection:
364 if isinstance(item, arvados.arvfile.ArvadosFile):
366 elif isinstance(item, arvados.collection.Collection):
367 size += self._collection_size(item)
370 def _update_task(self):
372 Periodically call support tasks. File uploading is
373 asynchronous so we poll status from the collection.
375 while not self._stop_checkpointer.wait(self._update_task_time):
380 Update cached manifest text and report progress.
382 with self._collection_lock:
383 self.bytes_written = self._collection_size(self._my_collection())
384 # Update cache, if resume enabled
386 with self._state_lock:
387 self._state['manifest'] = self._my_collection().manifest_text()
390 # Call the reporter, if any
391 self.report_progress()
393 def report_progress(self):
394 if self.reporter is not None:
395 self.reporter(self.bytes_written, self.bytes_expected)
397 def _write_directory_tree(self, path, stream_name="."):
398 # TODO: Check what happens when multiple directories are passes as
400 # If the code below is uncommented, integration test
401 # test_ArvPutSignedManifest (tests.test_arv_put.ArvPutIntegrationTest)
402 # fails, I suppose it is because the manifest_uuid changes because
403 # of the dir addition to stream_name.
405 # if stream_name == '.':
406 # stream_name = os.path.join('.', os.path.basename(path))
407 for item in os.listdir(path):
408 if os.path.isdir(os.path.join(path, item)):
409 self._write_directory_tree(os.path.join(path, item),
410 os.path.join(stream_name, item))
411 elif os.path.isfile(os.path.join(path, item)):
412 self._write_file(os.path.join(path, item),
413 os.path.join(stream_name, item))
415 raise FileUploadError('Inadequate file type, cannot upload: %s' % path)
417 def _write_stdin(self, filename):
418 with self._collection_lock:
419 output = self._my_collection().open(filename, 'w')
420 self._write(sys.stdin, output)
423 def _write_file(self, source, filename):
425 resume_upload = False
427 # Check if file was already uploaded (at least partially)
428 with self._collection_lock:
430 file_in_collection = self._my_collection().find(filename)
433 file_in_collection = None
434 # If no previous cached data on this file, store it for an eventual
436 if source not in self._state['files'].keys():
437 with self._state_lock:
438 self._state['files'][source] = {
439 'mtime' : os.path.getmtime(source),
440 'size' : os.path.getsize(source)
442 cached_file_data = self._state['files'][source]
443 # See if this file was already uploaded at least partially
444 if file_in_collection:
445 if cached_file_data['mtime'] == os.path.getmtime(source) and cached_file_data['size'] == os.path.getsize(source):
446 if os.path.getsize(source) == file_in_collection.size():
447 # File already there, skip it.
449 elif os.path.getsize(source) > file_in_collection.size():
450 # File partially uploaded, resume!
452 resume_offset = file_in_collection.size()
454 # Inconsistent cache, re-upload the file
457 # Local file differs from cached data, re-upload it
459 with open(source, 'r') as source_fd:
460 if self.resume and resume_upload:
461 with self._collection_lock:
463 output = self._my_collection().open(filename, 'a')
464 source_fd.seek(resume_offset)
466 with self._collection_lock:
467 output = self._my_collection().open(filename, 'w')
468 self._write(source_fd, output)
471 def _write(self, source_fd, output):
473 data = source_fd.read(arvados.config.KEEP_BLOCK_SIZE)
478 def _my_collection(self):
480 Create a new collection if none cached. Load it from cache otherwise.
482 if self._collection is None:
483 with self._state_lock:
484 manifest = self._state['manifest']
485 if self.resume and manifest is not None:
486 # Create collection from saved state
487 self._collection = arvados.collection.Collection(
489 num_write_copies=self.write_copies)
491 # Create new collection
492 self._collection = arvados.collection.Collection(
493 num_write_copies=self.write_copies)
494 return self._collection
496 def _setup_state(self):
498 Create a new cache file or load a previously existing one.
502 md5.update(arvados.config.get('ARVADOS_API_HOST', '!nohost'))
503 realpaths = sorted(os.path.realpath(path) for path in self.paths)
504 md5.update('\0'.join(realpaths))
505 self._cache_hash = md5.hexdigest()
507 md5.update(self.filename)
508 self._cache_file = open(os.path.join(
509 arv_cmd.make_home_conf_dir('.cache/arvados/arv-put', 0o700, 'raise'),
510 self._cache_hash), 'a+')
511 self._cache_file.seek(0)
512 with self._state_lock:
514 self._state = json.load(self._cache_file)
515 if not 'manifest' in self._state.keys():
516 self._state['manifest'] = ""
517 if not 'files' in self._state.keys():
518 self._state['files'] = {}
520 # File empty, set up new cache
523 # Previous run file list: {path : {size, mtime}}
526 # Load how many bytes were uploaded on previous run
527 with self._collection_lock:
528 self.bytes_written = self._collection_size(self._my_collection())
531 with self._state_lock:
534 'files' : {} # Previous run file list: {path : {size, mtime}}
537 def _lock_file(self, fileobj):
539 fcntl.flock(fileobj, fcntl.LOCK_EX | fcntl.LOCK_NB)
541 raise ResumeCacheConflict("{} locked".format(fileobj.name))
543 def _save_state(self):
545 Atomically save current state into cache.
548 with self._state_lock:
550 new_cache_fd, new_cache_name = tempfile.mkstemp(
551 dir=os.path.dirname(self._cache_file.name))
552 self._lock_file(new_cache_fd)
553 new_cache = os.fdopen(new_cache_fd, 'r+')
554 json.dump(state, new_cache)
556 # os.fsync(new_cache)
557 os.rename(new_cache_name, self._cache_file.name)
558 except (IOError, OSError, ResumeCacheConflict) as error:
560 os.unlink(new_cache_name)
561 except NameError: # mkstemp failed.
564 self._cache_file.close()
565 self._cache_file = new_cache
567 def collection_name(self):
568 with self._collection_lock:
569 name = self._my_collection().api_response()['name'] if self._my_collection().api_response() else None
572 def manifest_locator(self):
573 with self._collection_lock:
574 locator = self._my_collection().manifest_locator()
577 def portable_data_hash(self):
578 with self._collection_lock:
579 datahash = self._my_collection().portable_data_hash()
582 def manifest_text(self, stream_name=".", strip=False, normalize=False):
583 with self._collection_lock:
584 manifest = self._my_collection().manifest_text(stream_name, strip, normalize)
587 def _datablocks_on_item(self, item):
589 Return a list of datablock locators, recursively navigating
590 through subcollections
592 if isinstance(item, arvados.arvfile.ArvadosFile):
594 for segment in item.segments():
595 loc = segment.locator
596 if loc.startswith("bufferblock"):
597 loc = item._bufferblocks[loc].calculate_locator()
600 elif isinstance(item, arvados.collection.Collection) or isinstance(item, arvados.collection.Subcollection):
601 l = [self._datablocks_on_item(x) for x in item.values()]
602 # Fast list flattener method taken from:
603 # http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
604 return [loc for sublist in l for loc in sublist]
608 def data_locators(self):
609 with self._collection_lock:
610 datablocks = self._datablocks_on_item(self._my_collection())
614 class ArvPutCollectionWriter(arvados.ResumableCollectionWriter):
615 STATE_PROPS = (arvados.ResumableCollectionWriter.STATE_PROPS +
616 ['bytes_written', '_seen_inputs'])
618 def __init__(self, cache=None, reporter=None, bytes_expected=None, **kwargs):
619 self.bytes_written = 0
620 self._seen_inputs = []
622 self.reporter = reporter
623 self.bytes_expected = bytes_expected
624 super(ArvPutCollectionWriter, self).__init__(**kwargs)
627 def from_cache(cls, cache, reporter=None, bytes_expected=None,
628 num_retries=0, replication=0):
631 state['_data_buffer'] = [base64.decodestring(state['_data_buffer'])]
632 writer = cls.from_state(state, cache, reporter, bytes_expected,
633 num_retries=num_retries,
634 replication=replication)
635 except (TypeError, ValueError,
636 arvados.errors.StaleWriterStateError) as error:
637 return cls(cache, reporter, bytes_expected,
638 num_retries=num_retries,
639 replication=replication)
643 def cache_state(self):
644 if self.cache is None:
646 state = self.dump_state()
647 # Transform attributes for serialization.
648 for attr, value in state.items():
649 if attr == '_data_buffer':
650 state[attr] = base64.encodestring(''.join(value))
651 elif hasattr(value, 'popleft'):
652 state[attr] = list(value)
653 self.cache.save(state)
655 def report_progress(self):
656 if self.reporter is not None:
657 self.reporter(self.bytes_written, self.bytes_expected)
659 def flush_data(self):
660 start_buffer_len = self._data_buffer_len
661 start_block_count = self.bytes_written / arvados.config.KEEP_BLOCK_SIZE
662 super(ArvPutCollectionWriter, self).flush_data()
663 if self._data_buffer_len < start_buffer_len: # We actually PUT data.
664 self.bytes_written += (start_buffer_len - self._data_buffer_len)
665 self.report_progress()
666 if (self.bytes_written / arvados.config.KEEP_BLOCK_SIZE) > start_block_count:
669 def _record_new_input(self, input_type, source_name, dest_name):
670 # The key needs to be a list because that's what we'll get back
671 # from JSON deserialization.
672 key = [input_type, source_name, dest_name]
673 if key in self._seen_inputs:
675 self._seen_inputs.append(key)
678 def write_file(self, source, filename=None):
679 if self._record_new_input('file', source, filename):
680 super(ArvPutCollectionWriter, self).write_file(source, filename)
682 def write_directory_tree(self,
683 path, stream_name='.', max_manifest_depth=-1):
684 if self._record_new_input('directory', path, stream_name):
685 super(ArvPutCollectionWriter, self).write_directory_tree(
686 path, stream_name, max_manifest_depth)
689 def expected_bytes_for(pathlist):
690 # Walk the given directory trees and stat files, adding up file sizes,
691 # so we can display progress as percent
693 for path in pathlist:
694 if os.path.isdir(path):
695 for filename in arvados.util.listdir_recursive(path):
696 bytesum += os.path.getsize(os.path.join(path, filename))
697 elif not os.path.isfile(path):
700 bytesum += os.path.getsize(path)
703 _machine_format = "{} {}: {{}} written {{}} total\n".format(sys.argv[0],
705 def machine_progress(bytes_written, bytes_expected):
706 return _machine_format.format(
707 bytes_written, -1 if (bytes_expected is None) else bytes_expected)
709 def human_progress(bytes_written, bytes_expected):
711 return "\r{}M / {}M {:.1%} ".format(
712 bytes_written >> 20, bytes_expected >> 20,
713 float(bytes_written) / bytes_expected)
715 return "\r{} ".format(bytes_written)
717 def progress_writer(progress_func, outfile=sys.stderr):
718 def write_progress(bytes_written, bytes_expected):
719 outfile.write(progress_func(bytes_written, bytes_expected))
720 return write_progress
722 def exit_signal_handler(sigcode, frame):
725 def desired_project_uuid(api_client, project_uuid, num_retries):
727 query = api_client.users().current()
728 elif arvados.util.user_uuid_pattern.match(project_uuid):
729 query = api_client.users().get(uuid=project_uuid)
730 elif arvados.util.group_uuid_pattern.match(project_uuid):
731 query = api_client.groups().get(uuid=project_uuid)
733 raise ValueError("Not a valid project UUID: {}".format(project_uuid))
734 return query.execute(num_retries=num_retries)['uuid']
736 def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
739 args = parse_arguments(arguments)
741 if api_client is None:
742 api_client = arvados.api('v1')
744 # Determine the name to use
746 if args.stream or args.raw:
747 print >>stderr, "Cannot use --name with --stream or --raw"
749 collection_name = args.name
751 collection_name = "Saved at {} by {}@{}".format(
752 datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"),
753 pwd.getpwuid(os.getuid()).pw_name,
754 socket.gethostname())
756 if args.project_uuid and (args.stream or args.raw):
757 print >>stderr, "Cannot use --project-uuid with --stream or --raw"
760 # Determine the parent project
762 project_uuid = desired_project_uuid(api_client, args.project_uuid,
764 except (apiclient_errors.Error, ValueError) as error:
765 print >>stderr, error
768 # write_copies diverges from args.replication here.
769 # args.replication is how many copies we will instruct Arvados to
770 # maintain (by passing it in collections().create()) after all
771 # data is written -- and if None was given, we'll use None there.
772 # Meanwhile, write_copies is how many copies of each data block we
773 # write to Keep, which has to be a number.
775 # If we simply changed args.replication from None to a default
776 # here, we'd end up erroneously passing the default replication
777 # level (instead of None) to collections().create().
778 write_copies = (args.replication or
779 api_client._rootDesc.get('defaultCollectionReplication', 2))
782 reporter = progress_writer(human_progress)
783 elif args.batch_progress:
784 reporter = progress_writer(machine_progress)
788 bytes_expected = expected_bytes_for(args.paths)
790 writer = ArvPutUploadJob(paths = args.paths,
791 resume = args.resume,
793 bytes_expected = bytes_expected,
794 num_retries = args.retries,
795 write_copies = write_copies,
796 replication = args.replication,
797 name = collection_name,
798 owner_uuid = project_uuid,
799 ensure_unique_name = True)
800 except ResumeCacheConflict:
801 print >>stderr, "\n".join([
802 "arv-put: Another process is already uploading this data.",
803 " Use --no-resume if this is really what you want."])
806 # Install our signal handler for each code in CAUGHT_SIGNALS, and save
808 orig_signal_handlers = {sigcode: signal.signal(sigcode, exit_signal_handler)
809 for sigcode in CAUGHT_SIGNALS}
811 if args.resume and writer.bytes_written > 0:
812 print >>stderr, "\n".join([
813 "arv-put: Resuming previous upload from last checkpoint.",
814 " Use the --no-resume option to start over."])
816 writer.report_progress()
819 if args.progress: # Print newline to split stderr from stdout for humans.
824 output = writer.manifest_text(normalize=True)
826 output = writer.manifest_text()
828 output = ','.join(writer.data_locators())
831 writer.save_collection()
832 print >>stderr, "Collection saved as '%s'" % writer.collection_name()
833 if args.portable_data_hash:
834 output = writer.portable_data_hash()
836 output = writer.manifest_locator()
837 except apiclient_errors.Error as error:
839 "arv-put: Error creating Collection on project: {}.".format(
843 # Print the locator (uuid) of the new collection.
848 if not output.endswith('\n'):
851 for sigcode, orig_handler in orig_signal_handlers.items():
852 signal.signal(sigcode, orig_handler)
860 if __name__ == '__main__':