forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (44 loc) · 1.4 KB
/
utils.py
File metadata and controls
61 lines (44 loc) · 1.4 KB
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
55
56
57
58
59
60
#!/usr/bin/env python3
import subprocess
import time
import os
class Process:
def __init__(self, command, logfile=None):
self.command = command
if logfile is not None:
self.logfile = open(logfile, "wb+")
try:
self.proc = subprocess.Popen(self.command.split(), stderr=subprocess.STDOUT, stdout=self.logfile)
except Exception:
print("run command failed: {}".format(self.command))
raise
def is_running(self):
try:
self.proc.wait(0)
except Exception:
pass
return self.proc.returncode is None
def kill(self):
if self.is_running():
self.proc.kill()
if self.logfile is not None:
self.logfile.close()
def wait(self):
self.proc.wait()
def get_pid(self):
return self.proc.pid
def kill_all(children=[]):
for p in children:
p.kill()
def check_alive(children=[], seconds=0):
if seconds != 0:
time.sleep(seconds)
for p in children:
if not p.is_running():
message = "process lost - {}".format(p.command)
raise Exception(message)
def do_command(command):
return subprocess.call(command.split())
if __name__ != "__main__":
os.environ["PATH"] += os.pathsep + os.getcwd()
os.environ["PATH"] += os.pathsep + os.path.abspath(os.path.join(os.getcwd(), "../bin"))