Merge branch '8784-dir-listings'
[arvados.git] / crunch_scripts / decompress-all.py
1 #!/usr/bin/env python
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 #
7 # decompress-all.py
8 #
9 # Decompress all compressed files in the collection using the "dtrx" tool and
10 # produce a new collection with the contents.  Uncompressed files
11 # are passed through.
12 #
13 # input:
14 # A collection at script_parameters["input"]
15 #
16 # output:
17 # A manifest of the uncompressed contents of the input collection.
18
19 import arvados
20 import re
21 import subprocess
22 import os
23 import sys
24 import crunchutil.robust_put as robust_put
25
26 arvados.job_setup.one_task_per_input_file(if_sequence=0, and_end_task=True,
27                                           input_as_path=True)
28
29 task = arvados.current_task()
30
31 input_file = task['parameters']['input']
32
33 infile_parts = re.match(r"(^[a-f0-9]{32}\+\d+)(\+\S+)*(/.*)?(/[^/]+)$", input_file)
34
35 outdir = os.path.join(task.tmpdir, "output")
36 os.makedirs(outdir)
37 os.chdir(outdir)
38
39 if infile_parts is None:
40     print >>sys.stderr, "Failed to parse input filename '%s' as a Keep file\n" % input_file
41     sys.exit(1)
42
43 cr = arvados.CollectionReader(infile_parts.group(1))
44 streamname = infile_parts.group(3)[1:]
45 filename = infile_parts.group(4)[1:]
46
47 if streamname is not None:
48     subprocess.call(["mkdir", "-p", streamname])
49     os.chdir(streamname)
50 else:
51     streamname = '.'
52
53 m = re.match(r'.*\.(gz|Z|bz2|tgz|tbz|zip|rar|7z|cab|deb|rpm|cpio|gem)$', arvados.get_task_param_mount('input'), re.IGNORECASE)
54
55 if m is not None:
56     rc = subprocess.call(["dtrx", "-r", "-n", "-q", arvados.get_task_param_mount('input')])
57     if rc == 0:
58         task.set_output(robust_put.upload(outdir))
59     else:
60         sys.exit(rc)
61 else:
62     streamreader = filter(lambda s: s.name() == streamname, cr.all_streams())[0]
63     filereader = streamreader.files()[filename]
64     task.set_output(streamname + filereader.as_manifest()[1:])