]> code.delx.au - transcoding/commitdiff
Allow batchrun to take an arbitarly deep input file.
authorGreg Darke <greg+asuna@tsukasa.net.au>
Mon, 5 May 2008 13:37:45 +0000 (23:37 +1000)
committerGreg Darke <greg+asuna@tsukasa.net.au>
Mon, 5 May 2008 13:37:45 +0000 (23:37 +1000)
These changes force the minimum version of python required to by this script to be python2.4 (as itertools is required).

This version of the script also requires that the input file does not 'skip' levels. This means you can only move up from level 3 to 4 (not from 3 straight to 5), you may move down any number of levels at a time though.

batchrun.py

index b8596ac51c82a2827f7e52a6456f950baf494e9d..49fae6911e8fe12377f5417bdbfc54ab0a083688 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-import optparse, shlex, subprocess, sys
+import optparse, shlex, subprocess, sys, itertools
 
 def parseArgs():
        parser = optparse.OptionParser(usage="%prog batchfile1 [batchfile2] ...")
@@ -10,16 +10,37 @@ def parseArgs():
 def run(args):
        subprocess.Popen(args).wait()
 
-def batchProcess(fd):
-       opts = [[], []]
+def getblocks(fd):
+       def _countIndentationLevel(s):
+               level = 0
+               for ch in s:
+                       if ch == "\t":
+                               level += 1
+                       else:
+                               break
+               return level
+
        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
+               level = _countIndentationLevel(line)
+               line = line[level:] # Slice off the indentation
+               yield level, line
+                       
+def batchProcess(fd):
+       opts = []
+
+       for level, line in getblocks(fd):
+               oldLevel = len(opts) - 1
+               if level <= oldLevel:
+                       run(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(itertools.chain(*opts))
+
 
 def main():
        args = parseArgs()