#!/usr/bin/python import re from lxml import etree import os,sys from optparse import OptionParser options=None args=None usage="remove_from_link -l [link report filename] -m [match to remove] -o [outfile]" parser=OptionParser(usage=usage) parser.add_option("-l","--linkreport",dest="linkreport",help="the link report") parser.add_option("-m","--match",dest="match",help="the match to remove") parser.add_option("-o","--outfile",dest="outfile",help="the file to write the new linkreport in") (options,args)=parser.parse_args() if not options.linkreport: print "Error: link report not present." sys.exit() if not options.match: print "Error: the match to search for is not present." sys.exit() if not options.outfile: print "Error: the outfile is not present." sys.exit() #read the link report and setup etree stuff content=open(options.linkreport,"r").read() linkroot=etree.fromstring(content) linkscripts=linkroot[0] extdefs=linkroot[1] #create new xml file report=etree.Element("report") scripts=etree.SubElement(report,"scripts") externals=etree.SubElement(report,"external-defs") #parse scripts for script in linkscripts: name=script.get("name") if(re.match(options.match,name)):continue scripts.append(script) #add extdefs for edef in extdefs: externals.append(edef) #write open(options.outfile,"w").write(etree.tostring(report,pretty_print=True))