1 # Copyright (C) The Arvados Authors. All rights reserved.
2 # Copyright (C) 2018 Genome Research Ltd.
4 # SPDX-License-Identifier: Apache-2.0
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 from __future__ import print_function
19 from __future__ import absolute_import
20 from builtins import range
21 from past.builtins import basestring
22 from builtins import object
24 import arvados.commands.ws as ws
36 import arvados.commands._util as arv_cmd
37 import arvados.collection
38 import arvados.config as config
40 from arvados._version import __version__
42 logger = logging.getLogger('arvados.arv-run')
43 logger.setLevel(logging.INFO)
45 arvrun_parser = argparse.ArgumentParser(parents=[arv_cmd.retry_opt])
46 arvrun_parser.add_argument('--dry-run', action="store_true",
47 help="Print out the pipeline that would be submitted and exit")
48 arvrun_parser.add_argument('--local', action="store_true",
49 help="Run locally using arv-run-pipeline-instance")
50 arvrun_parser.add_argument('--docker-image', type=str,
51 help="Docker image to use, otherwise use instance default.")
52 arvrun_parser.add_argument('--ignore-rcode', action="store_true",
53 help="Commands that return non-zero return codes should not be considered failed.")
54 arvrun_parser.add_argument('--no-reuse', action="store_true",
55 help="Do not reuse past jobs.")
56 arvrun_parser.add_argument('--no-wait', action="store_true",
57 help="Do not wait and display logs after submitting command, just exit.")
58 arvrun_parser.add_argument('--project-uuid', type=str,
59 help="Parent project of the pipeline")
60 arvrun_parser.add_argument('--git-dir', type=str, default="",
61 help="Git repository passed to arv-crunch-job when using --local")
62 arvrun_parser.add_argument('--repository', type=str, default="arvados",
63 help="repository field of component, default 'arvados'")
64 arvrun_parser.add_argument('--script-version', type=str, default="master",
65 help="script_version field of component, default 'master'")
66 arvrun_parser.add_argument('--version', action='version',
67 version="%s %s" % (sys.argv[0], __version__),
68 help='Print version and exit.')
69 arvrun_parser.add_argument('args', nargs=argparse.REMAINDER)
71 class ArvFile(object):
72 def __init__(self, prefix, fn):
77 return (self.prefix+self.fn).__hash__()
79 def __eq__(self, other):
80 return (self.prefix == other.prefix) and (self.fn == other.fn)
82 class UploadFile(ArvFile):
85 # Determine if a file is in a collection, and return a tuple consisting of the
86 # portable data hash and the path relative to the root of the collection.
87 # Return None if the path isn't with an arv-mount collection or there was is error.
88 def is_in_collection(root, branch):
92 fn = os.path.join(root, ".arvados#collection")
93 if os.path.exists(fn):
94 with file(fn, 'r') as f:
96 return (c["portable_data_hash"], branch)
98 sp = os.path.split(root)
99 return is_in_collection(sp[0], os.path.join(sp[1], branch))
100 except (IOError, OSError):
103 # Determine the project to place the output of this command by searching upward
104 # for arv-mount psuedofile indicating the project. If the cwd isn't within
105 # an arv-mount project or there is an error, return current_user.
106 def determine_project(root, current_user):
110 fn = os.path.join(root, ".arvados#project")
111 if os.path.exists(fn):
112 with file(fn, 'r') as f:
114 if 'writable_by' in c and current_user in c['writable_by']:
119 sp = os.path.split(root)
120 return determine_project(sp[0], current_user)
121 except (IOError, OSError):
124 # Determine if string corresponds to a file, and if that file is part of a
125 # arv-mounted collection or only local to the machine. Returns one of
126 # ArvFile() (file already exists in a collection), UploadFile() (file needs to
127 # be uploaded to a collection), or simply returns prefix+fn (which yields the
128 # original parameter string).
129 def statfile(prefix, fn, fnPattern="$(file %s/%s)", dirPattern="$(dir %s/%s/)", raiseOSError=False):
130 absfn = os.path.abspath(fn)
133 sp = os.path.split(absfn)
134 (pdh, branch) = is_in_collection(sp[0], sp[1])
136 if stat.S_ISREG(st.st_mode):
137 return ArvFile(prefix, fnPattern % (pdh, branch))
138 elif stat.S_ISDIR(st.st_mode):
139 return ArvFile(prefix, dirPattern % (pdh, branch))
141 raise Exception("%s is not a regular file or directory" % absfn)
143 # trim leading '/' for path prefix test later
144 return UploadFile(prefix, absfn[1:])
146 if e.errno == errno.ENOENT and not raiseOSError:
153 def write_file(collection, pathprefix, fn, flush=False):
154 with open(os.path.join(pathprefix, fn), "rb") as src:
155 dst = collection.open(fn, "wb")
156 r = src.read(1024*128)
159 r = src.read(1024*128)
160 dst.close(flush=flush)
162 def uploadfiles(files, api, dry_run=False, num_retries=0,
164 fnPattern="$(file %s/%s)",
168 # Find the smallest path prefix that includes all the files that need to be uploaded.
169 # This starts at the root and iteratively removes common parent directory prefixes
170 # until all file paths no longer have a common parent.
180 # no parent directories left
183 # path step takes next directory
184 pathstep = sp[0] + "/"
186 # check if pathstep is common prefix for all files
187 if not c.fn.startswith(pathstep):
191 # pathstep is common parent directory for all files, so remove the prefix
193 pathprefix += pathstep
195 c.fn = c.fn[len(pathstep):]
197 logger.info("Upload local files: \"%s\"", '" "'.join([c.fn for c in files]))
200 logger.info("$(input) is %s", pathprefix.rstrip('/'))
203 files = sorted(files, key=lambda x: x.fn)
204 if collection is None:
205 collection = arvados.collection.Collection(api_client=api, num_retries=num_retries)
208 localpath = os.path.join(pathprefix, f.fn)
209 if prev and localpath.startswith(prev+"/"):
210 # If this path is inside an already uploaded subdirectory,
211 # don't redundantly re-upload it.
212 # e.g. we uploaded /tmp/foo and the next file is /tmp/foo/bar
213 # skip it because it starts with "/tmp/foo/"
216 if os.path.isfile(localpath):
217 write_file(collection, pathprefix, f.fn, not packed)
218 elif os.path.isdir(localpath):
219 for root, dirs, iterfiles in os.walk(localpath):
220 root = root[len(pathprefix):]
221 for src in iterfiles:
222 write_file(collection, pathprefix, os.path.join(root, src), not packed)
225 if len(collection) > 0:
226 # non-empty collection
227 filters = [["portable_data_hash", "=", collection.portable_data_hash()]]
228 name_pdh = "%s (%s)" % (name, collection.portable_data_hash())
230 filters.append(["name", "=", name_pdh])
232 filters.append(["owner_uuid", "=", project])
234 # do the list / create in a loop with up to 2 tries as we are using `ensure_unique_name=False`
235 # and there is a potential race with other workflows that may have created the collection
236 # between when we list it and find it does not exist and when we attempt to create it.
238 while pdh is None and tries > 0:
239 exists = api.collections().list(filters=filters, limit=1).execute(num_retries=num_retries)
242 item = exists["items"][0]
243 pdh = item["portable_data_hash"]
244 logger.info("Using collection %s (%s)", pdh, item["uuid"])
247 collection.save_new(name=name_pdh, owner_uuid=project, ensure_unique_name=False)
248 pdh = collection.portable_data_hash()
249 logger.info("Uploaded to %s (%s)", pdh, collection.manifest_locator())
250 except arvados.errors.ApiError as ae:
253 # Something weird going on here, probably a collection
254 # with a conflicting name but wrong PDH. We won't
255 # able to reuse it but we still need to save our
256 # collection, so so save it with unique name.
257 logger.info("Name conflict on '%s', existing collection has an unexpected portable data hash", name_pdh)
258 collection.save_new(name=name_pdh, owner_uuid=project, ensure_unique_name=True)
259 pdh = collection.portable_data_hash()
260 logger.info("Uploaded to %s (%s)", pdh, collection.manifest_locator())
263 pdh = collection.portable_data_hash()
264 assert (pdh == config.EMPTY_BLOCK_LOCATOR), "Empty collection portable_data_hash did not have expected locator, was %s" % pdh
265 logger.info("Using empty collection %s", pdh)
268 c.keepref = "%s/%s" % (pdh, c.fn)
269 c.fn = fnPattern % (pdh, c.fn)
272 def main(arguments=None):
273 args = arvrun_parser.parse_args(arguments)
275 if len(args.args) == 0:
276 arvrun_parser.print_help()
279 starting_args = args.args
283 # Parse the command arguments into 'slots'.
284 # All words following '>' are output arguments and are collected into slots[0].
285 # All words following '<' are input arguments and are collected into slots[1].
286 # slots[2..] store the parameters of each command in the pipeline.
288 # e.g. arv-run foo arg1 arg2 '|' bar arg3 arg4 '<' input1 input2 input3 '>' output.txt
289 # will be parsed into:
291 # ['input1', 'input2', 'input3'],
292 # ['foo', 'arg1', 'arg2'],
293 # ['bar', 'arg3', 'arg4']]
296 if c.startswith('>'):
299 slots[reading_into].append(c[1:])
300 elif c.startswith('<'):
303 slots[reading_into].append(c[1:])
305 reading_into = len(slots)
308 slots[reading_into].append(c)
310 if slots[0] and len(slots[0]) > 1:
311 logger.error("Can only specify a single stdout file (run-command substitutions are permitted)")
315 api = arvados.api('v1')
316 if args.project_uuid:
317 project = args.project_uuid
319 project = determine_project(os.getcwd(), api.users().current().execute()["uuid"])
321 # Identify input files. Look at each parameter and test to see if there is
322 # a file by that name. This uses 'patterns' to look for within
323 # command line arguments, such as --foo=file.txt or -lfile.txt
324 patterns = [re.compile("([^=]+=)(.*)"),
325 re.compile("(-[A-Za-z])(.+)")]
326 for j, command in enumerate(slots[1:]):
327 for i, a in enumerate(command):
329 # j == 0 is stdin, j > 0 is commands
330 # always skip program executable (i == 0) in commands
332 elif a.startswith('\\'):
333 # if it starts with a \ then don't do any interpretation
336 # See if it looks like a file
337 command[i] = statfile('', a)
339 # If a file named command[i] was found, it would now be an
340 # ArvFile or UploadFile. If command[i] is a basestring, that
341 # means it doesn't correspond exactly to a file, so do some
343 if isinstance(command[i], basestring):
347 command[i] = statfile(m.group(1), m.group(2))
350 files = [c for command in slots[1:] for c in command if isinstance(c, UploadFile)]
352 uploadfiles(files, api, dry_run=args.dry_run, num_retries=args.retries, project=project)
354 for i in range(1, len(slots)):
355 slots[i] = [("%s%s" % (c.prefix, c.fn)) if isinstance(c, ArvFile) else c for c in slots[i]]
358 "script": "run-command",
359 "script_version": args.script_version,
360 "repository": args.repository,
361 "script_parameters": {
363 "runtime_constraints": {}
366 if args.docker_image:
367 component["runtime_constraints"]["docker_image"] = args.docker_image
370 group_parser = argparse.ArgumentParser()
371 group_parser.add_argument('-b', '--batch-size', type=int)
372 group_parser.add_argument('args', nargs=argparse.REMAINDER)
374 for s in range(2, len(slots)):
375 for i in range(0, len(slots[s])):
376 if slots[s][i] == '--':
377 inp = "input%i" % (s-2)
378 groupargs = group_parser.parse_args(slots[2][i+1:])
379 if groupargs.batch_size:
380 component["script_parameters"][inp] = {"value": {"batch":groupargs.args, "size":groupargs.batch_size}}
381 slots[s] = slots[s][0:i] + [{"foreach": inp, "command": "$(%s)" % inp}]
383 component["script_parameters"][inp] = groupargs.args
384 slots[s] = slots[s][0:i] + ["$(%s)" % inp]
385 task_foreach.append(inp)
387 if slots[s][i] == '\--':
391 component["script_parameters"]["task.stdout"] = slots[0][0]
393 task_foreach.append("stdin")
394 component["script_parameters"]["stdin"] = slots[1]
395 component["script_parameters"]["task.stdin"] = "$(stdin)"
398 component["script_parameters"]["task.foreach"] = task_foreach
400 component["script_parameters"]["command"] = slots[2:]
401 if args.ignore_rcode:
402 component["script_parameters"]["task.ignore_rcode"] = args.ignore_rcode
405 "name": "arv-run " + " | ".join([s[0] for s in slots[2:]]),
406 "description": "@" + " ".join(starting_args) + "@",
410 "state": "RunningOnClient" if args.local else "RunningOnServer"
414 print(json.dumps(pipeline, indent=4))
416 pipeline["owner_uuid"] = project
417 pi = api.pipeline_instances().create(body=pipeline, ensure_unique_name=True).execute()
418 logger.info("Running pipeline %s", pi["uuid"])
421 subprocess.call(["arv-run-pipeline-instance", "--instance", pi["uuid"], "--run-jobs-here"] + (["--no-reuse"] if args.no_reuse else []))
422 elif not args.no_wait:
423 ws.main(["--pipeline", pi["uuid"]])
425 pi = api.pipeline_instances().get(uuid=pi["uuid"]).execute()
426 logger.info("Pipeline is %s", pi["state"])
427 if "output_uuid" in pi["components"]["command"]:
428 logger.info("Output is %s", pi["components"]["command"]["output_uuid"])
430 logger.info("No output")
432 if __name__ == '__main__':