Repository navigation
Expand file tree
/
Copy pathjobController.py
More file actions
215 lines (181 loc) · 6.76 KB
/
Copy pathjobController.py
File metadata and controls
215 lines (181 loc) · 6.76 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
str_description = """
This module provides some very simple shell-based job running
methods.
"""
import subprocess
import os
import json
import time
from pathlib import Path
from datetime import datetime
import uuid
import ast
class jobber:
def __init__(self, d_args : dict):
"""Constructor for the jobber class.
Args:
d_args (dict): a dictionary of "arguments" (parameters) for the
object.
"""
self.args = d_args.copy()
self.execCmd:Path = Path('somefile.cmd')
self.histlogPath:Path = Path('/tmp')
if not 'verbosity' in self.args.keys(): self.args['verbosity'] = 0
if not 'noJobLogging' in self.args.keys(): self.args['noJobLogging'] = False
def v2JSONcli(self, v:str) -> str:
"""
attempts to convert a JSON string serialization explicitly into a string
with enclosed single quotes. If the input is not a valid JSON string, simply
return it unchanged.
An input string of
'{ "key1": "value1", "key2": N, "key3": true }'
is explicitly enclosed in embedded single quotes:
'\'{ "key1": "value1", "key2": N, "key3": true }\''
args:
v(str): a value to process
returns:
str: cli equivalent string.
"""
vb:str = ""
try:
d_dict = json.loads(v)
vb = f"'{v}'"
except:
vb = '%s' % v
return vb
def dict2cli(self, d_dict : dict) -> str:
"""convert a dictionary into a cli conformant json string.
an input dictionary of
{
'key1': 'value1',
'key2': 'value2',
'key3': true,
'key4': false
}
is converted to a string:
"--key1 value1 --key2 value2 --key3"
args:
d_dict (dict): a python dictionary to convert
returns:
str: cli equivalent string.
"""
str_cli : str = ""
for k,v in d_dict.items():
if type(v) == bool:
if v:
str_cli += '--%s ' % k
elif len(v):
v = self.v2JSONcli(v)
str_cli += '--%s %s ' % (k, v)
return str_cli
def job_run(self, str_cmd: str):
"""
running some cli process via python is cumbersome. the typical/easy
path of
os.system(str_cmd)
is deprecated and prone to hidden complexity. The preferred
method is via subprocess, which has a cumbersome processing
syntax. Still, this method runs the `str_cmd` and returns the
stderr and stdout strings as well as a returncode.
Providing readtime output of both stdout and stderr seems
problematic. The approach here is to provide realtime
output on stdout and only provide stderr on process completion.
"""
d_ret : dict = {
'stdout': "",
'stderr': "",
'cmd': "",
'cwd': "",
'returncode': 0
}
str_stdoutline : str = ""
str_stdout : str = ""
p = subprocess.Popen(
str_cmd.split(),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
)
# realtime output on stdout
while True:
stdout = p.stdout.readline()
if p.poll() is not None:
break
if stdout:
str_stdoutline = stdout.decode()
if int(self.args['verbosity']):
print(str_stdoutline, end = '')
str_stdout += str_stdoutline
d_ret['cmd'] = str_cmd
d_ret['cwd'] = os.getcwd()
d_ret['stdout'] = str_stdout
d_ret['stderr'] = p.stderr.read().decode()
d_ret['returncode'] = p.returncode
if int(self.args['verbosity']) and len(d_ret['stderr']):
print('\nstderr: \n%s' % d_ret['stderr'])
return d_ret
def job_runbg(self, str_cmd : str) -> dict:
"""run a job in the background.
after much (probably unecessary pain) the best solution seemed to
be:
* create a shell script on the fs that contains the
<str_cmd> and a "&"
* run the shell script in subprocess.popen
args:
str_cmd (str): cli string to run
returns:
dict: a dictionary of exec state
"""
def txscript_content(message:str) -> str:
str_script:str = ""
str_script = f"""#!/bin/bash
{message}
"""
str_script = ''.join(str_script.split(r'\r'))
return str_script
def txscript_save(str_content) -> None:
with open(self.execCmd, "w") as f:
f.write(f'%s' % str_content)
self.execCmd.chmod(0o755)
def execstr_build(input:Path) -> str:
""" the histlogPath might have spaces, esp on non-Linux systems """
ret:str = ""
t_parts:tuple = input.parts
ret = '/'.join(['"{0}"'.format(arg) if ' ' in arg else arg for arg in t_parts])
return ret
baseFileName:str = f"job-{uuid.uuid4().hex}"
self.execCmd = logHistoryPath_create() / Path(baseFileName + ".sh")
d_ret:dict = {
'uid' : "",
'cmd' : "",
'cwd' : "",
'script' : self.execCmd
}
# pudb.set_trace()
str_cmd += " &"
txscript_save(txscript_content(str_cmd))
execCmd:str = execstr_build(self.execCmd)
process = subprocess.Popen(
execCmd.split(),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
close_fds = True
)
#self.execCmd.unlink()
d_ret['uid'] = str(os.getuid())
d_ret['cmd'] = str_cmd
d_ret['cwd'] = os.getcwd()
return d_ret
def job_stdwrite(self, d_job : dict, str_outputDir : str, str_prefix : str = "") -> dict:
"""
Capture the d_job entries to respective files.
"""
if not self.args['noJobLogging']:
for key in d_job.keys():
with open(
'%s/%s%s' % (str_outputDir, str_prefix, key), "w"
) as f:
f.write(str(d_job[key]))
f.close()
return {
'status': True
}