]> code.delx.au - transcoding/blob - batchrun.py
Allow batchrun to take an arbitarly deep input file.
[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 level = _countIndentationLevel(line)
25 line = line[level:] # Slice off the indentation
26 yield level, line
27
28 def batchProcess(fd):
29 opts = []
30
31 for level, line in getblocks(fd):
32 oldLevel = len(opts) - 1
33 if level <= oldLevel:
34 run(itertools.chain(*opts))
35
36 opts = opts[:level] # Delete all options that belong to groups that are indented more than this one
37 assert len(opts) == level, 'Seems we missed some options somewhere'
38
39 opts.append(shlex.split(line))
40
41 if len(opts) > 0:
42 run(itertools.chain(*opts))
43
44
45 def main():
46 args = parseArgs()
47 for name in args:
48 batchProcess(open(name))
49
50 if __name__ == "__main__":
51 main()
52