|
| 1 | +#! python3 # noqa: E265 |
| 2 | + |
| 3 | +""" |
| 4 | + QDT autocleaner. |
| 5 | +
|
| 6 | + Author: Julien Moura (https://github.com/guts) |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +# ############################################################################# |
| 11 | +# ########## Libraries ############# |
| 12 | +# ################################## |
| 13 | + |
| 14 | +# Standard library |
| 15 | +import logging |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +# package |
| 19 | +from qgis_deployment_toolbelt.jobs.generic_job import GenericJob |
| 20 | +from qgis_deployment_toolbelt.utils.file_stats import is_file_older_than |
| 21 | +from qgis_deployment_toolbelt.utils.trash_or_delete import move_files_to_trash_or_delete |
| 22 | + |
| 23 | +# ############################################################################# |
| 24 | +# ########## Globals ############### |
| 25 | +# ################################## |
| 26 | + |
| 27 | +# logs |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +# ############################################################################# |
| 32 | +# ########## Classes ############### |
| 33 | +# ################################## |
| 34 | + |
| 35 | + |
| 36 | +class JobAutoclean(GenericJob): |
| 37 | + """ |
| 38 | + Job to clean expired QDT resources (logs, plugins archives...) which are older than |
| 39 | + a specified frequency. |
| 40 | + """ |
| 41 | + |
| 42 | + ID: str = "qdt-autoclean" |
| 43 | + OPTIONS_SCHEMA: dict = { |
| 44 | + "delay": { |
| 45 | + "type": int, |
| 46 | + "required": False, |
| 47 | + "default": 730, |
| 48 | + "possible_values": range(1, 1000), |
| 49 | + "condition": None, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + def __init__(self, options: dict) -> None: |
| 54 | + """Instantiate the class. |
| 55 | +
|
| 56 | + :param dict options: job options. |
| 57 | + """ |
| 58 | + super().__init__() |
| 59 | + self.options: dict = self.validate_options(options) |
| 60 | + |
| 61 | + def run(self) -> None: |
| 62 | + """Execute job logic.""" |
| 63 | + li_files_to_be_deleted: list[Path] = [] |
| 64 | + |
| 65 | + # clean logs |
| 66 | + for log_file in self.qdt_logs_folder.glob("*.log"): |
| 67 | + if is_file_older_than( |
| 68 | + local_file_path=log_file, |
| 69 | + expiration_rotating_hours=self.options.get("delay", 730), |
| 70 | + ): |
| 71 | + logger.debug(f"Autoclean - LOGS - Outdated file: {log_file}") |
| 72 | + li_files_to_be_deleted.append(log_file) |
| 73 | + |
| 74 | + # clean plugins archives |
| 75 | + for plugin_archive in self.qdt_plugins_folder.glob("*.zip"): |
| 76 | + if is_file_older_than( |
| 77 | + local_file_path=plugin_archive, |
| 78 | + expiration_rotating_hours=self.options.get("delay", 730), |
| 79 | + ): |
| 80 | + logger.debug( |
| 81 | + f"Autoclean - PLUGIN ARCHIVE - Outdated file: {plugin_archive}" |
| 82 | + ) |
| 83 | + li_files_to_be_deleted.append(plugin_archive) |
| 84 | + |
| 85 | + move_files_to_trash_or_delete(files_to_trash=li_files_to_be_deleted) |
0 commit comments