-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyxargs
executable file
·224 lines (175 loc) · 6.11 KB
/
pyxargs
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
216
217
218
219
220
221
222
223
224
#!/usr/bin/python3.11
import re
import subprocess as shell
import sys
import os
import json
import tempfile
from argparse import ArgumentParser
class Shell:
def __init__(self, cmds, timeout=1, tries=5):
self.cmds = cmds
self.procs = {}
self.status = {}
self.timeout = timeout
self.tries = tries
def run(self) -> None:
for cmd in self.cmds:
pipe = shell.PIPE
self.procs[cmd] = shell.Popen([cmd], shell=True, stdout=pipe, stderr=pipe)
def is_complete(self):
returned = [x for x in self.status.keys() if self.status[x]["returncode"]]
return len(returned) == len(self.cmds)
def update_status(self, cmd, prop, value):
if not self.status.get(cmd):
self.status[cmd] = {"returncode": False, "error": False, "output": False}
self.status[cmd][prop] = value
def wait_once(self, cmds: list) -> bool:
while len(cmds) > 0:
current = cmds[0]
cmds = cmds[1:]
proc = self.procs[current]
timeout = self.timeout
try:
out, errs = proc.communicate(timeout=timeout)
self.update_status(current, "returncode", proc.returncode)
if proc.returncode == 0:
self.update_status(
current, "output", out.decode().strip().split("\n")
)
elif errs:
errs = errs.decode().strip()
if len(errs) > 0:
self.update_status(
current, "error", errs.split("\n")
)
else:
self.update_status(current, "error", False)
self.update_status(current, "output", False)
except shell.TimeoutExpired:
pass
return self.is_complete()
def wait(self) -> bool:
tries = self.tries
cmds = self.cmds
current_try = 0
while current_try != tries:
if self.wait_once(cmds):
break
else:
cmds = [x for x in cmds if not self.status.get(x)]
current_try = current_try + 1
return self.is_complete()
@staticmethod
def main(cmds, timeout=1, tries=5):
runner = Shell(cmds, timeout=timeout, tries=tries)
runner.run()
runner.wait()
return runner.status
def default_placeholders(x: str) -> dict:
basename = os.path.basename(x)
abspath = os.path.abspath(x)
dirname = os.path.dirname(x)
return dict(basename=basename, abspath=abspath, dirname=dirname)
def add_default_subs(xs: list) -> list:
out = []
for x in xs:
out.append(x.format(**default_placeholders(x)))
return out
def write_output(output: bytes):
with tempfile.NamedTemporaryFile(delete=False) as fh:
fh.write(output)
print(f"output written to", fh.name)
def parse_cmdline() -> tuple[tuple, dict]:
parser = ArgumentParser("python equivalent for xargs")
add = parser.add_argument
add(
"-x",
"--cmd",
nargs="+",
required=True,
help="command with python placeholder strings",
)
add(
"-t",
"--timeout",
default=10,
help="default timeout to check for polling for output",
)
add("-p", "--path", help="read lines for path")
add("-i", "--stdin", action="store_true", help="read lines from stdin (default)")
add("-/" ,"--replace", nargs="+", help="placeholder definitions")
add("-d", "--sep", nargs=1, help='record separation (default: "\n")', default="\n")
add("--stdout", action="store_true", help="print output as json")
add(
"-o",
"--dest",
nargs=1,
help="save output to path",
metavar="PATH",
default=False,
)
args: dict = vars(parser.parse_args())
definitions: list = args.get("defs", [])
defs: dict = {}
l = len(definitions)
path: str = args["path"]
sep: str = args["sep"][0]
records: list[str] = []
timeout: int = int(args["timeout"])
cmd: str = (" ").join(args["cmd"])
cmds: list[str] = []
failed = []
if path:
if not os.path.isfile(path):
raise ValueError(f"invalid path {path}")
else:
with open(path) as fh:
records = fh.read().split(sep)
elif not args["stdin"]:
raise ValueError(f"no records provided. use -i or pass records through stdin")
else:
records = sys.stdin.read().split(sep)
if l == 0:
cmds = add_default_subs(records)
elif l % 2 != 0:
raise ValueError(f"expected an even number of args, got {l}")
else:
def parse_cmds(cmd: str, recs: list, defs: dict) -> tuple[list, list]:
out = []
for i, x in enumerate(recs):
found = default_placeholders(x)
items = defs.items()
fails = 0
for var, r in items:
r = re.compile(r)
if m := re.search(r, x):
found[var] = m.group()
else:
fails = fails + 1
if fails != len(items):
out.append(cmd.format(**found))
else:
failed.append((i, x))
return (out, failed)
placeholder: list = [definitions[i] for i in range(l) if i % 2 == 0]
sub: list = [definitions[i] for i in range(l) if i % 2 != 0]
defs = dict(zip(placeholder, sub))
(cmds, failed) = parse_cmds(cmd, records, defs)
return (
(cmds, failed),
{"timeout": timeout, "stdout": args["stdout"], "dest": args["dest"]},
)
def main():
[cmds, failed], opts = parse_cmdline()
status = Shell.main(cmds, timeout=opts["timeout"])
status = json.dumps(status)
if opts["stdout"]:
print(status)
elif opts["dest"]:
with open(opts["dest"]) as fh:
fh.write(status)
print(f"output written to", opts["dest"])
else:
write_output(status.encode())
main()