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