|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
|
|
def clean_old_logs(log_dir, max_age_seconds=86400):
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
for file_name in os.listdir(log_dir):
|
|
|
|
file_path = os.path.join(log_dir, file_name)
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
file_age = now - datetime.fromtimestamp(os.path.getmtime(file_path), tz=timezone.utc)
|
|
|
|
if file_age > timedelta(seconds=max_age_seconds):
|
|
|
|
os.remove(file_path)
|