Skip to content

Commit 19d2f51

Browse files
committed
feature: add job to autoclean QDT expired resources
1 parent b47cb13 commit 19d2f51

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

qgis_deployment_toolbelt/jobs/generic_job.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
from sys import platform as opersys
1919

2020
# package
21-
from qgis_deployment_toolbelt.constants import OS_CONFIG, get_qdt_working_directory
21+
from qgis_deployment_toolbelt.constants import (
22+
OS_CONFIG,
23+
get_qdt_logs_folder,
24+
get_qdt_working_directory,
25+
)
2226
from qgis_deployment_toolbelt.exceptions import (
2327
JobOptionBadName,
2428
JobOptionBadValue,
@@ -60,7 +64,7 @@ def __init__(self) -> None:
6064
f"repositories/{getenv('QDT_TMP_RUNNING_SCENARIO_ID', 'default')}"
6165
)
6266
self.qdt_plugins_folder = self.qdt_working_folder.joinpath("plugins")
63-
67+
self.qdt_logs_folder = get_qdt_logs_folder()
6468
# destination profiles folder
6569
self.qgis_profiles_path: Path = Path(OS_CONFIG.get(opersys).profiles_path)
6670
if not self.qgis_profiles_path.exists():
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)