ca-certificates: update to version 20190110
[openwrt/staging/dedeckeh.git] / scripts / dl_cleanup.py
index 7adc475d4da5d2812ba913843468f6b88402b2db..d086761dc71a3b5dfd30140071673f9f3cde411b 100755 (executable)
@@ -1,23 +1,24 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
-# OpenWRT download directory cleanup utility.
+# OpenWrt download directory cleanup utility.
 # Delete all but the very last version of the program tarballs.
 #
-# Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
+# Copyright (C) 2010-2015 Michael Buesch <m@bues.ch>
+# Copyright (C) 2013-2015 OpenWrt.org
 """
 
+from __future__ import print_function
+
 import sys
 import os
 import re
 import getopt
 
-DEBUG = 0
-
 # Commandline options
 opt_dryrun = False
 
 
-def parseVer_1234(match):
+def parseVer_1234(match, filepath):
        progname = match.group(1)
        progversion = (int(match.group(2)) << 64) |\
                      (int(match.group(3)) << 48) |\
@@ -25,11 +26,11 @@ def parseVer_1234(match):
                      (int(match.group(5)) << 16)
        return (progname, progversion)
 
-def parseVer_123(match):
+def parseVer_123(match, filepath):
        progname = match.group(1)
        try:
                patchlevel = match.group(5)
-       except (IndexError), e:
+       except IndexError as e:
                patchlevel = None
        if patchlevel:
                patchlevel = ord(patchlevel[0])
@@ -41,11 +42,11 @@ def parseVer_123(match):
                      patchlevel
        return (progname, progversion)
 
-def parseVer_12(match):
+def parseVer_12(match, filepath):
        progname = match.group(1)
        try:
                patchlevel = match.group(4)
-       except (IndexError), e:
+       except IndexError as e:
                patchlevel = None
        if patchlevel:
                patchlevel = ord(patchlevel[0])
@@ -56,31 +57,41 @@ def parseVer_12(match):
                      patchlevel
        return (progname, progversion)
 
-def parseVer_r(match):
+def parseVer_r(match, filepath):
        progname = match.group(1)
        progversion = (int(match.group(2)) << 64)
        return (progname, progversion)
 
-def parseVer_ymd(match):
+def parseVer_ymd(match, filepath):
        progname = match.group(1)
        progversion = (int(match.group(2)) << 64) |\
                      (int(match.group(3)) << 48) |\
                      (int(match.group(4)) << 32)
        return (progname, progversion)
 
+def parseVer_GIT(match, filepath):
+       progname = match.group(1)
+       st = os.stat(filepath)
+       progversion = int(st.st_mtime) << 64
+       return (progname, progversion)
+
 extensions = (
        ".tar.gz",
        ".tar.bz2",
+       ".tar.xz",
        ".orig.tar.gz",
        ".orig.tar.bz2",
+       ".orig.tar.xz",
        ".zip",
        ".tgz",
        ".tbz",
+       ".txz",
 )
 
 versionRegex = (
        (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234),     # xxx-1.2.3.4
        (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd),      # xxx-YYYY-MM-DD
+       (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT),            # xxx-GIT_SHASUM
        (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123),        # xxx-1.2.3a
        (re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123),               # xxx-1_2_3
        (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12),                # xxx-1.2a
@@ -88,13 +99,13 @@ versionRegex = (
 )
 
 blacklist = [
-       ("linux",               re.compile(r"linux-.*")),
+       ("linux",               re.compile(r"linux-\d.*")),
        ("gcc",                 re.compile(r"gcc-.*")),
        ("wl_apsta",            re.compile(r"wl_apsta.*")),
        (".fw",                 re.compile(r".*\.fw")),
        (".arm",                re.compile(r".*\.arm")),
        (".bin",                re.compile(r".*\.bin")),
-       ("rt-firmware",         re.compile(r"RT\d+_Firmware.*")),
+       ("rt-firmware",         re.compile(r"RT[\d\w]+_Firmware.*")),
 ]
 
 class EntryParseError(Exception): pass
@@ -104,44 +115,45 @@ class Entry:
                self.directory = directory
                self.filename = filename
                self.progname = ""
+               self.fileext = ""
 
                for ext in extensions:
                        if filename.endswith(ext):
                                filename = filename[0:0-len(ext)]
+                               self.fileext = ext
                                break
                else:
-                       if DEBUG:
-                               print "Extension did not match on", filename
+                       print(self.filename, "has an unknown file-extension")
                        raise EntryParseError("ext")
                for (regex, parseVersion) in versionRegex:
                        match = regex.match(filename)
                        if match:
-                               (self.progname, self.version) = parseVersion(match)
+                               (self.progname, self.version) = parseVersion(
+                                       match, directory + "/" + filename + self.fileext)
                                break
                else:
-                       if DEBUG:
-                               print "Version regex did not match on", filename
+                       print(self.filename, "has an unknown version pattern")
                        raise EntryParseError("ver")
 
+       def getPath(self):
+               return (self.directory + "/" + self.filename).replace("//", "/")
+
        def deleteFile(self):
-               path = (self.directory + "/" + self.filename).replace("//", "/")
-               print "Deleting", path
+               path = self.getPath()
+               print("Deleting", path)
                if not opt_dryrun:
                        os.unlink(path)
 
-       def __eq__(self, y):
-               return self.filename == y.filename
-
        def __ge__(self, y):
                return self.version >= y.version
 
 def usage():
-       print "OpenWRT download directory cleanup utility"
-       print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
-       print ""
-       print " -d|--dry-run            Do a dry-run. Don't delete any files"
-       print " -B|--show-blacklist     Show the blacklist and exit"
-       print " -w|--whitelist ITEM     Remove ITEM from blacklist"
+       print("OpenWrt download directory cleanup utility")
+       print("Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>")
+       print("")
+       print(" -d|--dry-run            Do a dry-run. Don't delete any files")
+       print(" -B|--show-blacklist     Show the blacklist and exit")
+       print(" -w|--whitelist ITEM     Remove ITEM from blacklist")
 
 def main(argv):
        global opt_dryrun
@@ -151,8 +163,9 @@ def main(argv):
                        "hdBw:",
                        [ "help", "dry-run", "show-blacklist", "whitelist=", ])
                if len(args) != 1:
-                       raise getopt.GetoptError()
-       except getopt.GetoptError:
+                       usage()
+                       return 1
+       except getopt.GetoptError as e:
                usage()
                return 1
        directory = args[0]
@@ -169,12 +182,15 @@ def main(argv):
                                        del blacklist[i]
                                        break
                        else:
-                               print "Whitelist error: Item", v,\
-                                     "is not in blacklist"
+                               print("Whitelist error: Item", v,\
+                                     "is not in blacklist")
                                return 1
                if o in ("-B", "--show-blacklist"):
                        for (name, regex) in blacklist:
-                               print name
+                               sep = "\t\t"
+                               if len(name) >= 8:
+                                       sep = "\t"
+                               print("%s%s(%s)" % (name, sep, regex.pattern))
                        return 0
 
        # Create a directory listing and parse the file names.
@@ -185,12 +201,13 @@ def main(argv):
                for (name, regex) in blacklist:
                        if regex.match(filename):
                                if opt_dryrun:
-                                       print filename, "is blacklisted"
+                                       print(filename, "is blacklisted")
                                break
                else:
                        try:
                                entries.append(Entry(directory, filename))
-                       except (EntryParseError), e: pass
+                       except EntryParseError as e:
+                               pass
 
        # Create a map of programs
        progmap = {}
@@ -209,10 +226,10 @@ def main(argv):
                                lastVersion = version
                if lastVersion:
                        for version in versions:
-                               if version != lastVersion:
+                               if version is not lastVersion:
                                        version.deleteFile()
                        if opt_dryrun:
-                               print "Keeping", lastVersion.filename
+                               print("Keeping", lastVersion.getPath())
 
        return 0