diff options
author | Narayan Desai <desai@mcs.anl.gov> | 2005-11-21 16:38:20 +0000 |
---|---|---|
committer | Narayan Desai <desai@mcs.anl.gov> | 2005-11-21 16:38:20 +0000 |
commit | 2500c9a38ca7a0484951cb46d9785d4ffbc48754 (patch) | |
tree | 2df3b8f55fbca2f5a8acc66bb5a03b51afe4c676 /src/sbin | |
parent | fc05bf41718f87954ccb52d49c5f91eb85f54085 (diff) | |
download | bcfg2-2500c9a38ca7a0484951cb46d9785d4ffbc48754.tar.gz bcfg2-2500c9a38ca7a0484951cb46d9785d4ffbc48754.tar.bz2 bcfg2-2500c9a38ca7a0484951cb46d9785d4ffbc48754.zip |
switch to lxml
(Logical change 1.362)
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1508 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/sbin')
-rw-r--r-- | src/sbin/StatReports | 152 |
1 files changed, 30 insertions, 122 deletions
diff --git a/src/sbin/StatReports b/src/sbin/StatReports index 8539eb4de..283063b04 100644 --- a/src/sbin/StatReports +++ b/src/sbin/StatReports @@ -8,75 +8,56 @@ for bcfg2''' __revision__ = '$Revision$' from ConfigParser import ConfigParser, NoSectionError, NoOptionError -from lxml.etree import XML, Element, SubElement, tostring +from lxml.etree import XML, XSLT, parse, Element, ElementTree, SubElement, tostring from xml.parsers.expat import ExpatError from time import asctime, strptime, time from socket import getfqdn from sys import argv from getopt import getopt, GetoptError -import re, os, libxml2, libxslt -from tempfile import mktemp +import re, os from copy import deepcopy -def generatereport(rs, nr): +def generatereport(rspec, nrpt): '''generatereport creates and returns an ElementTree representation of a report adhering to the XML spec for intermediate reports''' - reportspec = deepcopy(rs) - nodereprt = deepcopy(nr) + reportspec = deepcopy(rspec) + nodereprt = deepcopy(nrpt) reportgood = reportspec.get("good", default = 'Y') reportmodified = reportspec.get("modified", default = 'Y') - current_date = asctime()[:10] '''build regex of all the nodes we are reporting about''' - regex = '|'.join([x.get("name") for x in \ - reportspec.findall('Machine')]) - pattern = re.compile(regex) - + pattern = re.compile( '|'.join([item.get("name") for item in reportspec.findall('Machine')])) for node in nodereprt.findall('Node'): - - if node.findall('HostInfo') == [] or \ - not pattern.match(node.get("name")) or \ - node.findall('Statistics') == [] or \ - node.find("HostInfo").get("fqdn") == "":#maybe issue a warning instead? + if not (node.findall("HostInfo") and node.findall("Statistics") and + node.find("HostInfo").get("fqdn") and pattern.match(node.get('name'))): + # don't know enough about node nodereprt.remove(node) continue #reduce to most recent Statistics entry statisticslist = node.findall('Statistics') #this line actually sorts from most recent to oldest - statisticslist.sort(lambda y, x: cmp(strptime(x.get("time")), \ - strptime(y.get("time")))) + stats = statisticslist.sort(lambda y, x: cmp(strptime(x.get("time")), strptime(y.get("time"))))[0] - stats = statisticslist[0] - - [node.remove(x) for x in node.findall('Statistics')] - - + [node.remove(item) for item in node.findall('Statistics')] + #add a good tag if node is good and we wnat to report such if reportgood == 'Y' and stats.get('state') == 'clean': SubElement(stats,"Good") - for x in stats.findall('Modified'): - if reportmodified == 'N' or x.getchildren() == None: - stats.remove(x) - - for x in stats.findall('Bad'): - if x.getchildren() == None: - stats.remove(x) + [stats.remove(item) for item in stats.findall("Bad") + stats.findall("Modified") if \ + item.getchildren() == None] + [stats.remove(item) for item in stats.findall("Modified") if reportmodified == 'N'] #test for staleness -if stale add Stale tag if stats.get("time").find(current_date) == -1: SubElement(stats,"Stale") - node.append(stats) - return nodereprt - - def mail(mailbody, confi): '''mail mails a previously generated report''' @@ -273,109 +254,36 @@ if __name__ == '__main__': #IMPORTANT to add some error checking here-parseerrors #this might be sufficient try: - styledoc = libxml2.parseFile(transformpath+transform) - style = libxslt.parseStylesheetDoc(styledoc) + stylesheet = XSLT(parse(transformpath + transform)) + #styledoc = libxml2.parseFile(transformpath+transform) + #style = libxslt.parseStylesheetDoc(styledoc) except: print("StatReports: invalid XSLT transform file.") raise SystemExit, 1 if deliverymechanism == 'mail': if delivtype == 'nodes-individual': - p2noderep = deepcopy(procnodereport) + reportdata = deepcopy(procnodereport) for noden in procnodereport.findall("Node"): - [p2noderep.remove(y) for y in p2noderep.findall("Node")] - p2noderep.append(noden) - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - tempr.write(tostring(p2noderep)) - tempr.seek(0) - doc = libxml2.parseFile(tempfilename) - result = style.applyStylesheet(doc, None) - tempr.close() - os.unlink(tempfilename) - del tempr - try: - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - style.saveResultToFile(tempr, result) - tempr.seek(0) - outputstring = tempr.read() - except: - outputstring = None#this is a nasty hack. When the xslt transform breaks-- just blank it out - #this is done due to a bug in libxslt - #This needs to be fixed in future releases - - tempr.close() - del tempr - os.unlink(tempfilename) - - if not outputstring == None: - toastring = '' - for desti in deliv.findall("Destination"): - toastring = "%s%s " % \ - (toastring, desti.get('address')) - #prepend To: and From: - outputstring = "To: %s\nFrom: root@%s\n%s"% \ - (toastring, getfqdn(), outputstring) - mail(outputstring, c) #call function to send - doc.freeDoc() - result.freeDoc() - style.freeStylesheet() + [reportdata.remove(y) for y in reportdata.findall("Node")] + reportdata.append(noden) else: - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - tempr.write(tostring(procnodereport)) - tempr.seek(0) - doc = libxml2.parseFile(tempfilename) - result = style.applyStylesheet(doc, None) - tempr.close() - del tempr - os.unlink(tempfilename) - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - style.saveResultToFile(tempr, result) - tempr.seek(0) - outputstring = tempr.read() - tempr.close() - del tempr - os.unlink(tempfilename) - - if not outputstring == None: - toastring = '' - for desti in deliv.findall("Destination"): - toastring = "%s%s " % \ - (toastring, desti.get('address')) + reportdata = procnodereport + outputstring = tostring(stylesheet.apply(reportdata).getroot()) + + if not outputstring == None: + toastring = '' + for desti in deliv.findall("Destination"): + toastring = "%s%s " % \ + (toastring, desti.get('address')) #prepend To: and From: outputstring = "To: %s\nFrom: root@%s\n%s"% \ (toastring, getfqdn(), outputstring) mail(outputstring, c) #call function to send - style.freeStylesheet() - doc.freeDoc() - result.freeDoc() else: - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - tempr.write(tostring(procnodereport)) - tempr.seek(0) - doc = libxml2.parseFile(tempfilename) - result = style.applyStylesheet(doc, None) - tempr.close() - del tempr - os.unlink(tempfilename) - tempfilename = mktemp() - tempr = open(tempfilename, "w+") - style.saveResultToFile(tempr, result) - tempr.seek(0) - outputstring = tempr.read() - tempr.close() - del tempr - os.unlink(tempfilename) - + outputstring = tostring(stylesheet.apply(ElementTree(procnodereport)).getroot()) if deliverymechanism == 'rss': rss(outputstring, deliv, reprt) else: # must be deliverymechanism == 'www': www(outputstring, deliv) - style.freeStylesheet() - doc.freeDoc() - result.freeDoc() |