comparison gd_util.py @ 27:8997f2ca8c7a

Update to Miller Lab devshed revision bae0d3306d3b
author Richard Burhans <burhans@bx.psu.edu>
date Mon, 15 Jul 2013 10:47:35 -0400
parents
children
comparison
equal deleted inserted replaced
26:91e835060ad2 27:8997f2ca8c7a
1 #!/usr/bin/env python
2
3 import base64
4 import errno
5 import os
6 import subprocess
7 import sys
8 import zlib
9
10 ################################################################################
11
12 def die(message):
13 print >> sys.stderr, message
14 sys.exit(1)
15
16 ################################################################################
17
18 def mkdir_p(path):
19 try:
20 os.makedirs(path)
21 except OSError, e:
22 if e.errno <> errno.EEXIST:
23 raise
24
25 ################################################################################
26
27 def run_program(prog, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
28 kwargs = {
29 "bufsize": -1,
30 "close_fds": False,
31 "creationflags": 0,
32 "cwd": None,
33 "env": None,
34 "executable": prog,
35 "preexec_fn": None,
36 "shell": False,
37 "startupinfo": None,
38 "stderr": stderr,
39 "stdin": None,
40 "stdout": stdout,
41 "universal_newlines": False
42 }
43
44 str_args = [str(x) for x in args]
45
46 p = subprocess.Popen(str_args, **kwargs)
47 (stdoutdata, stderrdata) = p.communicate()
48 rc = p.returncode
49
50 if rc != 0:
51 die('FAILED:\n{0}\nCOMMAND:\n{1}'.format(stderrdata, ' '.join(str_args)))
52
53 return stdoutdata, stderrdata
54
55 ################################################################################
56
57 def unwrap_string(string):
58 try:
59 decoded_string = base64.b64decode(string)
60 except TypeError, message:
61 die('base64.b64decode: {0}: {1}'.format(message, string))
62
63 try:
64 return zlib.decompress(decoded_string)
65 except zlib.error, message:
66 die('zlib.decompress: {0}'.format(message))
67
68 ################################################################################
69
70 def wrap_string(string, level=9):
71 try:
72 compressed_string = zlib.compress(string, level)
73 except zlib.error, message:
74 die('zlib.compress: {0}'.format(message))
75 return base64.b64encode(compressed_string)
76
77 ################################################################################