From 14f01e94863a2afa85efe6718661cc2b91b9bd14 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 3 Sep 2019 16:20:00 +0200 Subject: [PATCH] Auto remove branch builds older than 100 days --- master_unpack.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/master_unpack.py b/master_unpack.py index 3d65dfe..049e562 100644 --- a/master_unpack.py +++ b/master_unpack.py @@ -25,8 +25,11 @@ import argparse import os import shutil import sys +import time import zipfile +DOWNLOAD_DIR = "/data/www/vhosts/builder.blender.org/webroot/download/" + # Parse package filename to extract branch and platform class Package: def __init__(self, zipname): @@ -90,13 +93,12 @@ class Package: def get_download_dir(branch): - download_prefix = "/data/www/vhosts/builder.blender.org/webroot/download/" if not branch or branch == 'master': - directory = download_prefix + directory = DOWNLOAD_DIR elif branch == 'experimental-build': - directory = os.path.join(download_prefix, "experimental") + directory = os.path.join(DOWNLOAD_DIR, "experimental") else: - directory = os.path.join(download_prefix, branch) + directory = os.path.join(DOWNLOAD_DIR, branch) os.makedirs(directory, exist_ok=True) return directory @@ -157,8 +159,9 @@ def extract_zipfile_packages(zipfile, packages, branch): sys.stderr.write('Failed to unzip package: %s\n' % str(ex)) sys.exit(1) -def remove_older_packages(branch, platform, new_packages): - # Remove other files from the same platform and branch +def remove_replaced_packages(branch, platform, new_packages): + # Remove other files from the same platform and branch that are replaced + # by the new packages. directory = get_download_dir(branch) for filename in os.listdir(directory): @@ -173,8 +176,28 @@ def remove_older_packages(branch, platform, new_packages): try: os.remove(os.path.join(directory, filename)) except Exception as ex: - sys.stderr.write('Failed to remove old packages: %s\n' % str(ex)) + sys.stderr.write('Failed to remove replaced package: %s\n' % str(ex)) +def remove_old_packages(): + # Remove any branch packages that are 100 days or older. + cutoff_time = time.time() - 100 * 86400 + + for dirname in os.listdir(DOWNLOAD_DIR): + dirpath = os.path.join(DOWNLOAD_DIR, dirname) + if not os.path.isdir(dirpath): + continue + + for filename in os.listdir(dirpath): + filepath = os.path.join(dirpath, filename) + if not os.path.isfile(filepath): + continue + + file_mtime = os.stat(filepath).st_mtime + if file_mtime < cutoff_time: + try: + os.remove(filepath) + except Exception as ex: + sys.stderr.write('Failed to remove old package: %s\n' % str(ex)) if __name__ == "__main__": parser = argparse.ArgumentParser() @@ -186,4 +209,5 @@ if __name__ == "__main__": branch, platform = get_branch_platform(packages) extract_zipfile_packages(zipfile, packages, branch) - remove_older_packages(branch, platform, packages) + remove_replaced_packages(branch, platform, packages) + remove_old_packages()