From 2b9c1bfda84319aac8fbe8353847c6b2fe857aa5 Mon Sep 17 00:00:00 2001 From: niclas Date: Thu, 7 Mar 2024 15:06:15 +0100 Subject: [PATCH] Add [ailbuilder] cleanup --- other_installers/LXD/build/ailbuilder.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/other_installers/LXD/build/ailbuilder.py b/other_installers/LXD/build/ailbuilder.py index 3044c806..cf607092 100644 --- a/other_installers/LXD/build/ailbuilder.py +++ b/other_installers/LXD/build/ailbuilder.py @@ -3,6 +3,7 @@ import requests import subprocess import re import os +import shutil from time import sleep from typing import List, Optional from pathlib import Path @@ -70,6 +71,14 @@ class Repo: except Exception as e: print(f"Failed to run {cmd} for {self.id}: {e}") + def cleanup(self, num_to_keep: int) -> None: + files = os.listdir(self.outputdir) + repo_images = [f for f in files if f.startswith(self.name)] + if len(repo_images) > num_to_keep: + repo_images.sort(key=lambda x: os.path.getmtime(os.path.join(self.outputdir, x))) + for image in repo_images[:-num_to_keep]: + shutil.rmtree(os.path.join(self.outputdir, image)) + class GitHub(Repo): """Class for tracking GitHub repositories.""" @@ -126,8 +135,10 @@ def main(): while True: for repo in repos: repo.build() + repo.cleanup(num_to_keep=3) for package in aptpkg: package.build() + repo.cleanup(num_to_keep=3) sleep(config["check_interval"]) if __name__ == "__main__":