Coverage for daklib/urgencylog.py: 57%

44 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-01-04 16:18 +0000

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 

30from typing import IO, Any 

31 

32from .config import Config 

33from .utils import move, warn 

34 

35############################################################################### 

36 

37 

38class UrgencyLog: 

39 "Urgency Logger object" 

40 

41 __shared_state: dict[str, Any] = {} 

42 

43 log_dir: str | None 

44 log_filename: str | None 

45 log_file: IO[str] | None 

46 

47 def __init__(self) -> None: 

48 self.__dict__ = self.__shared_state 

49 

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

51 self.initialised = True 

52 

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

54 

55 cnf = Config() 

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

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

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

59 

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

61 self.log_dir, os.W_OK 

62 ): 

63 warn( 

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

65 % (self.log_dir) 

66 ) 

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

68 

69 # Open the logfile 

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

71 self.log_dir, 

72 self.timestamp, 

73 ) 

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

75 

76 else: 

77 self.log_dir = None 

78 self.log_filename = None 

79 self.log_file = None 

80 

81 self.writes = 0 

82 

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

84 "Log an event" 

85 

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

87 if self.log_file is None: 87 ↛ 90line 87 didn't jump to line 90 because the condition on line 87 was always true

88 return 

89 

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

91 self.log_file.flush() 

92 self.writes += 1 

93 

94 def close(self) -> None: 

95 "Close a Logger object" 

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

97 if self.log_file is None: 97 ↛ 100line 97 didn't jump to line 100 because the condition on line 97 was always true

98 return 

99 

100 self.log_file.flush() 

101 self.log_file.close() 

102 

103 assert self.log_dir is not None 

104 assert self.log_filename is not None 

105 

106 if self.writes: 

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

108 move(self.log_filename, new_filename) 

109 else: 

110 os.unlink(self.log_filename)