]>
code.delx.au - transcoding/blob - batch-run
12 def run(running_jobs
, cmd
):
13 p
= subprocess
.Popen(cmd
, stdin
=file(os
.devnull
, 'r'))
14 running_jobs
.append(p
)
16 def wait_for_completion(max_jobs
, running_jobs
):
17 while len(running_jobs
) >= max_jobs
:
19 for job
in running_jobs
:
20 if job
.poll() is not None:
21 running_jobs
.remove(job
)
36 continue # Ignore blank lines
37 level
= count_indent(line
)
38 line
= line
[level
:] # Slice off the indentation
42 def batch_process(opts
, lines
):
46 for level
, line
in lines
:
47 old_level
= len(cmd
) - 1
48 if level
<= old_level
:
49 run(running_jobs
, itertools
.chain(*cmd
))
50 wait_for_completion(opts
.jobs
, running_jobs
)
52 # Delete all options that belong to groups that are indented more than
55 assert len(cmd
) == level
58 cmd
.append(shlex
.split(line
))
60 # Wait for remaining jobs to finish
61 wait_for_completion(1, running_jobs
)
64 parser
= optparse
.OptionParser(usage
="%prog batchfile1 [batchfile2] ...")
65 parser
.add_option("-j", "--jobs",
66 action
="store", dest
="jobs", default
=1, type="int",
67 help="The number of concurrent jobs to run")
68 opts
, args
= parser
.parse_args(sys
.argv
[1:])
69 opts
.running_jobs
= []
73 opts
, args
= parse_args()
75 batch_process(opts
, parse_file(open(name
)))
77 if __name__
== "__main__":