]>
code.delx.au - transcoding/blob - batchrun.py
3 import optparse
, shlex
, subprocess
, os
, sys
, itertools
6 parser
= optparse
.OptionParser(usage
="%prog batchfile1 [batchfile2] ...")
7 parser
.add_option("-j", "--jobs",
8 action
="store", dest
="jobs", default
=1, type="int",
9 help="The number of concurrent jobs to run")
10 opts
, args
= parser
.parse_args(sys
.argv
[1:])
14 def run(batchOpts
, args
):
15 batchOpts
.runningJobs
.append(subprocess
.Popen(args
, stdin
=file(os
.devnull
, 'r')))
16 if len(batchOpts
.runningJobs
) >= batchOpts
.jobs
:
17 for job
in batchOpts
.runningJobs
:
19 batchOpts
.runningJobs
= []
22 def _countIndentationLevel(s
):
33 continue # Ignore blank lines
34 level
= _countIndentationLevel(line
)
35 line
= line
[level
:] # Slice off the indentation
38 def batchProcess(batchOpts
, fd
):
41 for level
, line
in getblocks(fd
):
42 oldLevel
= len(opts
) - 1
44 run(batchOpts
, itertools
.chain(*opts
))
46 opts
= opts
[:level
] # Delete all options that belong to groups that are indented more than this one
47 assert len(opts
) == level
, 'Seems we missed some options somewhere'
49 opts
.append(shlex
.split(line
))
52 run(batchOpts
, itertools
.chain(*opts
))
56 opts
, args
= parseArgs()
58 batchProcess(opts
, open(name
))
60 if __name__
== "__main__":