Auto remove branch builds older than 100 days
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Tue, 3 Sep 2019 14:20:00 +0000 (16:20 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Tue, 3 Sep 2019 14:21:06 +0000 (16:21 +0200)
master_unpack.py

index 3d65dfe0ff090615f13f9026f474c00cba367367..049e562baf040e7e3e6b578761214f348fceb768 100644 (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()