dl_cleanup: Add regex for foo_1_2_3
[openwrt/svn-archive/archive.git] / scripts / dl_cleanup.py
1 #!/usr/bin/env python
2 """
3 # OpenWRT download directory cleanup utility.
4 # Delete all but the very last version of the program tarballs.
5 #
6 # Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
7 """
8
9 import sys
10 import os
11 import re
12 import getopt
13
14 DEBUG = 0
15
16 # Commandline options
17 opt_dryrun = False
18
19
20 def parseVer_1234(match):
21 progname = match.group(1)
22 progversion = (int(match.group(2)) << 64) |\
23 (int(match.group(3)) << 48) |\
24 (int(match.group(4)) << 32) |\
25 (int(match.group(5)) << 16)
26 return (progname, progversion)
27
28 def parseVer_123(match):
29 progname = match.group(1)
30 try:
31 patchlevel = match.group(5)
32 except (IndexError), e:
33 patchlevel = None
34 if patchlevel:
35 patchlevel = ord(patchlevel[0])
36 else:
37 patchlevel = 0
38 progversion = (int(match.group(2)) << 64) |\
39 (int(match.group(3)) << 48) |\
40 (int(match.group(4)) << 32) |\
41 patchlevel
42 return (progname, progversion)
43
44 def parseVer_12(match):
45 progname = match.group(1)
46 try:
47 patchlevel = match.group(4)
48 except (IndexError), e:
49 patchlevel = None
50 if patchlevel:
51 patchlevel = ord(patchlevel[0])
52 else:
53 patchlevel = 0
54 progversion = (int(match.group(2)) << 64) |\
55 (int(match.group(3)) << 48) |\
56 patchlevel
57 return (progname, progversion)
58
59 def parseVer_r(match):
60 progname = match.group(1)
61 progversion = (int(match.group(2)) << 64)
62 return (progname, progversion)
63
64 def parseVer_ymd(match):
65 progname = match.group(1)
66 progversion = (int(match.group(2)) << 64) |\
67 (int(match.group(3)) << 48) |\
68 (int(match.group(4)) << 32)
69 return (progname, progversion)
70
71 extensions = (
72 ".tar.gz",
73 ".tar.bz2",
74 ".orig.tar.gz",
75 ".orig.tar.bz2",
76 ".zip",
77 ".tgz",
78 ".tbz",
79 )
80
81 versionRegex = (
82 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
83 (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
84 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
85 (re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
86 (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
87 (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
88 )
89
90 blacklist = [
91 ("linux", re.compile(r"linux-.*")),
92 ("gcc", re.compile(r"gcc-.*")),
93 ("wl_apsta", re.compile(r"wl_apsta.*")),
94 (".fw", re.compile(r".*\.fw")),
95 (".arm", re.compile(r".*\.arm")),
96 (".bin", re.compile(r".*\.bin")),
97 ("rt-firmware", re.compile(r"RT\d+_Firmware.*")),
98 ]
99
100 class EntryParseError(Exception): pass
101
102 class Entry:
103 def __init__(self, directory, filename):
104 self.directory = directory
105 self.filename = filename
106 self.progname = ""
107
108 for ext in extensions:
109 if filename.endswith(ext):
110 filename = filename[0:0-len(ext)]
111 break
112 else:
113 if DEBUG:
114 print "Extension did not match on", filename
115 raise EntryParseError("ext")
116 for (regex, parseVersion) in versionRegex:
117 match = regex.match(filename)
118 if match:
119 (self.progname, self.version) = parseVersion(match)
120 break
121 else:
122 if DEBUG:
123 print "Version regex did not match on", filename
124 raise EntryParseError("ver")
125
126 def deleteFile(self):
127 path = (self.directory + "/" + self.filename).replace("//", "/")
128 print "Deleting", path
129 if not opt_dryrun:
130 os.unlink(path)
131
132 def __eq__(self, y):
133 return self.filename == y.filename
134
135 def __ge__(self, y):
136 return self.version >= y.version
137
138 def usage():
139 print "OpenWRT download directory cleanup utility"
140 print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
141 print ""
142 print " -d|--dry-run Do a dry-run. Don't delete any files"
143 print " -B|--show-blacklist Show the blacklist and exit"
144 print " -w|--whitelist ITEM Remove ITEM from blacklist"
145
146 def main(argv):
147 global opt_dryrun
148
149 try:
150 (opts, args) = getopt.getopt(argv[1:],
151 "hdBw:",
152 [ "help", "dry-run", "show-blacklist", "whitelist=", ])
153 if len(args) != 1:
154 raise getopt.GetoptError()
155 except getopt.GetoptError:
156 usage()
157 return 1
158 directory = args[0]
159 for (o, v) in opts:
160 if o in ("-h", "--help"):
161 usage()
162 return 0
163 if o in ("-d", "--dry-run"):
164 opt_dryrun = True
165 if o in ("-w", "--whitelist"):
166 for i in range(0, len(blacklist)):
167 (name, regex) = blacklist[i]
168 if name == v:
169 del blacklist[i]
170 break
171 else:
172 print "Whitelist error: Item", v,\
173 "is not in blacklist"
174 return 1
175 if o in ("-B", "--show-blacklist"):
176 for (name, regex) in blacklist:
177 print name
178 return 0
179
180 # Create a directory listing and parse the file names.
181 entries = []
182 for filename in os.listdir(directory):
183 if filename == "." or filename == "..":
184 continue
185 for (name, regex) in blacklist:
186 if regex.match(filename):
187 if opt_dryrun:
188 print filename, "is blacklisted"
189 break
190 else:
191 try:
192 entries.append(Entry(directory, filename))
193 except (EntryParseError), e: pass
194
195 # Create a map of programs
196 progmap = {}
197 for entry in entries:
198 if entry.progname in progmap.keys():
199 progmap[entry.progname].append(entry)
200 else:
201 progmap[entry.progname] = [entry,]
202
203 # Traverse the program map and delete everything but the last version
204 for prog in progmap:
205 lastVersion = None
206 versions = progmap[prog]
207 for version in versions:
208 if lastVersion is None or version >= lastVersion:
209 lastVersion = version
210 if lastVersion:
211 for version in versions:
212 if version != lastVersion:
213 version.deleteFile()
214 if opt_dryrun:
215 print "Keeping", lastVersion.filename
216
217 return 0
218
219 if __name__ == "__main__":
220 sys.exit(main(sys.argv))