ar71xx: fix the wan port on pb92
[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 raise getopt.GetoptError()
151 except getopt.GetoptError:
152 usage()
153 return 1
154 directory = args[0]
155 for (o, v) in opts:
156 if o in ("-h", "--help"):
157 usage()
158 return 0
159 if o in ("-d", "--dry-run"):
160 opt_dryrun = True
161 if o in ("-w", "--whitelist"):
162 for i in range(0, len(blacklist)):
163 (name, regex) = blacklist[i]
164 if name == v:
165 del blacklist[i]
166 break
167 else:
168 print "Whitelist error: Item", v,\
169 "is not in blacklist"
170 return 1
171 if o in ("-B", "--show-blacklist"):
172 for (name, regex) in blacklist:
173 print name
174 return 0
175
176 # Create a directory listing and parse the file names.
177 entries = []
178 for filename in os.listdir(directory):
179 if filename == "." or filename == "..":
180 continue
181 for (name, regex) in blacklist:
182 if regex.match(filename):
183 if opt_dryrun:
184 print filename, "is blacklisted"
185 break
186 else:
187 try:
188 entries.append(Entry(directory, filename))
189 except (EntryParseError), e: pass
190
191 # Create a map of programs
192 progmap = {}
193 for entry in entries:
194 if entry.progname in progmap.keys():
195 progmap[entry.progname].append(entry)
196 else:
197 progmap[entry.progname] = [entry,]
198
199 # Traverse the program map and delete everything but the last version
200 for prog in progmap:
201 lastVersion = None
202 versions = progmap[prog]
203 for version in versions:
204 if lastVersion is None or version >= lastVersion:
205 lastVersion = version
206 if lastVersion:
207 for version in versions:
208 if version != lastVersion:
209 version.deleteFile()
210 if opt_dryrun:
211 print "Keeping", lastVersion.filename
212
213 return 0
214
215 if __name__ == "__main__":
216 sys.exit(main(sys.argv))
This page took 0.054864 seconds and 5 git commands to generate.