From a904025f793203e0931d7c6a9fe8e80fd7c4be3c Mon Sep 17 00:00:00 2001 From: Joey Hagedorn Date: Thu, 23 Jun 2005 19:36:48 +0000 Subject: used pylint to clean up code. (Logical change 1.240) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1001 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/StatReports.py | 179 +++++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/sbin/StatReports.py b/src/sbin/StatReports.py index 49fdf4b4e..48252c3ed 100644 --- a/src/sbin/StatReports.py +++ b/src/sbin/StatReports.py @@ -9,12 +9,17 @@ from xml.parsers.expat import ExpatError from xml.sax.saxutils import escape from smtplib import SMTP from time import asctime, strftime, strptime, ctime, gmtime -import re,string,os +from sys import exit, argv +from getopt import getopt, GetoptError +import re, string, os -def generateReport(report,delivery,deliverytype,statdata): +def generateReport(report, delivery, deliverytype, statdata): reportSections = [] + deliverytype = delivery.get("type", default = "nodes-individual") + reportgood = report.get("good", default = 'Y') + reportmodified = report.get("modified", default = 'Y') current_date = asctime()[:10] baddata = '' @@ -27,76 +32,75 @@ def generateReport(report,delivery,deliverytype,statdata): for node in statdata.findall('Node'): if node.attrib['name'] == machine.attrib['name']: if deliverytype == 'nodes-digest': - mheader="Machine: %s\n"%machine.attrib['name'] + mheader = "Machine: %s\n"%machine.attrib['name'] for stats in node.findall('Statistics'): if stats.attrib['state'] == 'clean' and current_date in stats.attrib['time']: clean += "%s\n"%machine.attrib['name'] - if report.attrib['modified'] == 'Y':#replace this with get + if reportmodified == 'Y': for modxml in stats.findall('Modified'): if current_date in stats.attrib['time']: modified+="\n%s\n"%tostring(modxml) for bad in stats.findall('Bad'): srtd = bad.findall('*') - srtd.sort(lambda x,y:cmp(tostring(x),tostring(y))) + srtd.sort(lambda x, y:cmp(tostring(x), tostring(y))) strongbad = Element("Bad") - map(lambda x:strongbad.append(x),srtd) - baddata+="Time Ran:%s\n%s\n"%(stats.attrib['time'],tostring(strongbad)) - dirty+="%s\n"%machine.attrib['name'] + map(lambda x:strongbad.append(x), srtd) + baddata += "Time Ran:%s\n%s\n"%(stats.attrib['time'], tostring(strongbad)) + dirty += "%s\n"%machine.attrib['name'] strongbad = '' - - if delivery.attrib['type'] == 'nodes-individual':#replace this with get + if deliverytype == 'nodes-individual': if baddata != '': - reportSections.append(("%s: Bcfg Nightly Errors"%machine.attrib['name'],"%s%s"%(modified,baddata))) + reportSections.append(("%s: Bcfg Nightly Errors"%machine.attrib['name'], "%s%s"%(modified, baddata))) else: - if report.attrib['good'] == 'Y': - reportSections.append(("%s: Bcfg Nightly Good"%machine.attrib['name'],"%s%s"%(modified,baddata))) - baddata='' - modified='' + if reportgood == 'Y': + reportSections.append(("%s: Bcfg Nightly Good"%machine.attrib['name'], "%s%s"%(modified, baddata))) + baddata = '' + modified = '' else: if not (modified == '' and baddata == ''): - msg += "%s %s %s\n"%(mheader,modified,baddata) - baddata='' - modified='' + msg += "%s %s %s\n"%(mheader, modified, baddata) + baddata = '' + modified = '' - if delivery.attrib['type'] == 'nodes-digest': + if deliverytype == 'nodes-digest': for destination in delivery.findall('Destination'): - toaddr=destination.attrib['address'] + toaddr = destination.attrib['address'] if msg != '': - reportSections.append(("Bcfg Nightly Errors","DIRTY:\n%s\nCLEAN:\n%s\nDETAILS:\n%s"%(dirty,clean,msg))) + reportSections.append(("Bcfg Nightly Errors", "DIRTY:\n%s\nCLEAN:\n%s\nDETAILS:\n%s"%(dirty, clean, msg))) else: - if user.attrib['good'] == 'Y': - reportSections.append(("Bcfg Nightly All Machines Good","All Machines Nomnial")) + if delivery.attrib['good'] == 'Y': + reportSections.append(("Bcfg Nightly All Machines Good", "All Machines Nomnial")) - if delivery.attrib['type'] == 'overview-stats': + if deliverytype == 'overview-stats': children = statdata.findall("Node") - regex = string.join(map(lambda x:x.get("name"), report.findall('Machine')),'|') + regex = string.join(map(lambda x:x.get("name"), report.findall('Machine')), '|') p = re.compile(regex) childstates = [] for child in children: if p.match(child.get("name")): child.states = [] for state in child.findall("Statistics"): - child.states.append((child.get("name"),state.get("state"),state.get("time"))) + child.states.append((child.get("name"), state.get("state"), state.get("time"))) if child.states != []: childstates.append(child.states[len(child.states)-1]) - childstates.sort(lambda x,y:cmp(x[0],y[0])) + childstates.sort(lambda x, y:cmp(x[0], y[0])) - staleones=[] - cleanones=[] - dirtyones=[] - unpingableones=[] + staleones = [] + cleanones = [] + dirtyones = [] + unpingableones = [] for instance in childstates: - if instance[1]=="dirty": + if instance[1] == "dirty": dirtyones.append(instance) - elif instance[1]=="clean": + elif instance[1] == "clean": cleanones.append(instance) - if strptime(instance[2])[0]!=strptime(ctime())[0] \ - or strptime(instance[2])[1]!=strptime(ctime())[1] \ - or strptime(instance[2])[2]!=strptime(ctime())[2]: + if strptime(instance[2])[0] != strptime(ctime())[0] \ + or strptime(instance[2])[1] != strptime(ctime())[1] \ + or strptime(instance[2])[2] != strptime(ctime())[2]: staleones.append(instance) if staleones != []: @@ -106,37 +110,37 @@ def generateReport(report,delivery,deliverytype,statdata): staleones.remove(instance) unpingableones.append(instance) - statmsg='' - statmsg+="SUMMARY INFORMATION:\n" - statmsg+="Up & Not Running Nightly: %d\n"%len(staleones) - statmsg+="Unpingable: %d\n"%len(unpingableones) - statmsg+="Dirty: %d\n"%len(dirtyones) - statmsg+="Clean: %d\n"%len(cleanones) - statmsg+="---------------------------------\n" - total=len(cleanones)+len(dirtyones) - statmsg+="Total: %d\n\n\n"%len(childstates) - - statmsg+="\n UP AND NOT RUNNING NIGHTLY:\n" + statmsg = '' + statmsg += "SUMMARY INFORMATION:\n" + statmsg += "Up & Not Running Nightly: %d\n"%len(staleones) + statmsg += "Unpingable: %d\n"%len(unpingableones) + statmsg += "Dirty: %d\n"%len(dirtyones) + statmsg += "Clean: %d\n"%len(cleanones) + statmsg += "---------------------------------\n" + total = len(cleanones)+len(dirtyones) + statmsg += "Total: %d\n\n\n"%len(childstates) + + statmsg += "\n UP AND NOT RUNNING NIGHTLY:\n" for one in staleones: - statmsg+=one[0]+".mcs.anl.gov\n" - statmsg+="\nDIRTY:\n" + statmsg += one[0] + ".mcs.anl.gov\n" + statmsg += "\nDIRTY:\n" for one in dirtyones: - statmsg+=one[0]+".mcs.anl.gov\n" - statmsg+="\nCLEAN:\n" + statmsg += one[0] + ".mcs.anl.gov\n" + statmsg += "\nCLEAN:\n" for one in cleanones: - statmsg+=one[0]+".mcs.anl.gov\n" - statmsg+="\nUNPINGABLE:\n" + statmsg += one[0] + ".mcs.anl.gov\n" + statmsg += "\nUNPINGABLE:\n" for one in unpingableones: - statmsg+=one[0]+".mcs.anl.gov\n" + statmsg += one[0] + ".mcs.anl.gov\n" - reportSections.append(("Bcfg Nightly Errors","%s"%(statmsg))) + reportSections.append(("Bcfg Nightly Errors", "%s"%(statmsg))) return reportSections -def mail(reportsections,delivery,deliverytype): +def mail(reportsections, delivery): current_date = asctime()[:10] mailer = SMTP('localhost') fromaddr = "root@netzero.mcs.anl.gov" @@ -144,31 +148,31 @@ def mail(reportsections,delivery,deliverytype): for destination in delivery.findall('Destination'): toaddr = destination.attrib['address'] for section in reportsections: - msg="To: %s\nFrom: %s\nSubject: %s\n\n\n%s"%(toaddr,fromaddr,section[0],section[1]) - mailer.sendmail(fromaddr,toaddr,msg) + msg="To: %s\nFrom: %s\nSubject: %s\n\n\n%s"%(toaddr, fromaddr, section[0], section[1]) + mailer.sendmail(fromaddr, toaddr, msg) mailer.quit() -def rss(reportsections,delivery,deliverytype): +def rss(reportsections, delivery): #need Report, Delivery, ReportSections(list of tuples) #check and see if rss file xists for destination in delivery.findall('Destination'): try: - fil = open(destination.attrib['address'],'r') + fil = open(destination.attrib['address'], 'r') olddoc = XML(fil.read()) #read array of 9 most recent items out items = olddoc.find("channel").findall("item")[0:9]#defines the number of recent articles to keep fil.close() - fil = open(destination.attrib['address'],'w') - except (IOError,ExpatError): - fil = open(destination.attrib['address'],'w') + fil = open(destination.attrib['address'], 'w') + except (IOError, ExpatError): + fil = open(destination.attrib['address'], 'w') items = [] rss = Element("rss") - channel = SubElement(rss,"channel") - rss.set("version","2.0") + channel = SubElement(rss, "channel") + rss.set("version", "2.0") chantitle = SubElement(channel, "title") chantitle.text = report.attrib['name'] chanlink = SubElement(channel, "link") @@ -180,38 +184,38 @@ def rss(reportsections,delivery,deliverytype): item = SubElement(channel, "item") title = SubElement(item, "title") title.text = section[0] - description = SubElement(item,"description") + description = SubElement(item, "description") description.text = "
"+escape(section[1])+"
" - date = SubElement(item,"pubDate") + date = SubElement(item, "pubDate") date.text = strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime()) item = None if items != []: for item in items: channel.append(item) - tree = ""+tostring(rss) + tree = "" + tostring(rss) fil.write(tree) fil.close() -def www(reportsections,delivery,deliverytype): +def www(reportsections, delivery): #need Report, Delivery, ReportSections(list of tuples) #check and see if rss file xists for destination in delivery.findall('Destination'): - fil = open(destination.attrib['address'],'w') + fil = open(destination.attrib['address'], 'w') html = Element("HTML") - body = SubElement(html,"BODY") + body = SubElement(html, "BODY") for section in reportsections: - SubElement(body,"br") + SubElement(body, "br") item = SubElement(body, "div") - title = SubElement(item,"h1") + title = SubElement(item, "h1") title.text = section[0] - pre = SubElement(item,"pre") + pre = SubElement(item, "pre") pre.text = section[1] - SubElement(body,"hr") - SubElement(body,"br") + SubElement(body, "hr") + SubElement(body, "br") fil.write(tostring(html)) fil.close() @@ -225,9 +229,9 @@ if __name__ == '__main__': statpath = "%s/statistics.xml" % c.get('server', 'metadata') try: opts, args = getopt(argv[1:], "hc:s:", ["help", "config=", "stats="]) - except GetoptError,msg: + except GetoptError, msg: # print help information and exit: - print "%s\nUsage:\nStatReports.py [-h] [-c ] [-s ]"%(msg) + print "%s\nUsage:\nStatReports.py [-h] [-c ] [-s ]"%(msg) exit(2) for o, a in opts: if o in ("-h", "--help"): @@ -238,31 +242,34 @@ if __name__ == '__main__': if o in ("-s", "--stats"): statpath = a + + try: statdata = XML(open(statpath).read()) except (IOError, ExpatError): print("StatReports: Failed to parse %s"%(statpath)) + exit(1) '''Reads report configuration info''' try: configdata = XML(open(configpath).read()) except (IOError, ExpatError): - print("StatReports: Failed to parse %s"%(configpath)) - + print("StatReports: Failed to parse %s"%(configpath)) + exit(1) for report in configdata.findall('Report'): for delivery in report.findall('Delivery'): - deliverytype = delivery.get('type',default='nodes-digest') - deliverymechanism = delivery.get('mechanism',default='invalid') + deliverytype = delivery.get('type', default='nodes-digest') + deliverymechanism = delivery.get('mechanism', default='invalid') - reportsections = generateReport(report,delivery,deliverytype,statdata) + reportsections = generateReport(report, delivery, deliverytype, statdata) if deliverymechanism == 'mail': - mail(reportsections,delivery,deliverytype) + mail(reportsections, delivery) elif deliverymechanism == 'rss': - rss(reportsections,delivery,deliverytype) + rss(reportsections, delivery) elif deliverymechanism == 'www': - www(reportsections,delivery,deliverytype) + www(reportsections, delivery) else: print("StatReports: Invalid delivery mechanism in report-configuration!") deliverymechanism = '' -- cgit v1.2.3-1-g7c22