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