urgencylog.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. from __future__ import absolute_import, print_function
  23. import os
  24. import time
  25. from .config import Config
  26. from .utils import warn, open_file, move
  27. ###############################################################################
  28. class UrgencyLog(object):
  29. "Urgency Logger object"
  30. __shared_state = {}
  31. def __init__(self, *args, **kwargs):
  32. self.__dict__ = self.__shared_state
  33. if not getattr(self, 'initialised', False):
  34. self.initialised = True
  35. self.timestamp = time.strftime("%Y%m%d%H%M%S")
  36. cnf = Config()
  37. if "Dir::UrgencyLog" in cnf:
  38. # Create the log directory if it doesn't exist
  39. self.log_dir = cnf["Dir::UrgencyLog"]
  40. if not os.path.exists(self.log_dir) or not os.access(self.log_dir, os.W_OK):
  41. warn("UrgencyLog directory %s does not exist or is not writeable, using /srv/ftp.debian.org/tmp/ instead" % (self.log_dir))
  42. self.log_dir = '/srv/ftp.debian.org/tmp/'
  43. # Open the logfile
  44. self.log_filename = "%s/.install-urgencies-%s.new" % (self.log_dir, self.timestamp)
  45. self.log_file = open_file(self.log_filename, 'w')
  46. else:
  47. self.log_dir = None
  48. self.log_filename = None
  49. self.log_file = None
  50. self.writes = 0
  51. def log(self, source, version, urgency):
  52. "Log an event"
  53. # Don't try and log if Dir::UrgencyLog is not configured
  54. if self.log_file is None:
  55. return
  56. self.log_file.write(" ".join([source, version, urgency]) + '\n')
  57. self.log_file.flush()
  58. self.writes += 1
  59. def close(self):
  60. "Close a Logger object"
  61. # Don't try and log if Dir::UrgencyLog is not configured
  62. if self.log_file is None:
  63. return
  64. self.log_file.flush()
  65. self.log_file.close()
  66. if self.writes:
  67. new_filename = "%s/install-urgencies-%s" % (self.log_dir, self.timestamp)
  68. move(self.log_filename, new_filename)
  69. else:
  70. os.unlink(self.log_filename)