Coverage for .tox/coverage/lib/python3.13/site-packages/wuttaweb/cleanup.py: 100%
34 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-20 15:24 -0500
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-20 15:24 -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"""
27import os
28import logging
29import time
31from wuttjamaican.cleanup import Cleaner
34log = logging.getLogger(__name__)
37class WuttaWebCleaner(Cleaner):
38 """
39 This :term:`cleaner` removes old Beaker session files.
40 """
42 def cleanup(self, dry_run=False):
43 """
44 This first calls :meth:`get_beaker_session_dir()` and then
45 iterates over the contents, looking for files older than the
46 cutoff (default is 30 days). Such files will be deleted
47 unless ``dry_mode`` is specified.
48 """
49 session_dir = self.get_beaker_session_dir()
50 if not session_dir:
51 log.debug("Beaker session folder could not be located")
52 return
54 data_dir = os.path.join(session_dir, "data")
55 lock_dir = os.path.join(session_dir, "lock")
57 # looking for files older than X days
58 days = self.config.get_int(
59 f"{self.config.appname}.cleanup.wuttaweb.beaker_session_cutoff_days",
60 default=30,
61 )
62 cutoff = time.time() - 3600 * 24 * days
64 for topdir in (data_dir, lock_dir):
65 if not os.path.isdir(topdir):
66 continue
68 for ( # pylint: disable=unused-variable
69 dirpath,
70 dirnames,
71 filenames,
72 ) in os.walk(topdir):
73 for fname in filenames:
74 path = os.path.join(dirpath, fname)
75 ts = os.path.getmtime(path)
76 if ts <= cutoff:
77 if dry_run:
78 log.debug("would delete file: %s", path)
79 else:
80 os.remove(path)
81 log.debug("deleted file: %s", path)
83 def get_beaker_session_dir(self):
84 """
85 Retrieve the path to Beaker session storage folder.
87 By default this will be at ``venv/app/cache/sessions`` but it
88 can be overridden via config.
90 :returns: Path as string, or ``None`` if it could not be
91 determined.
92 """
93 if path := self.config.get(
94 f"{self.config.appname}.cleanup.wuttaweb.beaker_session_dir"
95 ):
96 return path
98 path = self.app.get_appdir("cache", "sessions")
99 if os.path.isdir(path):
100 return path
102 return None