mirror of
https://github.com/openwrt/openwrt.git
synced 2024-12-24 15:56:49 +00:00
scripts: format dl_cleanup to black format python style
Forma dl_cleanup python script to black style. Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
This commit is contained in:
parent
4eb4c3c469
commit
ff875876da
@ -19,232 +19,263 @@ opt_dryrun = False
|
|||||||
|
|
||||||
|
|
||||||
def parseVer_1234(match, filepath):
|
def parseVer_1234(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
progversion = (int(match.group(2)) << 64) |\
|
progversion = (
|
||||||
(int(match.group(3)) << 48) |\
|
(int(match.group(2)) << 64)
|
||||||
(int(match.group(4)) << 32) |\
|
| (int(match.group(3)) << 48)
|
||||||
(int(match.group(5)) << 16)
|
| (int(match.group(4)) << 32)
|
||||||
return (progname, progversion)
|
| (int(match.group(5)) << 16)
|
||||||
|
)
|
||||||
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
def parseVer_123(match, filepath):
|
def parseVer_123(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
try:
|
try:
|
||||||
patchlevel = match.group(5)
|
patchlevel = match.group(5)
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
patchlevel = None
|
patchlevel = None
|
||||||
if patchlevel:
|
if patchlevel:
|
||||||
patchlevel = ord(patchlevel[0])
|
patchlevel = ord(patchlevel[0])
|
||||||
else:
|
else:
|
||||||
patchlevel = 0
|
patchlevel = 0
|
||||||
progversion = (int(match.group(2)) << 64) |\
|
progversion = (
|
||||||
(int(match.group(3)) << 48) |\
|
(int(match.group(2)) << 64)
|
||||||
(int(match.group(4)) << 32) |\
|
| (int(match.group(3)) << 48)
|
||||||
patchlevel
|
| (int(match.group(4)) << 32)
|
||||||
return (progname, progversion)
|
| patchlevel
|
||||||
|
)
|
||||||
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
def parseVer_12(match, filepath):
|
def parseVer_12(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
try:
|
try:
|
||||||
patchlevel = match.group(4)
|
patchlevel = match.group(4)
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
patchlevel = None
|
patchlevel = None
|
||||||
if patchlevel:
|
if patchlevel:
|
||||||
patchlevel = ord(patchlevel[0])
|
patchlevel = ord(patchlevel[0])
|
||||||
else:
|
else:
|
||||||
patchlevel = 0
|
patchlevel = 0
|
||||||
progversion = (int(match.group(2)) << 64) |\
|
progversion = (int(match.group(2)) << 64) | (int(match.group(3)) << 48) | patchlevel
|
||||||
(int(match.group(3)) << 48) |\
|
return (progname, progversion)
|
||||||
patchlevel
|
|
||||||
return (progname, progversion)
|
|
||||||
|
|
||||||
def parseVer_r(match, filepath):
|
def parseVer_r(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
progversion = (int(match.group(2)) << 64)
|
progversion = int(match.group(2)) << 64
|
||||||
return (progname, progversion)
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
def parseVer_ymd_GIT_SHASUM(match, filepath):
|
def parseVer_ymd_GIT_SHASUM(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
progversion = (int(match.group(2)) << 64) |\
|
progversion = (
|
||||||
(int(match.group(3)) << 48) |\
|
(int(match.group(2)) << 64)
|
||||||
(int(match.group(4)) << 32)
|
| (int(match.group(3)) << 48)
|
||||||
return (progname, progversion)
|
| (int(match.group(4)) << 32)
|
||||||
|
)
|
||||||
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
def parseVer_ymd(match, filepath):
|
def parseVer_ymd(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
progversion = (int(match.group(2)) << 64) |\
|
progversion = (
|
||||||
(int(match.group(3)) << 48) |\
|
(int(match.group(2)) << 64)
|
||||||
(int(match.group(4)) << 32)
|
| (int(match.group(3)) << 48)
|
||||||
return (progname, progversion)
|
| (int(match.group(4)) << 32)
|
||||||
|
)
|
||||||
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
def parseVer_GIT(match, filepath):
|
def parseVer_GIT(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
st = os.stat(filepath)
|
st = os.stat(filepath)
|
||||||
progversion = int(st.st_mtime) << 64
|
progversion = int(st.st_mtime) << 64
|
||||||
return (progname, progversion)
|
return (progname, progversion)
|
||||||
|
|
||||||
|
|
||||||
extensions = (
|
extensions = (
|
||||||
".tar.gz",
|
".tar.gz",
|
||||||
".tar.bz2",
|
".tar.bz2",
|
||||||
".tar.xz",
|
".tar.xz",
|
||||||
".orig.tar.gz",
|
".orig.tar.gz",
|
||||||
".orig.tar.bz2",
|
".orig.tar.bz2",
|
||||||
".orig.tar.xz",
|
".orig.tar.xz",
|
||||||
".zip",
|
".zip",
|
||||||
".tgz",
|
".tgz",
|
||||||
".tbz",
|
".tbz",
|
||||||
".txz",
|
".txz",
|
||||||
)
|
)
|
||||||
|
|
||||||
versionRegex = (
|
versionRegex = (
|
||||||
(re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
|
(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"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)-"),
|
||||||
(re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
|
parseVer_ymd_GIT_SHASUM,
|
||||||
(re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
|
), # xxx-YYYY-MM-DD-GIT_SHASUM
|
||||||
(re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
|
(re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
|
||||||
(re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
|
(re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
|
||||||
(re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
|
(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 = [
|
blacklist = [
|
||||||
("linux", re.compile(r"linux-\d.*")),
|
("linux", re.compile(r"linux-\d.*")),
|
||||||
("gcc", re.compile(r"gcc-.*")),
|
("gcc", re.compile(r"gcc-.*")),
|
||||||
("wl_apsta", re.compile(r"wl_apsta.*")),
|
("wl_apsta", re.compile(r"wl_apsta.*")),
|
||||||
(".fw", re.compile(r".*\.fw")),
|
(".fw", re.compile(r".*\.fw")),
|
||||||
(".arm", re.compile(r".*\.arm")),
|
(".arm", re.compile(r".*\.arm")),
|
||||||
(".bin", re.compile(r".*\.bin")),
|
(".bin", re.compile(r".*\.bin")),
|
||||||
("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
|
("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
|
||||||
]
|
]
|
||||||
|
|
||||||
class EntryParseError(Exception): pass
|
|
||||||
|
class EntryParseError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Entry:
|
class Entry:
|
||||||
def __init__(self, directory, filename):
|
def __init__(self, directory, filename):
|
||||||
self.directory = directory
|
self.directory = directory
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.progname = ""
|
self.progname = ""
|
||||||
self.fileext = ""
|
self.fileext = ""
|
||||||
|
|
||||||
for ext in extensions:
|
for ext in extensions:
|
||||||
if filename.endswith(ext):
|
if filename.endswith(ext):
|
||||||
filename = filename[0:0-len(ext)]
|
filename = filename[0 : 0 - len(ext)]
|
||||||
self.fileext = ext
|
self.fileext = ext
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print(self.filename, "has an unknown file-extension")
|
print(self.filename, "has an unknown file-extension")
|
||||||
raise EntryParseError("ext")
|
raise EntryParseError("ext")
|
||||||
for (regex, parseVersion) in versionRegex:
|
for (regex, parseVersion) in versionRegex:
|
||||||
match = regex.match(filename)
|
match = regex.match(filename)
|
||||||
if match:
|
if match:
|
||||||
(self.progname, self.version) = parseVersion(
|
(self.progname, self.version) = parseVersion(
|
||||||
match, directory + "/" + filename + self.fileext)
|
match, directory + "/" + filename + self.fileext
|
||||||
break
|
)
|
||||||
else:
|
break
|
||||||
print(self.filename, "has an unknown version pattern")
|
else:
|
||||||
raise EntryParseError("ver")
|
print(self.filename, "has an unknown version pattern")
|
||||||
|
raise EntryParseError("ver")
|
||||||
|
|
||||||
def getPath(self):
|
def getPath(self):
|
||||||
return (self.directory + "/" + self.filename).replace("//", "/")
|
return (self.directory + "/" + self.filename).replace("//", "/")
|
||||||
|
|
||||||
def deleteFile(self):
|
def deleteFile(self):
|
||||||
path = self.getPath()
|
path = self.getPath()
|
||||||
print("Deleting", path)
|
print("Deleting", path)
|
||||||
if not opt_dryrun:
|
if not opt_dryrun:
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
|
|
||||||
|
def __ge__(self, y):
|
||||||
|
return self.version >= y.version
|
||||||
|
|
||||||
def __ge__(self, y):
|
|
||||||
return self.version >= y.version
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print("OpenWrt download directory cleanup utility")
|
print("OpenWrt download directory cleanup utility")
|
||||||
print("Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>")
|
print("Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>")
|
||||||
print("")
|
print("")
|
||||||
print(" -d|--dry-run Do a dry-run. Don't delete any files")
|
print(" -d|--dry-run Do a dry-run. Don't delete any files")
|
||||||
print(" -B|--show-blacklist Show the blacklist and exit")
|
print(" -B|--show-blacklist Show the blacklist and exit")
|
||||||
print(" -w|--whitelist ITEM Remove ITEM from blacklist")
|
print(" -w|--whitelist ITEM Remove ITEM from blacklist")
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
global opt_dryrun
|
global opt_dryrun
|
||||||
|
|
||||||
try:
|
try:
|
||||||
(opts, args) = getopt.getopt(argv[1:],
|
(opts, args) = getopt.getopt(
|
||||||
"hdBw:",
|
argv[1:],
|
||||||
[ "help", "dry-run", "show-blacklist", "whitelist=", ])
|
"hdBw:",
|
||||||
if len(args) != 1:
|
[
|
||||||
usage()
|
"help",
|
||||||
return 1
|
"dry-run",
|
||||||
except getopt.GetoptError as e:
|
"show-blacklist",
|
||||||
usage()
|
"whitelist=",
|
||||||
return 1
|
],
|
||||||
directory = args[0]
|
)
|
||||||
|
if len(args) != 1:
|
||||||
|
usage()
|
||||||
|
return 1
|
||||||
|
except getopt.GetoptError as e:
|
||||||
|
usage()
|
||||||
|
return 1
|
||||||
|
directory = args[0]
|
||||||
|
|
||||||
if not os.path.exists(directory):
|
if not os.path.exists(directory):
|
||||||
print("Can't find dl path", directory)
|
print("Can't find dl path", directory)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
for (o, v) in opts:
|
for (o, v) in opts:
|
||||||
if o in ("-h", "--help"):
|
if o in ("-h", "--help"):
|
||||||
usage()
|
usage()
|
||||||
return 0
|
return 0
|
||||||
if o in ("-d", "--dry-run"):
|
if o in ("-d", "--dry-run"):
|
||||||
opt_dryrun = True
|
opt_dryrun = True
|
||||||
if o in ("-w", "--whitelist"):
|
if o in ("-w", "--whitelist"):
|
||||||
for i in range(0, len(blacklist)):
|
for i in range(0, len(blacklist)):
|
||||||
(name, regex) = blacklist[i]
|
(name, regex) = blacklist[i]
|
||||||
if name == v:
|
if name == v:
|
||||||
del blacklist[i]
|
del blacklist[i]
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("Whitelist error: Item", v,\
|
print("Whitelist error: Item", v, "is not in blacklist")
|
||||||
"is not in blacklist")
|
return 1
|
||||||
return 1
|
if o in ("-B", "--show-blacklist"):
|
||||||
if o in ("-B", "--show-blacklist"):
|
for (name, regex) in blacklist:
|
||||||
for (name, regex) in blacklist:
|
sep = "\t\t"
|
||||||
sep = "\t\t"
|
if len(name) >= 8:
|
||||||
if len(name) >= 8:
|
sep = "\t"
|
||||||
sep = "\t"
|
print("%s%s(%s)" % (name, sep, regex.pattern))
|
||||||
print("%s%s(%s)" % (name, sep, regex.pattern))
|
return 0
|
||||||
return 0
|
|
||||||
|
|
||||||
# Create a directory listing and parse the file names.
|
# Create a directory listing and parse the file names.
|
||||||
entries = []
|
entries = []
|
||||||
for filename in os.listdir(directory):
|
for filename in os.listdir(directory):
|
||||||
if filename == "." or filename == "..":
|
if filename == "." or filename == "..":
|
||||||
continue
|
continue
|
||||||
for (name, regex) in blacklist:
|
for (name, regex) in blacklist:
|
||||||
if regex.match(filename):
|
if regex.match(filename):
|
||||||
if opt_dryrun:
|
if opt_dryrun:
|
||||||
print(filename, "is blacklisted")
|
print(filename, "is blacklisted")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
entries.append(Entry(directory, filename))
|
entries.append(Entry(directory, filename))
|
||||||
except EntryParseError as e:
|
except EntryParseError as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Create a map of programs
|
# Create a map of programs
|
||||||
progmap = {}
|
progmap = {}
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry.progname in progmap.keys():
|
if entry.progname in progmap.keys():
|
||||||
progmap[entry.progname].append(entry)
|
progmap[entry.progname].append(entry)
|
||||||
else:
|
else:
|
||||||
progmap[entry.progname] = [entry,]
|
progmap[entry.progname] = [
|
||||||
|
entry,
|
||||||
|
]
|
||||||
|
|
||||||
# Traverse the program map and delete everything but the last version
|
# Traverse the program map and delete everything but the last version
|
||||||
for prog in progmap:
|
for prog in progmap:
|
||||||
lastVersion = None
|
lastVersion = None
|
||||||
versions = progmap[prog]
|
versions = progmap[prog]
|
||||||
for version in versions:
|
for version in versions:
|
||||||
if lastVersion is None or version >= lastVersion:
|
if lastVersion is None or version >= lastVersion:
|
||||||
lastVersion = version
|
lastVersion = version
|
||||||
if lastVersion:
|
if lastVersion:
|
||||||
for version in versions:
|
for version in versions:
|
||||||
if version is not lastVersion:
|
if version is not lastVersion:
|
||||||
version.deleteFile()
|
version.deleteFile()
|
||||||
if opt_dryrun:
|
if opt_dryrun:
|
||||||
print("Keeping", lastVersion.getPath())
|
print("Keeping", lastVersion.getPath())
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main(sys.argv))
|
sys.exit(main(sys.argv))
|
||||||
|
Loading…
Reference in New Issue
Block a user