Merge branch '22154-included-kind'
[arvados.git] / sdk / cwl / arvados_cwl / util.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import datetime
6 from arvados.errors import ApiError
7
8 collectionUUID =  "http://arvados.org/cwl#collectionUUID"
9
10 def get_intermediate_collection_info(workflow_step_name, current_container, intermediate_output_ttl):
11         if workflow_step_name:
12             name = "Intermediate collection for step %s" % (workflow_step_name)
13         else:
14             name = "Intermediate collection"
15         trash_time = None
16         if intermediate_output_ttl > 0:
17             trash_time = datetime.datetime.utcnow() + datetime.timedelta(seconds=intermediate_output_ttl)
18         container_uuid = None
19         props = {"type": "intermediate"}
20         if current_container:
21             props["container_uuid"] = current_container['uuid']
22
23         return {"name" : name, "trash_at" : trash_time, "properties" : props}
24
25 def get_current_container(api, num_retries=0, logger=None):
26     current_container = None
27     try:
28         current_container = api.containers().current().execute(num_retries=num_retries)
29     except ApiError as e:
30         # Status code 404 just means we're not running in a container.
31         if e.resp.status != 404:
32             if logger:
33                 logger.info("Getting current container: %s", e)
34             raise e
35
36     return current_container
37
38
39 def common_prefix(firstfile, all_files):
40     common_parts = firstfile.split('/')
41     common_parts[-1] = ''
42     for f in all_files:
43         f_parts = f.split('/')
44         for index, (a, b) in enumerate(zip(common_parts, f_parts)):
45             if a != b:
46                 common_parts = common_parts[:index + 1]
47                 common_parts[-1] = ''
48                 break
49         if not any(common_parts):
50             break
51     return '/'.join(common_parts)