-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
110 lines (88 loc) · 2.91 KB
/
model.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import logging
import subprocess
import json
class Job(object):
PROTOCOL_FLAG = "JOB"
@staticmethod
def readDesc(desc):
desc = json.loads(desc)
job = Job(desc["name"], desc["metadata"])
job.addCommands([" ".join(command) for command in desc["commands"]])
return job
def __init__(self, name, metadata=None):
self._name = name
self.commands = []
if not metadata:
metadata = {}
self.metadata = metadata
self.proc = None
logging.getLogger(self.__class__.__name__).setLevel(logging.INFO)
def isRunning(self):
if not self.proc:
return False
if self.proc.poll() is None:
return True
self.notifyEnded()
return False
def killProc(self):
if not self.isRunning():
return
self.proc.kill()
self.notifyEnded()
def notifyEnded(self):
if not self.proc:
logging.getLogger(self.__class__.__name__).warning(
"The method Job.notifyEndend has been called on a procless job"
)
return
if self.proc.returncode is None:
logging.getLogger(self.__class__.__name__).warning(
"The method Job.notifyEndend has been called on a running job"
)
return
# Clean proc attribute
proc = self.proc
self.proc = None
# Setup log message
if proc.returncode != 0:
# Error warning
state = "in error (rcode {rcode})".format(proc.returncode)
logLevel = logging.WARNING
else:
# Done info
state = "done"
logLevel = logging.INFO
# The outputs
stdout = proc.stdout.read()
stderr = proc.stderr.read()
message = "\n".join(
[
"The Job {name} is {state}.",
"\t-stdout:\n{stdout}.",
"\t-stderr:\n{stderr}",
]
).format(name=self.name, state=state, stdout=stdout, stderr=stderr)
logging.getLogger(self.__class__.__name__).log(logLevel, message)
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
def writeDesc(self):
desc = {
"commands": [command.split(" ") for command in self.commands],
"name": self.name,
"metadata": self.metadata,
}
return json.dumps(desc)
def addCommands(self, commands):
if not isinstance(commands, list):
commands = [commands]
self.commands.extend(commands)
def execute(self):
self.commands = [command.format(**self.metadata) for command in self.commands]
commandLine = "; ".join(self.commands)
self.proc = subprocess.Popen(
commandLine, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE
)