]>
code.delx.au - transcoding/blob - batch-run
12 def run(running_jobs
, cmd
):
13 p
= subprocess
.Popen(cmd
, stdin
=subprocess
.DEVNULL
)
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
)
63 def make_and_chdir(filename
):
64 dirname
= os
.path
.splitext(filename
)[0] + ".out"
67 except FileExistsError
:
72 parser
= optparse
.OptionParser(usage
="%prog batchfile1 [batchfile2] ...")
73 parser
.add_option("-j", "--jobs",
74 action
="store", dest
="jobs", default
=1, type="int",
75 help="The number of concurrent jobs to run")
76 opts
, args
= parser
.parse_args(sys
.argv
[1:])
77 opts
.running_jobs
= []
78 args
= map(os
.path
.abspath
, args
)
82 opts
, args
= parse_args()
83 filenames
= list(map(os
.path
.abspath
, args
))
84 for filename
in filenames
:
85 print("Processing", filename
)
86 make_and_chdir(filename
)
87 with
open(filename
) as fd
:
88 batch_process(opts
, parse_file(fd
))
90 if __name__
== "__main__":