Show
Ignore:
Timestamp:
06/03/04 23:50:34 (20 years ago)
Author:
jalet
Message:

Improved error logging.
crashrecipient directive added.
Now exports the job's size in bytes too.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r1516 r1517  
    2222# 
    2323# $Log$ 
     24# Revision 1.95  2004/06/03 21:50:34  jalet 
     25# Improved error logging. 
     26# crashrecipient directive added. 
     27# Now exports the job's size in bytes too. 
     28# 
    2429# Revision 1.94  2004/06/03 08:51:03  jalet 
    2530# logs job's size in bytes now 
     
    391396        self.documentation = doc 
    392397        self.config = config.PyKotaConfig("/etc/pykota") 
    393         self.logger = logger.openLogger(self.config.getLoggingBackend()) 
    394398        self.debug = self.config.getDebug() 
    395         self.storage = storage.openConnection(self) 
    396399        self.smtpserver = self.config.getSMTPServer() 
    397400        self.maildomain = self.config.getMailDomain() 
     401        self.logger = logger.openLogger(self.config.getLoggingBackend()) 
     402        self.storage = storage.openConnection(self) 
    398403        self.softwareJobSize = 0 
    399404        self.softwareJobPrice = 0.0 
     
    422427        print self.documentation 
    423428        sys.exit(0) 
     429         
     430    def crashed(self, message) :     
     431        """Outputs a crash message, and optionally sends it to software author.""" 
     432        import traceback 
     433        lines = [] 
     434        for line in traceback.format_exception(*sys.exc_info()) : 
     435            lines.extend([l for l in line.split("\n") if l]) 
     436        msg = "ERROR : ".join(["%s\n" % l for l in ([message] + lines)]) 
     437        sys.stderr.write(msg) 
     438        sys.stderr.flush() 
     439        crashrecipient = self.config.getCrashRecipient() 
     440        if crashrecipient : 
     441            try : 
     442                admin = self.config.getAdminMail("global") # Nice trick, isn't it ? 
     443                fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ 
     444                                (msg, \ 
     445                                 "\n".join(["    %s" % repr(a) for a in sys.argv]), \ 
     446                                 "\n".join(["    %s=%s" % (k, v) for (k, v) in os.environ.items()])) 
     447                server = smtplib.SMTP(self.smtpserver) 
     448                server.sendmail(admin, [admin, crashrecipient], "From: %s\nTo: %s\nCc: %s\nSubject: PyKota crash traceback !\n\n%s" % (admin, crashrecipient, admin, fullmessage)) 
     449                server.quit() 
     450            except : 
     451                pass 
    424452         
    425453    def parseCommandline(self, argv, short, long, allownothing=0) : 
     
    816844        self.exportJobInfo() 
    817845        self.jobdatastream = self.openJobDataStream() 
    818         os.putenv("PYKOTAJOBSIZEBYTES", str(self.JobSizeBytes)) 
    819         self.logdebug("Job size is %s bytes" % self.JobSizeBytes) 
     846        os.environ["PYKOTAJOBSIZEBYTES"] = str(self.jobSizeBytes) 
     847        self.logdebug("Job size is %s bytes" % self.jobSizeBytes) 
    820848        self.logdebug("Capturing SIGTERM events.") 
    821849        signal.signal(signal.SIGTERM, self.sigterm_handler) 
     
    829857            self.logdebug("Duplicating data stream from stdin to temporary file") 
    830858            MEGABYTE = 1024*1024 
    831             self.JobSizeBytes = 0 
     859            self.jobSizeBytes = 0 
    832860            infile = tempfile.TemporaryFile() 
    833861            while 1 : 
     
    835863                if not data : 
    836864                    break 
    837                 self.JobSizeBytes += len(data)     
     865                self.jobSizeBytes += len(data)     
    838866                infile.write(data) 
    839867            infile.flush()     
     
    843871            # real file, just open it 
    844872            self.logdebug("Opening data stream %s" % self.preserveinputfile) 
    845             self.JobSizeBytes = os.stat(self.preserveinputfile)[6] 
     873            self.jobSizeBytes = os.stat(self.preserveinputfile)[6] 
    846874            return open(self.preserveinputfile, "rb") 
    847875         
     
    879907        """Sets an attribute whenever SIGTERM is received.""" 
    880908        self.gotSigTerm = 1 
    881         os.putenv("PYKOTASTATUS", "CANCELLED") 
     909        os.environ["PYKOTASTATUS"] = "CANCELLED" 
    882910        self.logger.log_message(_("SIGTERM received, job %s cancelled.") % self.jobid, "info") 
    883911         
    884912    def exportJobInfo(self) :     
    885913        """Exports job information to the environment.""" 
    886         os.putenv("PYKOTAUSERNAME", str(self.username)) 
    887         os.putenv("PYKOTAPRINTERNAME", str(self.printername)) 
    888         os.putenv("PYKOTAJOBID", str(self.jobid)) 
    889         os.putenv("PYKOTATITLE", self.title or "") 
    890         os.putenv("PYKOTAFILENAME", self.preserveinputfile or "") 
    891         os.putenv("PYKOTACOPIES", str(self.copies)) 
    892         os.putenv("PYKOTAOPTIONS", self.options or "") 
     914        os.environ["PYKOTAUSERNAME"] = str(self.username) 
     915        os.environ["PYKOTAPRINTERNAME"] = str(self.printername) 
     916        os.environ["PYKOTAJOBID"] = str(self.jobid) 
     917        os.environ["PYKOTATITLE"] = self.title or "" 
     918        os.environ["PYKOTAFILENAME"] = self.preserveinputfile or "" 
     919        os.environ["PYKOTACOPIES"] = str(self.copies) 
     920        os.environ["PYKOTAOPTIONS"] = self.options or "" 
    893921     
    894922    def exportUserInfo(self, userpquota) : 
    895923        """Exports user information to the environment.""" 
    896         os.putenv("PYKOTALIMITBY", str(userpquota.User.LimitBy)) 
    897         os.putenv("PYKOTABALANCE", str(userpquota.User.AccountBalance or 0.0)) 
    898         os.putenv("PYKOTALIFETIMEPAID", str(userpquota.User.LifeTimePaid or 0.0)) 
    899         os.putenv("PYKOTAPAGECOUNTER", str(userpquota.PageCounter or 0)) 
    900         os.putenv("PYKOTALIFEPAGECOUNTER", str(userpquota.LifePageCounter or 0)) 
    901         os.putenv("PYKOTASOFTLIMIT", str(userpquota.SoftLimit)) 
    902         os.putenv("PYKOTAHARDLIMIT", str(userpquota.HardLimit)) 
    903         os.putenv("PYKOTADATELIMIT", str(userpquota.DateLimit)) 
     924        os.environ["PYKOTALIMITBY"] = str(userpquota.User.LimitBy) 
     925        os.environ["PYKOTABALANCE"] = str(userpquota.User.AccountBalance or 0.0) 
     926        os.environ["PYKOTALIFETIMEPAID"] = str(userpquota.User.LifeTimePaid or 0.0) 
     927        os.environ["PYKOTAPAGECOUNTER"] = str(userpquota.PageCounter or 0) 
     928        os.environ["PYKOTALIFEPAGECOUNTER"] = str(userpquota.LifePageCounter or 0) 
     929        os.environ["PYKOTASOFTLIMIT"] = str(userpquota.SoftLimit) 
     930        os.environ["PYKOTAHARDLIMIT"] = str(userpquota.HardLimit) 
     931        os.environ["PYKOTADATELIMIT"] = str(userpquota.DateLimit) 
    904932         
    905933        # not really an user information, but anyway 
    906934        # exports the list of printers groups the current 
    907935        # printer is a member of 
    908         os.putenv("PYKOTAPGROUPS", ",".join([p.Name for p in self.storage.getParentPrinters(userpquota.Printer)])) 
    909              
     936        os.environ["PYKOTAPGROUPS"] = ",".join([p.Name for p in self.storage.getParentPrinters(userpquota.Printer)]) 
     937         
    910938    def prehook(self, userpquota) : 
    911939        """Allows plugging of an external hook before the job gets printed."""