1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
6 This module provides the entire Python SDK for Arvados. The most useful modules
9 * arvados.api - After you `import arvados`, you can call `arvados.api.api` as
10 `arvados.api` to construct a client object.
12 * arvados.collection - The `arvados.collection.Collection` class provides a
13 high-level interface to read and write collections. It coordinates sending
14 data to and from Keep, and synchronizing updates with the collection object.
16 * arvados.util - Utility functions to use mostly in conjunction with the API
17 client object and the results it returns.
19 Other submodules provide lower-level functionality.
22 import logging as stdliblog
27 from collections import UserDict
29 from .api import api, api_from_config, http_cache
30 from .collection import CollectionReader, CollectionWriter, ResumableCollectionWriter
31 from arvados.keep import *
32 from arvados.stream import *
33 from .arvfile import StreamFileReader
34 from .logging import log_format, log_date_format, log_handler
35 from .retry import RetryLoop
36 import arvados.errors as errors
37 import arvados.util as util
39 # Override logging module pulled in via `from ... import *`
40 # so users can `import arvados.logging`.
41 logging = sys.modules['arvados.logging']
43 # Set up Arvados logging based on the user's configuration.
44 # All Arvados code should log under the arvados hierarchy.
45 logger = stdliblog.getLogger('arvados')
46 logger.addHandler(log_handler)
47 logger.setLevel(stdliblog.DEBUG if config.get('ARVADOS_DEBUG')
48 else stdliblog.WARNING)
50 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
51 def task_set_output(self, s, num_retries=5):
52 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0):
54 return api('v1').job_tasks().update(
61 except errors.ApiError as error:
62 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
63 logger.debug("task_set_output: job_tasks().update() raised {}, retrying with {} tries left".format(repr(error),tries_left))
68 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
69 def current_task(num_retries=5):
74 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
76 task = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
78 task.set_output = types.MethodType(task_set_output, task)
79 task.tmpdir = os.environ['TASK_WORK']
82 except errors.ApiError as error:
83 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
84 logger.debug("current_task: job_tasks().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
89 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
90 def current_job(num_retries=5):
95 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
97 job = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
99 job.tmpdir = os.environ['JOB_WORK']
102 except errors.ApiError as error:
103 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
104 logger.debug("current_job: jobs().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
108 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
109 def getjobparam(*args):
110 return current_job()['script_parameters'].get(*args)
112 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
113 def get_job_param_mount(*args):
114 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_job()['script_parameters'].get(*args))
116 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
117 def get_task_param_mount(*args):
118 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_task()['parameters'].get(*args))
120 class JobTask(object):
121 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
122 def __init__(self, parameters=dict(), runtime_constraints=dict()):
123 print("init jobtask %s %s" % (parameters, runtime_constraints))
125 class job_setup(object):
127 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
128 def one_task_per_input_file(if_sequence=0, and_end_task=True, input_as_path=False, api_client=None):
129 if if_sequence != current_task()['sequence']:
133 api_client = api('v1')
135 job_input = current_job()['script_parameters']['input']
136 cr = CollectionReader(job_input, api_client=api_client)
138 for s in cr.all_streams():
139 for f in s.all_files():
141 task_input = os.path.join(job_input, s.name(), f.name())
143 task_input = f.as_manifest()
145 'job_uuid': current_job()['uuid'],
146 'created_by_job_task_uuid': current_task()['uuid'],
147 'sequence': if_sequence + 1,
152 api_client.job_tasks().create(body=new_task_attrs).execute()
154 api_client.job_tasks().update(uuid=current_task()['uuid'],
155 body={'success':True}
160 @util._deprecated('3.0', 'arvados-cwl-runner or the containers API')
161 def one_task_per_input_stream(if_sequence=0, and_end_task=True):
162 if if_sequence != current_task()['sequence']:
164 job_input = current_job()['script_parameters']['input']
165 cr = CollectionReader(job_input)
166 for s in cr.all_streams():
167 task_input = s.tokens()
169 'job_uuid': current_job()['uuid'],
170 'created_by_job_task_uuid': current_task()['uuid'],
171 'sequence': if_sequence + 1,
176 api('v1').job_tasks().create(body=new_task_attrs).execute()
178 api('v1').job_tasks().update(uuid=current_task()['uuid'],
179 body={'success':True}