Changeset 1517 for pykota/trunk/pykota/tool.py
- Timestamp:
- 06/03/04 23:50:34 (20 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/tool.py
r1516 r1517 22 22 # 23 23 # $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 # 24 29 # Revision 1.94 2004/06/03 08:51:03 jalet 25 30 # logs job's size in bytes now … … 391 396 self.documentation = doc 392 397 self.config = config.PyKotaConfig("/etc/pykota") 393 self.logger = logger.openLogger(self.config.getLoggingBackend())394 398 self.debug = self.config.getDebug() 395 self.storage = storage.openConnection(self)396 399 self.smtpserver = self.config.getSMTPServer() 397 400 self.maildomain = self.config.getMailDomain() 401 self.logger = logger.openLogger(self.config.getLoggingBackend()) 402 self.storage = storage.openConnection(self) 398 403 self.softwareJobSize = 0 399 404 self.softwareJobPrice = 0.0 … … 422 427 print self.documentation 423 428 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 424 452 425 453 def parseCommandline(self, argv, short, long, allownothing=0) : … … 816 844 self.exportJobInfo() 817 845 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) 820 848 self.logdebug("Capturing SIGTERM events.") 821 849 signal.signal(signal.SIGTERM, self.sigterm_handler) … … 829 857 self.logdebug("Duplicating data stream from stdin to temporary file") 830 858 MEGABYTE = 1024*1024 831 self. JobSizeBytes = 0859 self.jobSizeBytes = 0 832 860 infile = tempfile.TemporaryFile() 833 861 while 1 : … … 835 863 if not data : 836 864 break 837 self. JobSizeBytes += len(data)865 self.jobSizeBytes += len(data) 838 866 infile.write(data) 839 867 infile.flush() … … 843 871 # real file, just open it 844 872 self.logdebug("Opening data stream %s" % self.preserveinputfile) 845 self. JobSizeBytes = os.stat(self.preserveinputfile)[6]873 self.jobSizeBytes = os.stat(self.preserveinputfile)[6] 846 874 return open(self.preserveinputfile, "rb") 847 875 … … 879 907 """Sets an attribute whenever SIGTERM is received.""" 880 908 self.gotSigTerm = 1 881 os. putenv("PYKOTASTATUS", "CANCELLED")909 os.environ["PYKOTASTATUS"] = "CANCELLED" 882 910 self.logger.log_message(_("SIGTERM received, job %s cancelled.") % self.jobid, "info") 883 911 884 912 def exportJobInfo(self) : 885 913 """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 "" 893 921 894 922 def exportUserInfo(self, userpquota) : 895 923 """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) 904 932 905 933 # not really an user information, but anyway 906 934 # exports the list of printers groups the current 907 935 # 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 910 938 def prehook(self, userpquota) : 911 939 """Allows plugging of an external hook before the job gets printed."""