Auto remove branch builds older than 100 days

This commit is contained in:
2019-09-03 16:20:00 +02:00
parent ab5646a13e
commit 14f01e9486

View File

@@ -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()