#!/usr/bin/env python
-import optparse, shlex, subprocess, sys
+import optparse, shlex, subprocess, os, sys, itertools
def parseArgs():
parser = optparse.OptionParser(usage="%prog batchfile1 [batchfile2] ...")
+ parser.add_option("-j", "--jobs",
+ action="store", dest="jobs", default=1, type="int",
+ help="The number of concurrent jobs to run")
opts, args = parser.parse_args(sys.argv[1:])
- return args
+ opts.runningJobs = []
+ return opts, args
-def run(args):
- subprocess.Popen(args).wait()
+def run(batchOpts, args):
+ batchOpts.runningJobs.append(subprocess.Popen(args, stdin=file(os.devnull, 'r')))
+ if len(batchOpts.runningJobs) >= batchOpts.jobs:
+ for job in batchOpts.runningJobs:
+ job.wait()
+ batchOpts.runningJobs = []
+
+def getblocks(fd):
+ def _countIndentationLevel(s):
+ level = 0
+ for ch in s:
+ if ch == "\t":
+ level += 1
+ else:
+ break
+ return level
-def batchProcess(fd):
- opts = [[], []]
for line in fd:
- args = shlex.split(line)
- if line.startswith("\t\t"):
- run(opts[0] + opts[1] + args)
- elif line.startswith("\t"):
- opts[1] = args
- else:
- opts[0] = args
+ if not line.strip():
+ continue # Ignore blank lines
+ level = _countIndentationLevel(line)
+ line = line[level:] # Slice off the indentation
+ yield level, line
+
+def batchProcess(batchOpts, fd):
+ opts = []
+
+ for level, line in getblocks(fd):
+ oldLevel = len(opts) - 1
+ if level <= oldLevel:
+ run(batchOpts, itertools.chain(*opts))
+
+ opts = opts[:level] # Delete all options that belong to groups that are indented more than this one
+ assert len(opts) == level, 'Seems we missed some options somewhere'
+
+ opts.append(shlex.split(line))
+
+ if len(opts) > 0:
+ run(batchOpts, itertools.chain(*opts))
+
def main():
- args = parseArgs()
+ opts, args = parseArgs()
for name in args:
- batchProcess(open(name))
+ batchProcess(opts, open(name))
if __name__ == "__main__":
main()