]> code.delx.au - transcoding/blob - batchrun.py
Many improvements!
[transcoding] / batchrun.py
1 #!/usr/bin/env python
2
3 import optparse, shlex, subprocess, os, sys, itertools
4
5 def parseArgs():
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:])
11 opts.runningJobs = []
12 return opts, args
13
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:
18 job.wait()
19 batchOpts.runningJobs = []
20
21 def getblocks(fd):
22 def _countIndentationLevel(s):
23 level = 0
24 for ch in s:
25 if ch == "\t":
26 level += 1
27 else:
28 break
29 return level
30
31 for line in fd:
32 if not line.strip():
33 continue # Ignore blank lines
34 level = _countIndentationLevel(line)
35 line = line[level:] # Slice off the indentation
36 yield level, line
37
38 def batchProcess(batchOpts, fd):
39 opts = []
40
41 for level, line in getblocks(fd):
42 oldLevel = len(opts) - 1
43 if level <= oldLevel:
44 run(batchOpts, itertools.chain(*opts))
45
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'
48
49 opts.append(shlex.split(line))
50
51 if len(opts) > 0:
52 run(batchOpts, itertools.chain(*opts))
53
54
55 def main():
56 opts, args = parseArgs()
57 for name in args:
58 batchProcess(opts, open(name))
59
60 if __name__ == "__main__":
61 main()
62