-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpardo
More file actions
executable file
·131 lines (106 loc) · 2.79 KB
/
Copy pathpardo
File metadata and controls
executable file
·131 lines (106 loc) · 2.79 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
#!/usr/bin/python
import sys
import os
import signal
import shlex
from multiprocessing import Pool, cpu_count
from subprocess import Popen, PIPE
verbose = 1
nprocs = cpu_count()
key = None
split_line = True
shell = True
def usage():
sys.stderr.write(
"""Usage: %s [options] <cmd> [arg1] [arg2] ...
[options]:
-k <key> Set the text to substitute the input line. If none is given,
just append the text to the end of the command [%s].
-n <num-procs> Set the number of parallel processes to run [%s].
-s <bool> Split the line into separate args [%s].
-S <bool> Run the command through the default shell [%s].
-v <0|1|2> Verbose level [%s].
-h Display this message.
""" % (sys.argv[0], key, nprocs, split_line, shell, verbose))
sys.exit()
if len(sys.argv) < 2:
usage()
lastopt = 0
i = 1
while i < len(sys.argv):
if sys.argv[i][0] == '-':
if i < len(sys.argv)-1 and sys.argv[i] == "-k":
key = sys.argv[i+1]
lastopt = i+1
i += 1
elif i < len(sys.argv)-1 and sys.argv[i] == '-n':
nprocs = int(sys.argv[i+1])
lastopt = i+1
i += 1
elif i < len(sys.argv)-1 and sys.argv[i] == '-v':
verbose = int(sys.argv[i+1])
lastopt = i+1
i += 1
elif i < len(sys.argv)-1 and sys.argv[i] == '-s':
split_line = bool(sys.argv[i+1])
lastopt = i+1
i += 1
elif i < len(sys.argv)-1 and sys.argv[i] == '-S':
shell = bool(sys.argv[i+1])
lastopt = i+1
i += 1
else:
usage()
i += 1
else:
break
if verbose >= 2:
sys.stderr.write('key=%s\n' % key)
sys.stderr.write('nprocs=%s\n' % nprocs)
sys.stderr.write('split=%s\n' % split_line)
sys.stderr.write('shell=%s\n' % shell)
if lastopt >= len(sys.argv)-1:
sys.stderr.write("No command given, exiting.\n");
sys.exit()
cmd = [sys.argv[i] for i in range(lastopt+1, len(sys.argv))]
class ReplaceInCommandStream:
def __init__(self, stream, cmd, key):
self.stream = stream
self.cmd = cmd[:]
self.key = key
def __iter__(self):
return self
def next(self):
line = str(self.stream.readline())
if line == "":
raise StopIteration
line = line.rstrip()
cmd2 = self.cmd[:]
if self.key == None:
if split_line:
cmd2.extend(shlex.split(line))
else:
cmd2.append(line)
else:
for i in range(len(self.cmd)):
cmd2[i] = cmd2[i].replace(self.key, line)
return cmd2
def run_cmd(cmd):
try:
if verbose >= 1:
sys.stderr.write(" ".join(cmd)+'\n')
if shell:
p = Popen(" ".join(cmd), shell=True)
else:
p = Popen(cmd)
signal.signal(signal.SIGINT, lambda : p.terminate())
p.wait()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
try:
pool = Pool(processes=nprocs)
r = pool.map_async(run_cmd, ReplaceInCommandStream(sys.stdin, cmd, key))
r.get(timeout = None)
except KeyboardInterrupt:
pass