diff options
author | Narayan Desai <desai@mcs.anl.gov> | 2005-09-07 17:33:45 +0000 |
---|---|---|
committer | Narayan Desai <desai@mcs.anl.gov> | 2005-09-07 17:33:45 +0000 |
commit | f0a1f4923568f1c70d024cf10aa375e02e951659 (patch) | |
tree | 319c84be4f2efa0ad7ea566dc454154b8b4c4f05 /src/sbin/StatReports | |
parent | 321448ca8e3306766f5af6b09e10b16163e57d40 (diff) | |
download | bcfg2-f0a1f4923568f1c70d024cf10aa375e02e951659.tar.gz bcfg2-f0a1f4923568f1c70d024cf10aa375e02e951659.tar.bz2 bcfg2-f0a1f4923568f1c70d024cf10aa375e02e951659.zip |
finish up temp file leak fix
2005/09/06 20:06:54-05:00 anl.gov!desai
fix four tempfile leaks
switch sys.exit calls to raise SystemExit
Fix some imports and error handling
(Logical change 1.299)
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1194 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/sbin/StatReports')
-rw-r--r-- | src/sbin/StatReports | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/sbin/StatReports b/src/sbin/StatReports index 91321919d..6fd8f8adc 100644 --- a/src/sbin/StatReports +++ b/src/sbin/StatReports @@ -8,14 +8,13 @@ for bcfg2''' __revision__ = '$Revision$' from ConfigParser import ConfigParser, NoSectionError, NoOptionError -from elementtree.ElementTree import * +from elementtree.ElementTree import XML, Element, SubElement, tostring from xml.parsers.expat import ExpatError -from xml.sax.saxutils import escape -from time import asctime, strftime, strptime, gmtime, time +from time import asctime, strptime, time from socket import getfqdn -from sys import exit, argv +from sys import argv from getopt import getopt, GetoptError -import re, os, string, libxml2, libxslt +import re, os, libxml2, libxslt from tempfile import mktemp from copy import deepcopy @@ -31,8 +30,8 @@ def generatereport(rs, nr): current_date = asctime()[:10] '''build regex of all the nodes we are reporting about''' - regex = string.join([x.get("name") for x in \ - reportspec.findall('Machine')], '|') + regex = '|'.join([x.get("name") for x in \ + reportspec.findall('Machine')]) pattern = re.compile(regex) @@ -83,7 +82,7 @@ def mail(mailbody, confi): try: mailer = confi.get('statistics', 'sendmailpath') - except NoSectionError, NoOptionError: + except (NoSectionError, NoOptionError): mailer = "/usr/sbin/sendmail" # open a pipe to the mail program and # write the data to the pipe @@ -173,11 +172,11 @@ if __name__ == '__main__': except GetoptError, mesg: # print help information and exit: print "%s\nUsage:\nStatReports.py [-h] [-c <configuration-file>] [-s <statistics-file>]" % (mesg) - exit(2) + raise SystemExit, 2 for o, a in opts: if o in ("-h", "--help"): print "Usage:\nStatReports.py [-h] [-c <configuration-file>] [-s <statistics-file>]" - exit() + raise SystemExit if o in ("-c", "--config"): configpath = a if o in ("-s", "--stats"): @@ -198,22 +197,22 @@ if __name__ == '__main__': statsdata = XML(open(statpath).read()) except (IOError, ExpatError): print("StatReports: Failed to parse %s"%(statpath)) - exit(1) + raise SystemExit, 1 try: configdata = XML(open(configpath).read()) except (IOError, ExpatError): print("StatReports: Failed to parse %s"%(configpath)) - exit(1) + raise SystemExit, 1 try: metadata = XML(open(metadatapath).read()) except (IOError, ExpatError): print("StatReports: Failed to parse %s"%(metadatapath)) - exit(1) + raise SystemExit, 1 try: hostinfodata = XML(open(hostinfopath).read()) except (IOError, ExpatError): print("StatReports: Failed to parse %s. Is GenerateHostInfo in your path?"%(hostinfopath)) - exit(1) + raise SystemExit, 1 #Merge data from three sources @@ -268,7 +267,7 @@ if __name__ == '__main__': transform = 'nodes-digest-html.xsl' else: print("StatReports: Invalid delivery mechanism in report-config") - exit(1) + raise SystemExit, 1 #IMPORTANT to add some error checking here-parseerrors @@ -278,7 +277,7 @@ if __name__ == '__main__': style = libxslt.parseStylesheetDoc(styledoc) except: print("StatReports: invalid XSLT transform file.") - exit(1) + raise SystemExit, 1 if deliverymechanism == 'mail': if delivtype == 'nodes-individual': @@ -293,6 +292,7 @@ if __name__ == '__main__': doc = libxml2.parseFile(tempfilename) result = style.applyStylesheet(doc, None) tempr.close() + os.unlink(tempfilename) del tempr try: tempfilename = mktemp() @@ -307,6 +307,7 @@ if __name__ == '__main__': tempr.close() del tempr + os.unlink(tempfilename) if not outputstring == None: toastring = '' @@ -329,6 +330,7 @@ if __name__ == '__main__': result = style.applyStylesheet(doc, None) tempr.close() del tempr + os.unlink(tempfilename) tempfilename = mktemp() tempr = open(tempfilename, "w+") style.saveResultToFile(tempr, result) @@ -336,6 +338,7 @@ if __name__ == '__main__': outputstring = tempr.read() tempr.close() del tempr + os.unlink(tempfilename) if not outputstring == None: toastring = '' @@ -358,6 +361,7 @@ if __name__ == '__main__': result = style.applyStylesheet(doc, None) tempr.close() del tempr + os.unlink(tempfilename) tempfilename = mktemp() tempr = open(tempfilename, "w+") style.saveResultToFile(tempr, result) @@ -365,6 +369,7 @@ if __name__ == '__main__': outputstring = tempr.read() tempr.close() del tempr + os.unlink(tempfilename) if deliverymechanism == 'rss': rss(outputstring, deliv, reprt) @@ -373,3 +378,4 @@ if __name__ == '__main__': style.freeStylesheet() doc.freeDoc() result.freeDoc() + |