comparison venv/lib/python2.7/site-packages/planemo/io.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 from __future__ import print_function
2 import contextlib
3 import os
4 import shutil
5 import sys
6 import tempfile
7 import time
8
9 import click
10 from galaxy.tools.deps import commands
11 from galaxy.tools.deps.commands import which
12
13
14 def communicate(cmds, **kwds):
15 info(cmds)
16 p = commands.shell_process(cmds, **kwds)
17 ret_val = p.communicate()
18 if p.returncode != 0:
19 template = "Problem executing commands {0} - ({1}, {2})"
20 msg = template.format(cmds, ret_val[0], ret_val[1])
21 raise RuntimeError(msg)
22 return ret_val
23
24
25 def shell(cmds, **kwds):
26 info(cmds)
27 return commands.shell(cmds, **kwds)
28
29
30 def info(message, *args):
31 if args:
32 message = message % args
33 _echo(click.style(message, bold=True, fg='green'))
34
35
36 def can_write_to_path(path, **kwds):
37 if not kwds["force"] and os.path.exists(path):
38 error("%s already exists, exiting." % path)
39 return False
40 return True
41
42
43 def error(message, *args):
44 if args:
45 message = message % args
46 _echo(click.style(message, bold=True, fg='red'), err=True)
47
48
49 def warn(message, *args):
50 if args:
51 message = message % args
52 _echo(click.style(message, fg='red'), err=True)
53
54
55 def _echo(message, err=False):
56 if sys.version_info[0] == 2:
57 click.echo(message, err=err)
58 else:
59 print(message)
60
61
62 def write_file(path, content):
63 with open(path, "w") as f:
64 f.write(content)
65
66
67 def untar_to(url, path, tar_args):
68 if which("wget"):
69 download_cmd = "wget -q --recursive -O - '%s'"
70 else:
71 download_cmd = "curl '%s'"
72 download_cmd = download_cmd % url
73 if tar_args:
74 if not os.path.exists(path):
75 os.makedirs(path)
76
77 untar_cmd = "tar %s" % tar_args
78 shell("%s | %s" % (download_cmd, untar_cmd))
79 else:
80 shell("%s > '%s'" % (download_cmd, path))
81
82
83 @contextlib.contextmanager
84 def temp_directory(prefix="planemo_tmp_"):
85 temp_dir = tempfile.mkdtemp(prefix=prefix)
86 try:
87 yield temp_dir
88 finally:
89 shutil.rmtree(temp_dir)
90
91
92 def kill_pid_file(pid_file):
93 if not os.path.exists(pid_file):
94 return
95
96 pid = int(open(pid_file, "r").read())
97 kill_posix(pid)
98
99
100 def kill_posix(pid):
101 def _check_pid():
102 try:
103 os.kill(pid, 0)
104 return True
105 except OSError:
106 return False
107
108 if _check_pid():
109 for sig in [15, 9]:
110 try:
111 os.kill(pid, sig)
112 except OSError:
113 return
114 time.sleep(1)
115 if not _check_pid():
116 return