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:
Ansuel Smith 2021-07-06 03:20:40 +02:00 committed by Paul Spooren
parent 4eb4c3c469
commit ff875876da

View File

@ -20,12 +20,15 @@ opt_dryrun = False
def parseVer_1234(match, filepath):
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)
progversion = (
(int(match.group(2)) << 64)
| (int(match.group(3)) << 48)
| (int(match.group(4)) << 32)
| (int(match.group(5)) << 16)
)
return (progname, progversion)
def parseVer_123(match, filepath):
progname = match.group(1)
try:
@ -36,12 +39,15 @@ def parseVer_123(match, filepath):
patchlevel = ord(patchlevel[0])
else:
patchlevel = 0
progversion = (int(match.group(2)) << 64) |\
(int(match.group(3)) << 48) |\
(int(match.group(4)) << 32) |\
patchlevel
progversion = (
(int(match.group(2)) << 64)
| (int(match.group(3)) << 48)
| (int(match.group(4)) << 32)
| patchlevel
)
return (progname, progversion)
def parseVer_12(match, filepath):
progname = match.group(1)
try:
@ -52,36 +58,43 @@ def parseVer_12(match, filepath):
patchlevel = ord(patchlevel[0])
else:
patchlevel = 0
progversion = (int(match.group(2)) << 64) |\
(int(match.group(3)) << 48) |\
patchlevel
progversion = (int(match.group(2)) << 64) | (int(match.group(3)) << 48) | patchlevel
return (progname, progversion)
def parseVer_r(match, filepath):
progname = match.group(1)
progversion = (int(match.group(2)) << 64)
progversion = int(match.group(2)) << 64
return (progname, progversion)
def parseVer_ymd_GIT_SHASUM(match, filepath):
progname = match.group(1)
progversion = (int(match.group(2)) << 64) |\
(int(match.group(3)) << 48) |\
(int(match.group(4)) << 32)
progversion = (
(int(match.group(2)) << 64)
| (int(match.group(3)) << 48)
| (int(match.group(4)) << 32)
)
return (progname, progversion)
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)
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",
@ -97,7 +110,10 @@ extensions = (
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_GIT_SHASUM), # xxx-YYYY-MM-DD-GIT_SHASUM
(
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
@ -116,7 +132,10 @@ blacklist = [
("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
]
class EntryParseError(Exception): pass
class EntryParseError(Exception):
pass
class Entry:
def __init__(self, directory, filename):
@ -137,7 +156,8 @@ class Entry:
match = regex.match(filename)
if match:
(self.progname, self.version) = parseVersion(
match, directory + "/" + filename + self.fileext)
match, directory + "/" + filename + self.fileext
)
break
else:
print(self.filename, "has an unknown version pattern")
@ -155,6 +175,7 @@ class Entry:
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>")
@ -163,13 +184,21 @@ def usage():
print(" -B|--show-blacklist Show the blacklist and exit")
print(" -w|--whitelist ITEM Remove ITEM from blacklist")
def main(argv):
global opt_dryrun
try:
(opts, args) = getopt.getopt(argv[1:],
(opts, args) = getopt.getopt(
argv[1:],
"hdBw:",
[ "help", "dry-run", "show-blacklist", "whitelist=", ])
[
"help",
"dry-run",
"show-blacklist",
"whitelist=",
],
)
if len(args) != 1:
usage()
return 1
@ -195,8 +224,7 @@ 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:
@ -228,7 +256,9 @@ def main(argv):
if entry.progname in progmap.keys():
progmap[entry.progname].append(entry)
else:
progmap[entry.progname] = [entry,]
progmap[entry.progname] = [
entry,
]
# Traverse the program map and delete everything but the last version
for prog in progmap:
@ -246,5 +276,6 @@ def main(argv):
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))