]> code.delx.au - transcoding/blob - batchrun.py
Automated merge with ssh://hg@kagami/james_transcoding
[transcoding] / batchrun.py
1 #!/usr/bin/env python
2
3 import optparse, shlex, subprocess, sys, itertools
4
5 def parseArgs():
6 parser = optparse.OptionParser(usage="%prog batchfile1 [batchfile2] ...")
7 opts, args = parser.parse_args(sys.argv[1:])
8 return args
9
10 def run(args):
11 subprocess.Popen(args).wait()
12
13 def getblocks(fd):
14 def _countIndentationLevel(s):
15 level = 0
16 for ch in s:
17 if ch == "\t":
18 level += 1
19 else:
20 break
21 return level
22
23 for line in fd:
24 if not line.strip():
25 continue # Ignore blank lines
26 level = _countIndentationLevel(line)
27 line = line[level:] # Slice off the indentation
28 yield level, line
29
30 def batchProcess(fd):
31 opts = []
32
33 for level, line in getblocks(fd):
34 oldLevel = len(opts) - 1
35 if level <= oldLevel:
36 run(itertools.chain(*opts))
37
38 opts = opts[:level] # Delete all options that belong to groups that are indented more than this one
39 assert len(opts) == level, 'Seems we missed some options somewhere'
40
41 opts.append(shlex.split(line))
42
43 if len(opts) > 0:
44 run(itertools.chain(*opts))
45
46
47 def main():
48 args = parseArgs()
49 for name in args:
50 batchProcess(open(name))
51
52 if __name__ == "__main__":
53 main()
54