urgencylog.py 3.1 KB

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