-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreach_j.py
390 lines (302 loc) · 12.8 KB
/
foreach_j.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
##########################################################################
##########################################################################
from __future__ import print_function
import sys,argparse,os,subprocess,ctypes,term_tools,multiprocessing,threading,collections,signal
##########################################################################
##########################################################################
def flushall():
sys.stdout.flush()
sys.stderr.flush()
def pe(str):
sys.stderr.write(str)
sys.stderr.flush()
##########################################################################
##########################################################################
def get_quoted_arg(x):
if ' ' in x: return '"%s"'%x
else: return x
##########################################################################
##########################################################################
class JobQueue:
def __init__(self):
self.jobs=[]
self.results=[]
self.index=0
def add(self,job):
self.jobs.append(job)
self.results.append(None)
##########################################################################
##########################################################################
Job=collections.namedtuple('Job','argv')
JobResult=collections.namedtuple('JobResult','returncode stdout stderr')
##########################################################################
##########################################################################
class JobsThread(threading.Thread):
def __init__(self,thread_index,queue,cv,shell):
threading.Thread.__init__(self)
self.thread_index=thread_index
self._queue=queue
self._cv=cv
self.pid=None
self._shell=shell
def run(self):
while True:
with self._cv:
assert self._queue.index<=len(self._queue.jobs)
if self._queue.index==len(self._queue.jobs): break
index=self._queue.index
self._queue.index+=1
job=self._queue.jobs[index]
assert index<=len(self._queue.results),(index,len(self._queue.results))
# print(job.argv)
process=subprocess.Popen(args=job.argv,
shell=self._shell,
stdin=None,#subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.pid=process.pid
stdout,stderr=process.communicate(None)
with self._cv:
self.pid=None
assert self._queue.results[index] is None
print('%d: %d -> %d'%(self.thread_index,index,process.returncode))
self._queue.results[index]=JobResult(process.returncode,
stdout,
stderr)
self._cv.notify()
##########################################################################
##########################################################################
def main2(options,
cmd_argv):
if len(cmd_argv)==0:
pe("FATAL: No command specified.\n")
sys.exit(1)
if len(options.inputs)==0: lines=sys.stdin.readlines()
else:
lines=[]
for i in options.inputs:
with open(i,"rt") as f: lines+=f.readlines()
queue=JobQueue()
for line in lines:
# Replace -I string with the input line, or append it to the
# supplied argv.
argv=[]
line=line.strip()
replaced=False
for arg in cmd_argv:
new_arg=arg.replace(options.replstr,line)
if new_arg!=arg: replaced=True
argv.append(new_arg)
if not replaced: argv.append(line)
job=Job(argv)
queue.add(job)
print('%d job(s)'%len(queue.jobs))
job_lock=threading.Lock()
job_cv=threading.Condition(job_lock)
num_threads=multiprocessing.cpu_count()
#num_threads=1
threads=[]
for i in range(num_threads):
thread=JobsThread(i,queue,job_cv,options.shell)
threads.append(thread)
for thread in threads: thread.start()
result_index=0
while True:
new_results=[]
while len(new_results)==0:
with job_cv:
if result_index==len(queue.results): break
if queue.results[result_index] is None: job_cv.wait()
num_jobs=len(queue.results)
job_index=queue.index
while (result_index<len(queue.results) and
queue.results[result_index] is not None):
new_results.append(queue.results[result_index])
result_index+=1
if len(new_results)==0: break
print('qi=%d ri=%d tot=%d'%(job_index,result_index,num_jobs))
for new_result in new_results:
if new_result.stdout is not None:
sys.stdout.write(new_result.stdout)
sys.stdout.flush()
if new_result.stderr is not None:
sys.stderr.write(new_result.stderr)
sys.stderr.flush()
if new_result.returncode!=0:
if not options.keep_going_quietly:
prefix='WARNING' if options.keep_going else 'ERROR'
pe('%s: command returned %d: %s\n'%(prefix,
new_result.returncode,
' '.join(job.argv)))
if not options.keep_going:
with job_cv:
for thread in threads:
if thread.pid is not None:
os.kill(thread.pid,signal.SIGTERM)
queue.result_index=len(queue.results)
break
for thread in threads: thread.join()
def main(options,
cmd_argv):
if len(cmd_argv)==0:
pe("FATAL: No command specified.\n")
sys.exit(1)
if options.dry_run:
options.progress=True
options.verbose=True
if options.keep_going_quietly:
options.keep_going=True
if len(options.inputs)==0:
lines=[x.strip() for x in sys.stdin.readlines()]
else:
lines=[]
for i in options.inputs:
with open(i,
"rt") as f:
lines+=[x.strip() for x in f.readlines()]
# re-quote everything
n=len(lines)
for i in range(n):
line=lines[i]
argv=[]
replaced=False
for arg in cmd_argv:
new_arg=arg.replace(options.replstr,
line)
if arg!=new_arg:
replaced=True
argv.append(new_arg)
if not replaced:
argv.append(line)
progress_line=""
if options.progress:
progress_line+="%d/%d"%(1+i,n)
if options.progress and options.verbose:
progress_line+=": "
cmd_line=" ".join([get_quoted_arg(arg) for arg in argv])
if options.verbose: progress_line+=cmd_line
term_tools.set_title("%d/%d: %s"%(1+i,n,cmd_line))
if options.progress and not options.verbose:
pe(progress_line)
pe("\r")
elif options.progress or options.verbose:
con_stdout=None
with term_tools.TextColourInverter(): pe(progress_line)
pe("\n")
r=0
if not options.dry_run:
r=subprocess.call(argv,
shell=options.shell)
#r=os.system(cmd)
if r!=0:
if not options.keep_going_quietly:
pe("%s: Command returned %d: %s\n"%("WARNING" if options.keep_going else "ERROR",
r,
" ".join(argv)))
if not options.keep_going:
return 1
if options.progress and not options.verbose:
pe(" "*len(progress_line)+"\r")
return 0
##########################################################################
##########################################################################
# class ForeachArgumentParser(argparse.ArgumentParser):
# def __init__(self,
# *args,
# **kwargs):
# super(ForeachArgumentParser,
# self).__init__(*args,
# **kwargs)
# def format_usage(self):
# return super(ForeachArgumentParser,
# self).format_usage().rstrip()+" - CMD\n"
class MultiprocessingAction(argparse.Action):
def __init__(self,
option_strings,
dest,
nargs=None,
**kwargs):
print('%r %r %r %r'%(option_strings,dest,nargs,kwargs))
super(MultiprocessingAction,self).__init__(option_strings,
dest,
**kwargs)
def __call__(self,parser,namespace,values,option_string=None):
print('%r %r %r'%(namespace,values,option_string))
#setattr(namespace,self.dest,values)
if __name__=="__main__":
parser=argparse.ArgumentParser(fromfile_prefix_chars="@",
description="Follow options with single '-', then command to run.")
parser.add_argument("-i",
dest="inputs",
metavar="FILE",
default=[],
action="append",
help=
"""Read items from %(metavar)s. (If specified, stdin is ignored.)""")
parser.add_argument("-s",
dest="shell",
default=False,
action="store_true",
help=
"""If specified, invoke command via shell.""")
parser.add_argument("-I",
metavar="STR",
dest="replstr",
default="{}",
help=
"""Replace %(metavar)s (default %(default)s) with line from file. (If %(metavar)s not present on command line, add line to end of command line.)""")
parser.add_argument("-v",
dest="verbose",
default=False,
action="store_true",
help=
"""If specified, print each command line to stderr before executing.""")
parser.add_argument("-p",
dest="progress",
default=False,
action="store_true",
help=
"""If specified, print progress to stderr.""")
parser.add_argument("-n",
"--dry-run",
default=False,
action="store_true",
help=
"""If specified, don't actually run any commands (implies --progress and --verbose).""")
parser.add_argument("-k",
"--keep-going",
default=False,
action="store_true",
help=
"""If specified, keep going even when a command fails.""")
parser.add_argument("-K",
"--keep-going-quietly",
default=False,
action="store_true",
help=
"""If specified, keep going even when a
command fails, and don't print a warning.
""")
parser.add_argument('-j',
dest='multiprocess',
action='store_true',
help='''if specified, run multiple processes simultaneously''')
# parser.add_argument("--max-args",
# metavar="MAX-ARGS",
# default=1,
# help=
# """Use at most %(metavar)s arguments (default %(default)s) per command line.
# Work up to the '-'
argv=sys.argv[1:]
if not "-" in argv:
sep_index=len(argv)
else:
sep_index=argv.index("-")
# old_title=None
# if got_win32console:
# old_title=win32console.GetConsoleTitle()
result=main2(parser.parse_args(argv[:sep_index]),argv[sep_index+1:])
# if got_win32console:
# if old_title is not None:
# win32console.SetConsoleTitle(old_title)
sys.exit(result)