X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1d77dec0f7bc1cca34d52288bd39ecd0a79be250..95e5ccacf6c1193b313fa90a6d39baafa2ba67d8:/sdk/python/arvados/commands/run.py?ds=sidebyside diff --git a/sdk/python/arvados/commands/run.py b/sdk/python/arvados/commands/run.py index 216b512085..96f5bdd44a 100644 --- a/sdk/python/arvados/commands/run.py +++ b/sdk/python/arvados/commands/run.py @@ -1,3 +1,20 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# Copyright (C) 2018 Genome Research Ltd. +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from __future__ import print_function from __future__ import absolute_import from builtins import range @@ -18,6 +35,7 @@ import sys import errno import arvados.commands._util as arv_cmd import arvados.collection +import arvados.config as config from arvados._version import __version__ @@ -108,7 +126,7 @@ def determine_project(root, current_user): # ArvFile() (file already exists in a collection), UploadFile() (file needs to # be uploaded to a collection), or simply returns prefix+fn (which yields the # original parameter string). -def statfile(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)"): +def statfile(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False): absfn = os.path.abspath(fn) try: st = os.stat(absfn) @@ -125,27 +143,28 @@ def statfile(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)"): # trim leading '/' for path prefix test later return UploadFile(prefix, absfn[1:]) except OSError as e: - if e.errno == errno.ENOENT: + if e.errno == errno.ENOENT and not raiseOSError: pass else: raise return prefix+fn -def write_file(collection, pathprefix, fn): +def write_file(collection, pathprefix, fn, flush=False): with open(os.path.join(pathprefix, fn)) as src: dst = collection.open(fn, "w") r = src.read(1024*128) while r: dst.write(r) r = src.read(1024*128) - dst.close(flush=False) + dst.close(flush=flush) def uploadfiles(files, api, dry_run=False, num_retries=0, project=None, fnPattern="$(file %s/%s)", name=None, - collection=None): + collection=None, + packed=True): # Find the smallest path prefix that includes all the files that need to be uploaded. # This starts at the root and iteratively removes common parent directory prefixes # until all file paths no longer have a common parent. @@ -195,29 +214,55 @@ def uploadfiles(files, api, dry_run=False, num_retries=0, continue prev = localpath if os.path.isfile(localpath): - write_file(collection, pathprefix, f.fn) + write_file(collection, pathprefix, f.fn, not packed) elif os.path.isdir(localpath): for root, dirs, iterfiles in os.walk(localpath): root = root[len(pathprefix):] for src in iterfiles: - write_file(collection, pathprefix, os.path.join(root, src)) - - filters=[["portable_data_hash", "=", collection.portable_data_hash()]] - if name: - filters.append(["name", "like", name+"%"]) - if project: - filters.append(["owner_uuid", "=", project]) - - exists = api.collections().list(filters=filters, limit=1).execute(num_retries=num_retries) - - if exists["items"]: - item = exists["items"][0] - pdh = item["portable_data_hash"] - logger.info("Using collection %s (%s)", pdh, item["uuid"]) - elif len(collection) > 0: - collection.save_new(name=name, owner_uuid=project, ensure_unique_name=True) + write_file(collection, pathprefix, os.path.join(root, src), not packed) + + pdh = None + if len(collection) > 0: + # non-empty collection + filters = [["portable_data_hash", "=", collection.portable_data_hash()]] + name_pdh = "%s (%s)" % (name, collection.portable_data_hash()) + if name: + filters.append(["name", "=", name_pdh]) + if project: + filters.append(["owner_uuid", "=", project]) + + # do the list / create in a loop with up to 2 tries as we are using `ensure_unique_name=False` + # and there is a potential race with other workflows that may have created the collection + # between when we list it and find it does not exist and when we attempt to create it. + tries = 2 + while pdh is None and tries > 0: + exists = api.collections().list(filters=filters, limit=1).execute(num_retries=num_retries) + + if exists["items"]: + item = exists["items"][0] + pdh = item["portable_data_hash"] + logger.info("Using collection %s (%s)", pdh, item["uuid"]) + else: + try: + collection.save_new(name=name_pdh, owner_uuid=project, ensure_unique_name=False) + pdh = collection.portable_data_hash() + logger.info("Uploaded to %s (%s)", pdh, collection.manifest_locator()) + except arvados.errors.ApiError as ae: + tries -= 1 + if pdh is None: + # Something weird going on here, probably a collection + # with a conflicting name but wrong PDH. We won't + # able to reuse it but we still need to save our + # collection, so so save it with unique name. + logger.info("Name conflict on '%s', existing collection has an unexpected portable data hash", name_pdh) + collection.save_new(name=name_pdh, owner_uuid=project, ensure_unique_name=True) + pdh = collection.portable_data_hash() + logger.info("Uploaded to %s (%s)", pdh, collection.manifest_locator()) + else: + # empty collection pdh = collection.portable_data_hash() - logger.info("Uploaded to %s (%s)", pdh, collection.manifest_locator()) + assert (pdh == config.EMPTY_BLOCK_LOCATOR), "Empty collection portable_data_hash did not have expected locator, was %s" % pdh + logger.info("Using empty collection %s", pdh) for c in files: c.keepref = "%s/%s" % (pdh, c.fn)