#!/usr/bin/env python
# partition an lrs computation into several jobs, for potential parallel
# execution
# David Bremner, bremner@unb.ca, 2007/05/03
# this script requires at least python 2.3
# it is placed in the public domain
from optparse import OptionParser
from tempfile import mkdtemp
from os.path import basename,join,splitext
import sys
import re
import os
def mklrsfile(outdir,basefile,tag,**options):
infile=open(basefile)
(root,ext)=os.path.splitext(os.path.basename(basefile))
outname=os.path.join(outdir,root+"_"+tag+ext)
outfile=open(outname,'w')
for line in infile:
print >>outfile,line,
for key in options.keys():
print >>outfile,key,' ',
if options[key] is True:
pass
elif isinstance(options[key],(tuple,list)):
for v in options[key]:
print >>outfile, v, ' ',
else:
print >>outfile,options[key]
outfile.close()
infile.close()
return outname
def runlrs(lrs,input):
pipe=os.popen(lrs+"<"+input)
mode=None
rval=[]
vmode=re.compile('V-representation')
hmode=re.compile('H-representation')
numre=re.compile('\d+')
for line in pipe:
if mode is None:
if vmode.match(line):
mode='V'
linere=re.compile('V#\d+.*([:]|I)')
elif hmode.match(line):
mode='H'
linere=re.compile(r'F#\d+ .*([:]|I)')
if mode:
match=linere.match(line)
if match:
numbers=map(lambda x : int(x) ,numre.findall(match.group()))
if mode == 'H':
depth=numbers[2]
else:
depth=numbers[3]
if options.depth==depth:
rval.append(numbers)
return rval
parser = OptionParser()
parser.add_option("-i", "--input",
action="store", type="string", dest="input",
help="lrs input in FILE", metavar="FILE",default=None)
parser.add_option("-l", "--lrs",
action="store", type="string", dest="lrs",
help="lrs binary path is PATH", metavar="PATH",
default="glrs")
parser.add_option("-d","--depth",
action="store", type="int", dest="depth",
help="expand to depth DEPTH", metavar="DEPTH",
default=3)
parser.add_option("-o","--outdir",
action="store", type="string", dest="outdir",
help="place output DIR", metavar="DIR",
default=os.curdir)
(options, args) = parser.parse_args()
if len(args)>0 and options.input is None:
options.input=args[0]
if (options.input is None):
print >>sys.stderr,"Please give an input file"
parser.print_help()
sys.exit(1)
name=mklrsfile(outdir=options.outdir,basefile=options.input,
maxdepth=options.depth,
tag='split',
printcobasis=1)
mklrsfile(outdir=options.outdir,basefile=options.input,
maxdepth=options.depth,
tag='0')
lists=runlrs(options.lrs,name)
counter=1
for params in lists:
mklrsfile(outdir=options.outdir,basefile=options.input,
tag=str(counter),
restart=params,
mindepth=options.depth)
counter+=1