Debian Codegraph @

src/CedarBackup3/actions/purge.py

raw
1 # vim: set ft=python ts=4 sw=4 expandtab:
2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
3 #
4 # C E D A R
5 # S O L U T I O N S "Software done right."
6 # S O F T W A R E
7 #
8 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
9 #
10 # Copyright (c) 2004-2007,2010,2015 Kenneth J. Pronovici.
11 # All rights reserved.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License,
15 # Version 2, as published by the Free Software Foundation.
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.
20 #
21 # Copies of the GNU General Public License are available from
22 # the Free Software Foundation website, http://www.gnu.org/.
23 #
24 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
25 #
26 # Author : Kenneth J. Pronovici <[email protected]>
27 # Language : Python 3
28 # Project : Cedar Backup, release 3
29 # Purpose : Implements the standard 'purge' action.
30 #
31 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
32
33 ########################################################################
34 # Module documentation
35 ########################################################################
36
37 """
38 Implements the standard 'purge' action.
39 :author: Kenneth J. Pronovici <[email protected]>
40 """
41
42
43 ########################################################################
44 # Imported modules
45 ########################################################################
46
47 import logging
48
49 from CedarBackup3.filesystem import PurgeItemList
50
51 ########################################################################
52 # Module-wide constants and variables
53 ########################################################################
54
55 logger = logging.getLogger("CedarBackup3.log.actions.purge")
56
57
58 ########################################################################
59 # Public functions
60 ########################################################################
61
62 ##########################
63 # executePurge() function
64 ##########################
65
66
67 def executePurge(configPath, options, config): # ruff: ignore[unused-function-argument]
68 """
69 Executes the purge backup action.
70
71 For each configured directory, we create a purge item list, remove from the
72 list anything that's younger than the configured retain days value, and then
73 purge from the filesystem what's left.
74
75 Args:
76 configPath (String representing a path on disk): Path to configuration file on disk
77 options (Options object): Program command-line options
78 config (Config object): Program configuration
79 Raises:
80 ValueError: Under many generic error conditions
81 """
82 logger.debug("Executing the 'purge' action.")
83 if config.options is None or config.purge is None:
84 raise ValueError("Purge configuration is not properly filled in.")
85 if config.purge.purgeDirs is not None:
86 for purgeDir in config.purge.purgeDirs:
87 purgeList = PurgeItemList()
88 purgeList.addDirContents(purgeDir.absolutePath) # add everything within directory
89 purgeList.removeYoungFiles(purgeDir.retainDays) # remove young files *from the list* so they won't be purged
90 purgeList.purgeItems() # remove remaining items from the filesystem
91 logger.info("Executed the 'purge' action successfully.")