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
|
2021-07-06 01:28:55 +00:00
|
|
|
import shutil
|
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):
|
2021-07-06 01:20:40 +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-01-03 13:26:30 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_123(match, filepath):
|
2021-07-06 01:20:40 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
try:
|
|
|
|
patchlevel = match.group(5)
|
|
|
|
except IndexError as e:
|
|
|
|
patchlevel = None
|
|
|
|
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-01-03 13:26:30 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_12(match, filepath):
|
2021-07-06 01:20:40 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
try:
|
|
|
|
patchlevel = match.group(4)
|
|
|
|
except IndexError as e:
|
|
|
|
patchlevel = None
|
|
|
|
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-01-03 13:26:30 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_r(match, filepath):
|
2021-07-06 01:20:40 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
progversion = int(match.group(2)) << 64
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
2021-07-05 22:56:21 +00:00
|
|
|
def parseVer_ymd_GIT_SHASUM(match, filepath):
|
2021-07-06 01:20:40 +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)
|
|
|
|
|
2021-07-05 22:56:21 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_ymd(match, filepath):
|
2021-07-06 01:20:40 +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-01-03 13:26:30 +00:00
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
def parseVer_GIT(match, filepath):
|
2021-07-06 01:20:40 +00:00
|
|
|
progname = match.group(1)
|
|
|
|
st = os.stat(filepath)
|
|
|
|
progversion = int(st.st_mtime) << 64
|
|
|
|
return (progname, progversion)
|
|
|
|
|
2010-09-18 19:19:34 +00:00
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
extensions = (
|
2021-07-06 01:20:40 +00:00
|
|
|
".tar.gz",
|
|
|
|
".tar.bz2",
|
|
|
|
".tar.xz",
|
2024-04-06 14:02:28 +00:00
|
|
|
".tar.zst",
|
2021-07-06 01:20:40 +00:00
|
|
|
".orig.tar.gz",
|
|
|
|
".orig.tar.bz2",
|
|
|
|
".orig.tar.xz",
|
|
|
|
".zip",
|
|
|
|
".tgz",
|
|
|
|
".tbz",
|
|
|
|
".txz",
|
2010-01-03 13:26:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
versionRegex = (
|
2021-07-06 01:53:58 +00:00
|
|
|
(re.compile(r"(gcc[-_]\d+)\.(\d+)\.(\d+)"), parseVer_12), # gcc.1.2
|
|
|
|
(re.compile(r"(linux[-_]\d+\.\d+)\.(\d+)"), parseVer_r), # linux.1
|
2021-07-06 01:20:40 +00:00
|
|
|
(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_GIT_SHASUM,
|
|
|
|
), # xxx-YYYY-MM-DD-GIT_SHASUM
|
|
|
|
(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
|
2022-03-31 19:15:51 +00:00
|
|
|
(re.compile(r"(.+)[-_]v(\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-v1.2.3a
|
2021-07-06 01:20:40 +00:00
|
|
|
(re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
|
|
|
|
(re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
|
2022-03-31 19:15:51 +00:00
|
|
|
(re.compile(r"(.+)[-_]v(\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-v1.2a
|
2021-07-06 01:20:40 +00:00
|
|
|
(re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
|
2010-01-03 13:26:30 +00:00
|
|
|
)
|
|
|
|
|
2010-01-09 18:31:29 +00:00
|
|
|
blacklist = [
|
2021-07-06 01:20:40 +00:00
|
|
|
("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.*")),
|
2010-01-09 18:31:29 +00:00
|
|
|
]
|
2010-01-03 13:26:30 +00:00
|
|
|
|
2021-07-06 01:20:40 +00:00
|
|
|
|
|
|
|
class EntryParseError(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
class Entry:
|
2021-07-06 01:28:55 +00:00
|
|
|
def __init__(self, directory, builddir, filename):
|
2021-07-06 01:20:40 +00:00
|
|
|
self.directory = directory
|
|
|
|
self.filename = filename
|
2021-07-06 01:28:55 +00:00
|
|
|
self.builddir = builddir
|
2021-07-06 01:20:40 +00:00
|
|
|
self.progname = ""
|
|
|
|
self.fileext = ""
|
2021-07-06 01:28:55 +00:00
|
|
|
self.filenoext = ""
|
2021-07-06 01:20:40 +00:00
|
|
|
|
2022-09-19 20:39:34 +00:00
|
|
|
if os.path.isdir(self.getPath()):
|
|
|
|
self.filenoext = filename
|
2021-07-06 01:20:40 +00:00
|
|
|
else:
|
2022-09-19 20:39:34 +00:00
|
|
|
for ext in extensions:
|
|
|
|
if filename.endswith(ext):
|
|
|
|
filename = filename[0 : 0 - len(ext)]
|
|
|
|
self.filenoext = filename
|
|
|
|
self.fileext = ext
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print(self.filename, "has an unknown file-extension")
|
|
|
|
raise EntryParseError("ext")
|
2021-07-06 01:20:40 +00:00
|
|
|
for (regex, parseVersion) in versionRegex:
|
|
|
|
match = regex.match(filename)
|
|
|
|
if match:
|
|
|
|
(self.progname, self.version) = parseVersion(
|
|
|
|
match, directory + "/" + filename + self.fileext
|
|
|
|
)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print(self.filename, "has an unknown version pattern")
|
|
|
|
raise EntryParseError("ver")
|
|
|
|
|
|
|
|
def getPath(self):
|
|
|
|
return (self.directory + "/" + self.filename).replace("//", "/")
|
|
|
|
|
2021-07-06 01:28:55 +00:00
|
|
|
def getBuildPaths(self):
|
|
|
|
paths = []
|
|
|
|
for subdir in os.scandir(self.builddir):
|
|
|
|
package_build_dir = os.path.join(subdir.path, self.filenoext)
|
|
|
|
if os.path.exists(package_build_dir):
|
|
|
|
paths.append(package_build_dir)
|
|
|
|
return paths
|
|
|
|
|
2021-07-06 01:20:40 +00:00
|
|
|
def deleteFile(self):
|
|
|
|
path = self.getPath()
|
|
|
|
print("Deleting", path)
|
|
|
|
if not opt_dryrun:
|
2022-09-19 20:39:34 +00:00
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
else:
|
|
|
|
os.unlink(path)
|
2021-07-06 01:20:40 +00:00
|
|
|
|
2021-07-06 01:28:55 +00:00
|
|
|
def deleteBuildDir(self):
|
|
|
|
paths = self.getBuildPaths()
|
|
|
|
for path in paths:
|
|
|
|
print("Deleting BuildDir", path)
|
|
|
|
if not opt_dryrun:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
2021-07-06 01:20:40 +00:00
|
|
|
def __ge__(self, y):
|
|
|
|
return self.version >= y.version
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
def usage():
|
2021-07-06 01:20:40 +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")
|
2021-07-06 01:25:06 +00:00
|
|
|
print(
|
|
|
|
" -D|--download-dir Provide path to dl dir to clean also the build directory"
|
|
|
|
)
|
2021-07-06 01:28:55 +00:00
|
|
|
print(
|
|
|
|
" -b|--build-dir Provide path to build dir to clean also the build directory"
|
|
|
|
)
|
2021-07-06 01:20:40 +00:00
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
def main(argv):
|
2021-07-06 01:20:40 +00:00
|
|
|
global opt_dryrun
|
|
|
|
|
|
|
|
try:
|
|
|
|
(opts, args) = getopt.getopt(
|
|
|
|
argv[1:],
|
2022-11-12 08:50:36 +00:00
|
|
|
"hdBw:D:b:",
|
2021-07-06 01:20:40 +00:00
|
|
|
[
|
|
|
|
"help",
|
|
|
|
"dry-run",
|
|
|
|
"show-blacklist",
|
|
|
|
"whitelist=",
|
2021-07-06 01:25:06 +00:00
|
|
|
"download-dir=",
|
2021-07-06 01:28:55 +00:00
|
|
|
"build-dir=",
|
2021-07-06 01:20:40 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
except getopt.GetoptError as e:
|
|
|
|
usage()
|
|
|
|
return 1
|
|
|
|
|
2021-07-06 01:25:06 +00:00
|
|
|
directory = "dl/"
|
2021-07-06 01:28:55 +00:00
|
|
|
builddir = "build_dir/"
|
2021-07-06 01:20:40 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
sep = "\t\t"
|
|
|
|
if len(name) >= 8:
|
|
|
|
sep = "\t"
|
|
|
|
print("%s%s(%s)" % (name, sep, regex.pattern))
|
|
|
|
return 0
|
2021-07-06 01:25:06 +00:00
|
|
|
if o in ("-D", "--download-dir"):
|
|
|
|
directory = v
|
2021-07-06 01:28:55 +00:00
|
|
|
if o in ("-b", "--build-dir"):
|
|
|
|
builddir = v
|
2021-07-06 01:25:06 +00:00
|
|
|
|
2022-11-12 08:50:36 +00:00
|
|
|
if args:
|
|
|
|
directory = args[0]
|
|
|
|
|
2021-07-06 01:25:06 +00:00
|
|
|
if not os.path.exists(directory):
|
2022-11-12 08:50:36 +00:00
|
|
|
print("Can't find download directory", directory)
|
2021-07-06 01:25:06 +00:00
|
|
|
return 1
|
2021-07-06 01:20:40 +00:00
|
|
|
|
2021-07-06 01:28:55 +00:00
|
|
|
if not os.path.exists(builddir):
|
2022-11-12 08:50:36 +00:00
|
|
|
print("Can't find build directory", builddir)
|
2021-07-06 01:28:55 +00:00
|
|
|
return 1
|
|
|
|
|
2021-07-06 01:20:40 +00:00
|
|
|
# Create a directory listing and parse the file names.
|
|
|
|
entries = []
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
if filename == "." or filename == "..":
|
|
|
|
continue
|
|
|
|
for (name, regex) in blacklist:
|
|
|
|
if regex.match(filename):
|
|
|
|
if opt_dryrun:
|
|
|
|
print(filename, "is blacklisted")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
try:
|
2021-07-06 01:28:55 +00:00
|
|
|
entries.append(Entry(directory, builddir, filename))
|
2021-07-06 01:20:40 +00:00
|
|
|
except EntryParseError as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# 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:
|
2022-09-19 20:39:34 +00:00
|
|
|
if lastVersion:
|
|
|
|
if os.path.isdir(lastVersion.getPath()) and not os.path.isdir(version.getPath()):
|
|
|
|
continue
|
2021-07-06 01:20:40 +00:00
|
|
|
if lastVersion is None or version >= lastVersion:
|
|
|
|
lastVersion = version
|
|
|
|
if lastVersion:
|
|
|
|
for version in versions:
|
|
|
|
if version is not lastVersion:
|
|
|
|
version.deleteFile()
|
2021-07-06 01:28:55 +00:00
|
|
|
if builddir:
|
|
|
|
version.deleteBuildDir()
|
2021-07-06 01:20:40 +00:00
|
|
|
if opt_dryrun:
|
|
|
|
print("Keeping", lastVersion.getPath())
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2010-01-03 13:26:30 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-07-06 01:20:40 +00:00
|
|
|
sys.exit(main(sys.argv))
|