-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
54 lines (46 loc) · 1.48 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import re
import subprocess
import signal
def strip_ext_gz(f):
return re.sub(r'\.gz$', '', str(f))
def run_shell_cmd(cmd):
p = subprocess.Popen(
['/bin/bash', '-o', 'pipefail'], # to catch error in pipe
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
preexec_fn=os.setsid) # to make a new process with a new PGID
pid = p.pid
pgid = os.getpgid(pid)
stdout, stderr = p.communicate(cmd)
rc = p.returncode
err_str = 'PID={}, PGID={}, RC={}\nSTDERR={}\nSTDOUT={}'.format(
pid, pgid, rc, stderr.strip(), stdout.strip())
if rc:
# kill all child processes
try:
os.killpg(pgid, signal.SIGKILL)
except:
pass
finally:
raise Exception(err_str)
return stdout.strip('\n')
def gunzip(f, suffix, out_dir):
if not f.endswith('.gz'):
raise Exception('Cannot gunzip a file without .gz extension.')
gunzipped = os.path.join(out_dir,
os.path.basename(strip_ext_gz(f)))
if suffix:
gunzipped += '.{}'.format(suffix)
# cmd = 'gzip -cd {} > {}'.format(f, gunzipped)
cmd = 'zcat -f {} > {}'.format(f, gunzipped)
run_shell_cmd(cmd)
return gunzipped
def rm_f(files):
if files:
if type(files) == list:
run_shell_cmd('rm -f {}'.format(' '.join(files)))
else:
run_shell_cmd('rm -f {}'.format(files))