1# vim:set et sw=4: 

2 

3""" 

4Urgency Logger class for dak 

5 

6@contact: Debian FTP Master <ftpmaster@debian.org> 

7@copyright: 2001 - 2006 James Troup <james@nocrew.org> 

8@copyright: 2009 Joerg Jaspert <joerg@debian.org> 

9@license: GNU General Public License version 2 or later 

10""" 

11 

12# This program is free software; you can redistribute it and/or modify 

13# it under the terms of the GNU General Public License as published by 

14# the Free Software Foundation; either version 2 of the License, or 

15# (at your option) any later version. 

16 

17# This program is distributed in the hope that it will be useful, 

18# but WITHOUT ANY WARRANTY; without even the implied warranty of 

19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20# GNU General Public License for more details. 

21 

22# You should have received a copy of the GNU General Public License 

23# along with this program; if not, write to the Free Software 

24# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

25 

26############################################################################### 

27 

28import os 

29import time 

30 

31from .config import Config 

32from .utils import move, warn 

33 

34############################################################################### 

35 

36 

37class UrgencyLog: 

38 "Urgency Logger object" 

39 

40 __shared_state = {} 

41 

42 def __init__(self, *args, **kwargs): 

43 self.__dict__ = self.__shared_state 

44 

45 if not getattr(self, "initialised", False): 

46 self.initialised = True 

47 

48 self.timestamp = time.strftime("%Y%m%d%H%M%S") 

49 

50 cnf = Config() 

51 if "Dir::UrgencyLog" in cnf: 51 ↛ 53line 51 didn't jump to line 53, because the condition on line 51 was never true

52 # Create the log directory if it doesn't exist 

53 self.log_dir = cnf["Dir::UrgencyLog"] 

54 

55 if not os.path.exists(self.log_dir) or not os.access( 

56 self.log_dir, os.W_OK 

57 ): 

58 warn( 

59 "UrgencyLog directory %s does not exist or is not writeable, using /srv/ftp.debian.org/tmp/ instead" 

60 % (self.log_dir) 

61 ) 

62 self.log_dir = "/srv/ftp.debian.org/tmp/" 

63 

64 # Open the logfile 

65 self.log_filename = "%s/.install-urgencies-%s.new" % ( 

66 self.log_dir, 

67 self.timestamp, 

68 ) 

69 self.log_file = open(self.log_filename, "w") 

70 

71 else: 

72 self.log_dir = None 

73 self.log_filename = None 

74 self.log_file = None 

75 

76 self.writes = 0 

77 

78 def log(self, source: str, version: str, urgency: str) -> None: 

79 "Log an event" 

80 

81 # Don't try and log if Dir::UrgencyLog is not configured 

82 if self.log_file is None: 82 ↛ 85line 82 didn't jump to line 85, because the condition on line 82 was never false

83 return 

84 

85 self.log_file.write(" ".join([source, version, urgency]) + "\n") 

86 self.log_file.flush() 

87 self.writes += 1 

88 

89 def close(self) -> None: 

90 "Close a Logger object" 

91 # Don't try and log if Dir::UrgencyLog is not configured 

92 if self.log_file is None: 92 ↛ 95line 92 didn't jump to line 95, because the condition on line 92 was never false

93 return 

94 

95 self.log_file.flush() 

96 self.log_file.close() 

97 

98 if self.writes: 

99 new_filename = "%s/install-urgencies-%s" % (self.log_dir, self.timestamp) 

100 move(self.log_filename, new_filename) 

101 else: 

102 os.unlink(self.log_filename)