urgencylog.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # vim:set et sw=4:
  3. """
  4. Urgency Logger class for dak
  5. @contact: Debian FTP Master <ftpmaster@debian.org>
  6. @copyright: 2001 - 2006 James Troup <james@nocrew.org>
  7. @copyright: 2009 Joerg Jaspert <joerg@debian.org>
  8. @license: GNU General Public License version 2 or later
  9. """
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. ###############################################################################
  22. import os
  23. import time
  24. from config import Config
  25. from utils import warn, open_file, move
  26. ###############################################################################
  27. class UrgencyLog(object):
  28. "Urgency Logger object"
  29. __shared_state = {}
  30. def __init__(self, *args, **kwargs):
  31. self.__dict__ = self.__shared_state
  32. if not getattr(self, 'initialised', False):
  33. self.initialised = True
  34. self.timestamp = time.strftime("%Y%m%d%H%M%S")
  35. cnf = Config()
  36. if cnf.has_key("Dir::UrgencyLog"):
  37. # Create the log directory if it doesn't exist
  38. self.log_dir = cnf["Dir::UrgencyLog"]
  39. if not os.path.exists(self.log_dir) or not os.access(self.log_dir, os.W_OK):
  40. warn("UrgencyLog directory %s does not exist or is not writeable, using /srv/ftp.debian.org/tmp/ instead" % (self.log_dir))
  41. self.log_dir = '/srv/ftp.debian.org/tmp/'
  42. # Open the logfile
  43. self.log_filename = "%s/.install-urgencies-%s.new" % (self.log_dir, self.timestamp)
  44. self.log_file = open_file(self.log_filename, 'w')
  45. else:
  46. self.log_dir = None
  47. self.log_filename = None
  48. self.log_file = None
  49. self.writes = 0
  50. def log(self, source, version, urgency):
  51. "Log an event"
  52. # Don't try and log if Dir::UrgencyLog is not configured
  53. if self.log_file is None:
  54. return
  55. self.log_file.write(" ".join([source, version, urgency])+'\n')
  56. self.log_file.flush()
  57. self.writes += 1
  58. def close(self):
  59. "Close a Logger object"
  60. # Don't try and log if Dir::UrgencyLog is not configured
  61. if self.log_file is None:
  62. return
  63. self.log_file.flush()
  64. self.log_file.close()
  65. if self.writes:
  66. new_filename = "%s/install-urgencies-%s" % (self.log_dir, self.timestamp)
  67. move(self.log_filename, new_filename)
  68. else:
  69. os.unlink(self.log_filename)