X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/98501a1fbe3f6e39b4ebc5c0b4d07309e7b2df5c..2f01b6f43a6346f7b4dab7cc0d1c633eeecef408:/scripts/dl_cleanup.py diff --git a/scripts/dl_cleanup.py b/scripts/dl_cleanup.py index e83c82ac2..81c69f7aa 100755 --- a/scripts/dl_cleanup.py +++ b/scripts/dl_cleanup.py @@ -9,11 +9,13 @@ 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) |\ @@ -21,9 +23,12 @@ 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) - patchlevel = match.group(5) + try: + patchlevel = match.group(5) + except (IndexError), e: + patchlevel = None if patchlevel: patchlevel = ord(patchlevel[0]) else: @@ -34,9 +39,12 @@ def parseVer_123(match): patchlevel return (progname, progversion) -def parseVer_12(match): +def parseVer_12(match, filepath): progname = match.group(1) - patchlevel = match.group(4) + try: + patchlevel = match.group(4) + except (IndexError), e: + patchlevel = None if patchlevel: patchlevel = ord(patchlevel[0]) else: @@ -46,18 +54,24 @@ 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", @@ -69,21 +83,24 @@ extensions = ( ) versionRegex = ( + (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM (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"(.+)[-_](\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 (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111 ) -blacklist = ( - re.compile(r"wl_apsta.*"), - re.compile(r"boost.*"), - re.compile(r".*\.fw"), - re.compile(r".*\.arm"), - re.compile(r".*\.bin"), - re.compile(r"RT\d+_Firmware.*"), -) +blacklist = [ + ("linux", re.compile(r"linux-.*")), + ("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\w]+_Firmware.*")), +] class EntryParseError(Exception): pass @@ -92,29 +109,30 @@ 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 deleteFile(self): path = (self.directory + "/" + self.filename).replace("//", "/") print "Deleting", path - if not DEBUG: + if not opt_dryrun: os.unlink(path) def __eq__(self, y): @@ -125,22 +143,55 @@ class Entry: def usage(): print "OpenWRT download directory cleanup utility" - print "Usage: " + sys.argv[0] + " path/to/dl" + print "Usage: " + sys.argv[0] + " [OPTIONS] " + 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): - if len(argv) != 2: + global opt_dryrun + + try: + (opts, args) = getopt.getopt(argv[1:], + "hdBw:", + [ "help", "dry-run", "show-blacklist", "whitelist=", ]) + if len(args) != 1: + usage() + return 1 + except getopt.GetoptError: usage() return 1 - directory = argv[1] + directory = args[0] + for (o, v) in opts: + if o in ("-h", "--help"): + usage() + return 0 + if o in ("-d", "--dry-run"): + opt_dryrun = True + if o in ("-w", "--whitelist"): + for i in range(0, len(blacklist)): + (name, regex) = blacklist[i] + if name == v: + del blacklist[i] + break + else: + print "Whitelist error: Item", v,\ + "is not in blacklist" + return 1 + if o in ("-B", "--show-blacklist"): + for (name, regex) in blacklist: + print name + return 0 # Create a directory listing and parse the file names. entries = [] for filename in os.listdir(directory): if filename == "." or filename == "..": continue - for black in blacklist: - if black.match(filename): - if DEBUG: + for (name, regex) in blacklist: + if regex.match(filename): + if opt_dryrun: print filename, "is blacklisted" break else: @@ -167,7 +218,7 @@ def main(argv): for version in versions: if version != lastVersion: version.deleteFile() - if DEBUG: + if opt_dryrun: print "Keeping", lastVersion.filename return 0