X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d56c1ef84083152a6623d8661ca45b93a605e0ea..64eadab02f0ffd58b3b6c66c463b91abe07ecc07:/sdk/python/arvados/commands/put.py diff --git a/sdk/python/arvados/commands/put.py b/sdk/python/arvados/commands/put.py index aa8467cb95..4b04ad229e 100644 --- a/sdk/python/arvados/commands/put.py +++ b/sdk/python/arvados/commands/put.py @@ -236,10 +236,11 @@ Do not save upload state in a cache file for resuming. """) _group = upload_opts.add_mutually_exclusive_group() -_group.add_argument('--trash-at', metavar='YYYY-MM-DD HH:MM', default=None, +_group.add_argument('--trash-at', metavar='YYYY-MM-DDTHH:MM', default=None, help=""" Set the trash date of the resulting collection to an absolute date in the future. -The accepted format is defined by the ISO 8601 standard. +The accepted format is defined by the ISO 8601 standard. Examples: 20090103, 2009-01-03, 20090103T181505, 2009-01-03T18:15:05.\n +Timezone information can be added. If not, the provided date/time is assumed as being in the local system's timezone. """) _group.add_argument('--trash-after', type=int, metavar='DAYS', default=None, help=""" @@ -484,10 +485,13 @@ class ArvPutUploadJob(object): self.follow_links = follow_links self.exclude_paths = exclude_paths self.exclude_names = exclude_names - self.trash_at = trash_at + self._trash_at = trash_at - if self.trash_at is not None and type(self.trash_at) not in [datetime.datetime, datetime.timedelta]: - raise TypeError('trash_at should be None, datetime or timedelta') + if self._trash_at is not None: + if type(self._trash_at) not in [datetime.datetime, datetime.timedelta]: + raise TypeError('trash_at should be None, timezone-naive datetime or timedelta') + if type(self._trash_at) == datetime.datetime and self._trash_at.tzinfo is not None: + raise TypeError('provided trash_at datetime should be timezone-naive') if not self.use_cache and self.resume: raise ArvPutArgumentConflict('resume cannot be True when use_cache is False') @@ -628,11 +632,18 @@ class ArvPutUploadJob(object): if self.use_cache: self._cache_file.close() - def save_collection(self): - if type(self.trash_at) == datetime.timedelta: - # Get an absolute datetime for trash_at before saving. - self.trash_at = datetime.datetime.utcnow() + self.trash_at + def _collection_trash_at(self): + """ + Returns the trash date that the collection should use at save time. + Takes into account absolute/relative trash_at values requested + by the user. + """ + if type(self._trash_at) == datetime.timedelta: + # Get an absolute datetime for trash_at + return datetime.datetime.utcnow() + self._trash_at + return self._trash_at + def save_collection(self): if self.update: # Check if files should be updated on the remote collection. for fp in self._file_paths: @@ -648,7 +659,7 @@ class ArvPutUploadJob(object): pass self._remote_collection.save(storage_classes=self.storage_classes, num_retries=self.num_retries, - trash_at=self.trash_at) + trash_at=self._collection_trash_at()) else: if self.storage_classes is None: self.storage_classes = ['default'] @@ -657,7 +668,7 @@ class ArvPutUploadJob(object): storage_classes=self.storage_classes, ensure_unique_name=self.ensure_unique_name, num_retries=self.num_retries, - trash_at=self.trash_at) + trash_at=self._collection_trash_at()) def destroy_cache(self): if self.use_cache: @@ -712,6 +723,15 @@ class ArvPutUploadJob(object): self._save_state() except Exception as e: self.logger.error("Unexpected error trying to save cache file: {}".format(e)) + # Keep remote collection's trash_at attribute synced when using relative expire dates + if self._remote_collection is not None and type(self._trash_at) == datetime.timedelta: + try: + self._api_client.collections().update( + uuid=self._remote_collection.manifest_locator(), + body={'trash_at': self._collection_trash_at().strftime("%Y-%m-%dT%H:%M:%S.%fZ")} + ).execute(num_retries=self.num_retries) + except Exception as e: + self.logger.error("Unexpected error trying to update remote collection's expire date: {}".format(e)) else: self.bytes_written = self.bytes_skipped # Call the reporter, if any @@ -982,6 +1002,9 @@ class ArvPutUploadJob(object): def collection_name(self): return self._my_collection().api_response()['name'] if self._my_collection().api_response() else None + def collection_trash_at(self): + return self._my_collection().get_trash_at() + def manifest_locator(self): return self._my_collection().manifest_locator() @@ -1100,22 +1123,35 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr, # Trash arguments validation trash_at = None if args.trash_at is not None: + # ciso8601 considers YYYYMM as invalid but YYYY-MM as valid, so here we + # make sure the user provides a complete YYYY-MM-DD date. + if not re.match(r'^\d{4}(?P-?)\d{2}?(?P=dash)\d{2}', args.trash_at): + logger.error("--trash-at argument format invalid, use --help to see examples.") + sys.exit(1) + # Check if no time information was provided. In that case, assume end-of-day. + if re.match(r'^\d{4}(?P-?)\d{2}?(?P=dash)\d{2}$', args.trash_at): + args.trash_at += 'T23:59:59' try: trash_at = ciso8601.parse_datetime(args.trash_at) except: - logger.error("--trash-at argument format invalid, should be YYYY-MM-DDTHH:MM.") + logger.error("--trash-at argument format invalid, use --help to see examples.") sys.exit(1) else: if trash_at.tzinfo is not None: - # Timezone-aware datetime provided, convert to non-aware UTC - delta = trash_at.tzinfo.utcoffset(None) - trash_at = trash_at.replace(tzinfo=None) - delta + # Timezone aware datetime provided. + utcoffset = -trash_at.utcoffset() + else: + # Timezone naive datetime provided. Assume is local. + utcoffset = datetime.timedelta(seconds=time.timezone) + # Convert to UTC timezone naive datetime. + trash_at = trash_at.replace(tzinfo=None) + utcoffset + if trash_at <= datetime.datetime.utcnow(): - logger.error("--trash-at argument should be set in the future") + logger.error("--trash-at argument must be set in the future") sys.exit(1) if args.trash_after is not None: if args.trash_after < 1: - logger.error("--trash-after argument should be >= 1") + logger.error("--trash-after argument must be >= 1") sys.exit(1) trash_at = datetime.timedelta(seconds=(args.trash_after * 24 * 60 * 60)) @@ -1277,10 +1313,18 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr, output = ','.join(writer.data_locators()) else: try: + expiration_notice = "" + if writer.collection_trash_at() is not None: + # Get the local timezone-naive version, and log it with timezone information. + local_trash_at = writer.collection_trash_at().replace(tzinfo=None) - datetime.timedelta(seconds=time.timezone) + expiration_notice = ". It will expire on {} {}.".format( + local_trash_at.strftime("%Y-%m-%d %H:%M:%S"), time.strftime("%z")) if args.update_collection: - logger.info(u"Collection updated: '{}'".format(writer.collection_name())) + logger.info(u"Collection updated: '{}'{}".format( + writer.collection_name(), expiration_notice)) else: - logger.info(u"Collection saved as '{}'".format(writer.collection_name())) + logger.info(u"Collection saved as '{}'{}".format( + writer.collection_name(), expiration_notice)) if args.portable_data_hash: output = writer.portable_data_hash() else: