Coverage for .tox/coverage/lib/python3.13/site-packages/wuttaweb/cleanup.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-24 16:36 -0500

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# wuttaweb -- Web App for Wutta Framework 

5# Copyright © 2024-2026 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24Cleanup for WuttaWeb 

25""" 

26 

27import os 

28import logging 

29import time 

30 

31from wuttjamaican.cleanup import Cleaner 

32 

33log = logging.getLogger(__name__) 

34 

35 

36class WuttaWebCleaner(Cleaner): 

37 """ 

38 This :term:`cleaner` removes old Beaker session files. 

39 """ 

40 

41 def cleanup(self, dry_run=False): 

42 """ 

43 This first calls :meth:`get_beaker_session_dir()` and then 

44 iterates over the contents, looking for files older than the 

45 cutoff (default is 30 days). Such files will be deleted 

46 unless ``dry_mode`` is specified. 

47 """ 

48 session_dir = self.get_beaker_session_dir() 

49 if not session_dir: 

50 log.debug("Beaker session folder could not be located") 

51 return 

52 

53 data_dir = os.path.join(session_dir, "data") 

54 lock_dir = os.path.join(session_dir, "lock") 

55 

56 # looking for files older than X days 

57 days = self.config.get_int( 

58 f"{self.config.appname}.cleanup.wuttaweb.beaker_session_cutoff_days", 

59 default=30, 

60 ) 

61 cutoff = time.time() - 3600 * 24 * days 

62 

63 for topdir in (data_dir, lock_dir): 

64 if not os.path.isdir(topdir): 

65 continue 

66 

67 for ( # pylint: disable=unused-variable 

68 dirpath, 

69 dirnames, 

70 filenames, 

71 ) in os.walk(topdir): 

72 for fname in filenames: 

73 path = os.path.join(dirpath, fname) 

74 ts = os.path.getmtime(path) 

75 if ts <= cutoff: 

76 if dry_run: 

77 log.debug("would delete file: %s", path) 

78 else: 

79 os.remove(path) 

80 log.debug("deleted file: %s", path) 

81 

82 def get_beaker_session_dir(self): 

83 """ 

84 Retrieve the path to Beaker session storage folder. 

85 

86 By default this will be at ``venv/app/cache/sessions`` but it 

87 can be overridden via config. 

88 

89 :returns: Path as string, or ``None`` if it could not be 

90 determined. 

91 """ 

92 if path := self.config.get( 

93 f"{self.config.appname}.cleanup.wuttaweb.beaker_session_dir" 

94 ): 

95 return path 

96 

97 path = self.app.get_appdir("cache", "sessions") 

98 if os.path.isdir(path): 

99 return path 

100 

101 return None