3 # OpenWRT download directory cleanup utility.
4 # Delete all but the very last version of the program tarballs.
6 # Copyright (C) 2010 Michael Buesch <mb@bu3sch.de>
7 # Copyright (C) 2013 OpenWrt.org
19 def parseVer_1234(match, filepath):
20 progname = match.group(1)
21 progversion = (int(match.group(2)) << 64) |\
22 (int(match.group(3)) << 48) |\
23 (int(match.group(4)) << 32) |\
24 (int(match.group(5)) << 16)
25 return (progname, progversion)
27 def parseVer_123(match, filepath):
28 progname = match.group(1)
30 patchlevel = match.group(5)
31 except (IndexError), e:
34 patchlevel = ord(patchlevel[0])
37 progversion = (int(match.group(2)) << 64) |\
38 (int(match.group(3)) << 48) |\
39 (int(match.group(4)) << 32) |\
41 return (progname, progversion)
43 def parseVer_12(match, filepath):
44 progname = match.group(1)
46 patchlevel = match.group(4)
47 except (IndexError), e:
50 patchlevel = ord(patchlevel[0])
53 progversion = (int(match.group(2)) << 64) |\
54 (int(match.group(3)) << 48) |\
56 return (progname, progversion)
58 def parseVer_r(match, filepath):
59 progname = match.group(1)
60 progversion = (int(match.group(2)) << 64)
61 return (progname, progversion)
63 def parseVer_ymd(match, filepath):
64 progname = match.group(1)
65 progversion = (int(match.group(2)) << 64) |\
66 (int(match.group(3)) << 48) |\
67 (int(match.group(4)) << 32)
68 return (progname, progversion)
70 def parseVer_GIT(match, filepath):
71 progname = match.group(1)
72 st = os.stat(filepath)
73 progversion = int(st.st_mtime) << 64
74 return (progname, progversion)
90 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
91 (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
92 (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
93 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
94 (re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
95 (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
96 (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
100 ("linux", re.compile(r"linux-.*")),
101 ("gcc", re.compile(r"gcc-.*")),
102 ("wl_apsta", re.compile(r"wl_apsta.*")),
103 (".fw", re.compile(r".*\.fw")),
104 (".arm", re.compile(r".*\.arm")),
105 (".bin", re.compile(r".*\.bin")),
106 ("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
109 class EntryParseError(Exception): pass
112 def __init__(self, directory, filename):
113 self.directory = directory
114 self.filename = filename
118 for ext in extensions:
119 if filename.endswith(ext):
120 filename = filename[0:0-len(ext)]
124 print self.filename, "has an unknown file-extension"
125 raise EntryParseError("ext")
126 for (regex, parseVersion) in versionRegex:
127 match = regex.match(filename)
129 (self.progname, self.version) = parseVersion(
130 match, directory + "/" + filename + self.fileext)
133 print self.filename, "has an unknown version pattern"
134 raise EntryParseError("ver")
136 def deleteFile(self):
137 path = (self.directory + "/" + self.filename).replace("//", "/")
138 print "Deleting", path
143 return self.filename == y.filename
146 return self.version >= y.version
149 print "OpenWRT download directory cleanup utility"
150 print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
152 print " -d|--dry-run Do a dry-run. Don't delete any files"
153 print " -B|--show-blacklist Show the blacklist and exit"
154 print " -w|--whitelist ITEM Remove ITEM from blacklist"
160 (opts, args) = getopt.getopt(argv[1:],
162 [ "help", "dry-run", "show-blacklist", "whitelist=", ])
166 except getopt.GetoptError:
171 if o in ("-h", "--help"):
174 if o in ("-d", "--dry-run"):
176 if o in ("-w", "--whitelist"):
177 for i in range(0, len(blacklist)):
178 (name, regex) = blacklist[i]
183 print "Whitelist error: Item", v,\
184 "is not in blacklist"
186 if o in ("-B", "--show-blacklist"):
187 for (name, regex) in blacklist:
191 # Create a directory listing and parse the file names.
193 for filename in os.listdir(directory):
194 if filename == "." or filename == "..":
196 for (name, regex) in blacklist:
197 if regex.match(filename):
199 print filename, "is blacklisted"
203 entries.append(Entry(directory, filename))
204 except (EntryParseError), e: pass
206 # Create a map of programs
208 for entry in entries:
209 if entry.progname in progmap.keys():
210 progmap[entry.progname].append(entry)
212 progmap[entry.progname] = [entry,]
214 # Traverse the program map and delete everything but the last version
217 versions = progmap[prog]
218 for version in versions:
219 if lastVersion is None or version >= lastVersion:
220 lastVersion = version
222 for version in versions:
223 if version != lastVersion:
226 print "Keeping", lastVersion.filename
230 if __name__ == "__main__":
231 sys.exit(main(sys.argv))