2015-10-07 10:03:16 +00:00
|
|
|
#!/usr/bin/env python3
|
2010-01-03 13:26:30 +00:00
|
|
|
"""
|
2014-12-07 16:53:30 +00:00
|
|
|
# OpenWrt download directory cleanup utility.
|
2010-01-03 13:26:30 +00:00
|
|
|
# Delete all but the very last version of the program tarballs.
|
|
|
|
#
|
2015-10-07 10:03:16 +00:00
|
|
|
# Copyright (C) 2010-2015 Michael Buesch <m@bues.ch>
|
|
|
|
# Copyright (C) 2013-2015 OpenWrt.org
|
2010-01-03 13:26:30 +00:00
|
|
|
"""
|
|
|
|
|
2015-10-07 10:03:16 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
2010-01-09 18:06:54 +00:00
|
|
|
import getopt
|
2010-01-03 13:26:30 +00:00
|
|
|
|
2010-01-09 18:06:54 +00:00
|
|
|
# Commandline options
|
|
|
|
opt_dryrun = False
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_1234(match, filepath):
|
2010-01-03 13:26:30 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
progversion = (int(match.group(2)) << 64) |\
|
|
|
|
(int(match.group(3)) << 48) |\
|
|
|
|
(int(match.group(4)) << 32) |\
|
|
|
|
(int(match.group(5)) << 16)
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_123(match, filepath):
|
2010-01-03 13:26:30 +00:00
|
|
|
progname = match.group(1)
|
2010-01-09 18:45:05 +00:00
|
|
|
try:
|
|
|
|
patchlevel = match.group(5)
|
2015-10-07 10:03:16 +00:00
|
|
|
except IndexError as e:
|
2010-01-09 18:45:05 +00:00
|
|
|
patchlevel = None
|
2010-01-03 13:26:30 +00:00
|
|
|
if patchlevel:
|
|
|
|
patchlevel = ord(patchlevel[0])
|
|
|
|
else:
|
|
|
|
patchlevel = 0
|
|
|
|
progversion = (int(match.group(2)) << 64) |\
|
|
|
|
(int(match.group(3)) << 48) |\
|
|
|
|
(int(match.group(4)) << 32) |\
|
|
|
|
patchlevel
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_12(match, filepath):
|
2010-01-03 13:26:30 +00:00
|
|
|
progname = match.group(1)
|
2010-01-09 18:45:05 +00:00
|
|
|
try:
|
|
|
|
patchlevel = match.group(4)
|
2015-10-07 10:03:16 +00:00
|
|
|
except IndexError as e:
|
2010-01-09 18:45:05 +00:00
|
|
|
patchlevel = None
|
2010-01-03 13:26:30 +00:00
|
|
|
if patchlevel:
|
|
|
|
patchlevel = ord(patchlevel[0])
|
|
|
|
else:
|
|
|
|
patchlevel = 0
|
|
|
|
progversion = (int(match.group(2)) << 64) |\
|
|
|
|
(int(match.group(3)) << 48) |\
|
|
|
|
patchlevel
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_r(match, filepath):
|
2010-01-03 13:26:30 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
progversion = (int(match.group(2)) << 64)
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_ymd(match, filepath):
|
2010-01-03 13:26:30 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
progversion = (int(match.group(2)) << 64) |\
|
|
|
|
(int(match.group(3)) << 48) |\
|
|
|
|
(int(match.group(4)) << 32)
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_GIT(match, filepath):
|
|
|
|
progname = match.group(1)
|
|
|
|
st = os.stat(filepath)
|
|
|
|
progversion = int(st.st_mtime) << 64
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
extensions = (
|
|
|
|
".tar.gz",
|
|
|
|
".tar.bz2",
|
2013-02-17 21:37:14 +00:00
|
|
|
".tar.xz",
|
2010-01-03 13:26:30 +00:00
|
|
|
".orig.tar.gz",
|
|
|
|
".orig.tar.bz2",
|
2013-02-17 21:37:14 +00:00
|
|
|
".orig.tar.xz",
|
2010-01-03 13:26:30 +00:00
|
|
|
".zip",
|
|
|
|
".tgz",
|
|
|
|
".tbz",
|
2013-02-17 21:37:14 +00:00
|
|
|
".txz",
|
2010-01-03 13:26:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2013-05-26 17:28:11 +00:00
|
|
|
(re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
|
2010-01-03 13:26:30 +00:00
|
|
|
(re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
|
2010-01-09 18:45:05 +00:00
|
|
|
(re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
|
2010-01-03 13:26:30 +00:00
|
|
|
(re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
|
|
|
|
(re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
|
|
|
|
)
|
|
|
|
|
2010-01-09 18:31:29 +00:00
|
|
|
blacklist = [
|
2015-10-07 10:51:18 +00:00
|
|
|
("linux", re.compile(r"linux-\d.*")),
|
2010-01-09 18:19:29 +00:00
|
|
|
("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")),
|
2010-01-09 18:58:28 +00:00
|
|
|
("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
|
2010-01-09 18:31:29 +00:00
|
|
|
]
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
class EntryParseError(Exception): pass
|
|
|
|
|
|
|
|
class Entry:
|
|
|
|
def __init__(self, directory, filename):
|
|
|
|
self.directory = directory
|
|
|
|
self.filename = filename
|
|
|
|
self.progname = ""
|
2010-09-18 19:19:34 +00:00
|
|
|
self.fileext = ""
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
for ext in extensions:
|
|
|
|
if filename.endswith(ext):
|
|
|
|
filename = filename[0:0-len(ext)]
|
2010-09-18 19:19:34 +00:00
|
|
|
self.fileext = ext
|
2010-01-03 13:26:30 +00:00
|
|
|
break
|
|
|
|
else:
|
2015-10-07 10:03:16 +00:00
|
|
|
print(self.filename, "has an unknown file-extension")
|
2010-01-03 13:26:30 +00:00
|
|
|
raise EntryParseError("ext")
|
|
|
|
for (regex, parseVersion) in versionRegex:
|
|
|
|
match = regex.match(filename)
|
|
|
|
if match:
|
2010-09-18 19:19:34 +00:00
|
|
|
(self.progname, self.version) = parseVersion(
|
|
|
|
match, directory + "/" + filename + self.fileext)
|
2010-01-03 13:26:30 +00:00
|
|
|
break
|
|
|
|
else:
|
2015-10-07 10:03:16 +00:00
|
|
|
print(self.filename, "has an unknown version pattern")
|
2010-01-03 13:26:30 +00:00
|
|
|
raise EntryParseError("ver")
|
|
|
|
|
2015-10-07 10:03:16 +00:00
|
|
|
def getPath(self):
|
|
|
|
return (self.directory + "/" + self.filename).replace("//", "/")
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
def deleteFile(self):
|
2015-10-07 10:03:16 +00:00
|
|
|
path = self.getPath()
|
|
|
|
print("Deleting", path)
|
2010-01-09 18:06:54 +00:00
|
|
|
if not opt_dryrun:
|
2010-01-03 13:26:30 +00:00
|
|
|
os.unlink(path)
|
|
|
|
|
|
|
|
def __ge__(self, y):
|
|
|
|
return self.version >= y.version
|
|
|
|
|
|
|
|
def usage():
|
2015-10-07 10:03:16 +00:00
|
|
|
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")
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
def main(argv):
|
2010-01-09 18:06:54 +00:00
|
|
|
global opt_dryrun
|
|
|
|
|
|
|
|
try:
|
|
|
|
(opts, args) = getopt.getopt(argv[1:],
|
2010-01-09 18:31:29 +00:00
|
|
|
"hdBw:",
|
|
|
|
[ "help", "dry-run", "show-blacklist", "whitelist=", ])
|
2010-01-09 18:06:54 +00:00
|
|
|
if len(args) != 1:
|
2010-08-10 15:08:02 +00:00
|
|
|
usage()
|
|
|
|
return 1
|
2015-10-07 10:03:16 +00:00
|
|
|
except getopt.GetoptError as e:
|
2010-01-03 13:26:30 +00:00
|
|
|
usage()
|
|
|
|
return 1
|
2010-01-09 18:06:54 +00:00
|
|
|
directory = args[0]
|
2021-07-05 22:59:40 +00:00
|
|
|
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
print("Can't find dl path", directory)
|
|
|
|
return 1
|
|
|
|
|
2010-01-09 18:06:54 +00:00
|
|
|
for (o, v) in opts:
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
usage()
|
|
|
|
return 0
|
|
|
|
if o in ("-d", "--dry-run"):
|
|
|
|
opt_dryrun = True
|
2010-01-09 18:31:29 +00:00
|
|
|
if o in ("-w", "--whitelist"):
|
|
|
|
for i in range(0, len(blacklist)):
|
|
|
|
(name, regex) = blacklist[i]
|
|
|
|
if name == v:
|
|
|
|
del blacklist[i]
|
|
|
|
break
|
|
|
|
else:
|
2015-10-07 10:03:16 +00:00
|
|
|
print("Whitelist error: Item", v,\
|
|
|
|
"is not in blacklist")
|
2010-01-09 18:31:29 +00:00
|
|
|
return 1
|
2010-01-09 18:19:29 +00:00
|
|
|
if o in ("-B", "--show-blacklist"):
|
|
|
|
for (name, regex) in blacklist:
|
2015-10-07 10:51:18 +00:00
|
|
|
sep = "\t\t"
|
|
|
|
if len(name) >= 8:
|
|
|
|
sep = "\t"
|
|
|
|
print("%s%s(%s)" % (name, sep, regex.pattern))
|
2010-01-09 18:19:29 +00:00
|
|
|
return 0
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
# Create a directory listing and parse the file names.
|
|
|
|
entries = []
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
if filename == "." or filename == "..":
|
|
|
|
continue
|
2010-01-09 18:19:29 +00:00
|
|
|
for (name, regex) in blacklist:
|
|
|
|
if regex.match(filename):
|
2010-01-09 18:06:54 +00:00
|
|
|
if opt_dryrun:
|
2015-10-07 10:03:16 +00:00
|
|
|
print(filename, "is blacklisted")
|
2010-01-03 13:26:30 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
entries.append(Entry(directory, filename))
|
2015-10-07 10:03:16 +00:00
|
|
|
except EntryParseError as e:
|
|
|
|
pass
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
# Create a map of programs
|
|
|
|
progmap = {}
|
|
|
|
for entry in entries:
|
|
|
|
if entry.progname in progmap.keys():
|
|
|
|
progmap[entry.progname].append(entry)
|
|
|
|
else:
|
|
|
|
progmap[entry.progname] = [entry,]
|
|
|
|
|
|
|
|
# Traverse the program map and delete everything but the last version
|
|
|
|
for prog in progmap:
|
|
|
|
lastVersion = None
|
|
|
|
versions = progmap[prog]
|
|
|
|
for version in versions:
|
|
|
|
if lastVersion is None or version >= lastVersion:
|
|
|
|
lastVersion = version
|
|
|
|
if lastVersion:
|
|
|
|
for version in versions:
|
2015-10-07 10:03:16 +00:00
|
|
|
if version is not lastVersion:
|
2010-01-03 13:26:30 +00:00
|
|
|
version.deleteFile()
|
2010-01-09 18:06:54 +00:00
|
|
|
if opt_dryrun:
|
2015-10-07 10:03:16 +00:00
|
|
|
print("Keeping", lastVersion.getPath())
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv))
|