41f172de5641bfe5181d45f2868143984d428489
[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 patchlevel = match.group(5)
31 if patchlevel:
32 patchlevel = ord(patchlevel[0])
33 else:
34 patchlevel = 0
35 progversion = (int(match.group(2)) << 64) |\
36 (int(match.group(3)) << 48) |\
37 (int(match.group(4)) << 32) |\
38 patchlevel
39 return (progname, progversion)
40
41 def parseVer_12(match):
42 progname = match.group(1)
43 patchlevel = match.group(4)
44 if patchlevel:
45 patchlevel = ord(patchlevel[0])
46 else:
47 patchlevel = 0
48 progversion = (int(match.group(2)) << 64) |\
49 (int(match.group(3)) << 48) |\
50 patchlevel
51 return (progname, progversion)
52
53 def parseVer_r(match):
54 progname = match.group(1)
55 progversion = (int(match.group(2)) << 64)
56 return (progname, progversion)
57
58 def parseVer_ymd(match):
59 progname = match.group(1)
60 progversion = (int(match.group(2)) << 64) |\
61 (int(match.group(3)) << 48) |\
62 (int(match.group(4)) << 32)
63 return (progname, progversion)
64
65 extensions = (
66 ".tar.gz",
67 ".tar.bz2",
68 ".orig.tar.gz",
69 ".orig.tar.bz2",
70 ".zip",
71 ".tgz",
72 ".tbz",
73 )
74
75 versionRegex = (
76 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
77 (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
78 (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
79 (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
80 (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
81 )
82
83 blacklist = (
84 re.compile(r"wl_apsta.*"),
85 re.compile(r"boost.*"),
86 re.compile(r".*\.fw"),
87 re.compile(r".*\.arm"),
88 re.compile(r".*\.bin"),
89 re.compile(r"RT\d+_Firmware.*"),
90 )
91
92 class EntryParseError(Exception): pass
93
94 class Entry:
95 def __init__(self, directory, filename):
96 self.directory = directory
97 self.filename = filename
98 self.progname = ""
99
100 for ext in extensions:
101 if filename.endswith(ext):
102 filename = filename[0:0-len(ext)]
103 break
104 else:
105 if DEBUG:
106 print "Extension did not match on", filename
107 raise EntryParseError("ext")
108 for (regex, parseVersion) in versionRegex:
109 match = regex.match(filename)
110 if match:
111 (self.progname, self.version) = parseVersion(match)
112 break
113 else:
114 if DEBUG:
115 print "Version regex did not match on", filename
116 raise EntryParseError("ver")
117
118 def deleteFile(self):
119 path = (self.directory + "/" + self.filename).replace("//", "/")
120 print "Deleting", path
121 if not opt_dryrun:
122 os.unlink(path)
123
124 def __eq__(self, y):
125 return self.filename == y.filename
126
127 def __ge__(self, y):
128 return self.version >= y.version
129
130 def usage():
131 print "OpenWRT download directory cleanup utility"
132 print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
133 print ""
134 print " -d|--dry-run Do a dry-run. Don't delete any files"
135
136 def main(argv):
137 global opt_dryrun
138
139 try:
140 (opts, args) = getopt.getopt(argv[1:],
141 "hd",
142 [ "help", "dry-run", ])
143 if len(args) != 1:
144 raise getopt.GetoptError()
145 except getopt.GetoptError:
146 usage()
147 return 1
148 directory = args[0]
149 for (o, v) in opts:
150 if o in ("-h", "--help"):
151 usage()
152 return 0
153 if o in ("-d", "--dry-run"):
154 opt_dryrun = True
155
156 # Create a directory listing and parse the file names.
157 entries = []
158 for filename in os.listdir(directory):
159 if filename == "." or filename == "..":
160 continue
161 for black in blacklist:
162 if black.match(filename):
163 if opt_dryrun:
164 print filename, "is blacklisted"
165 break
166 else:
167 try:
168 entries.append(Entry(directory, filename))
169 except (EntryParseError), e: pass
170
171 # Create a map of programs
172 progmap = {}
173 for entry in entries:
174 if entry.progname in progmap.keys():
175 progmap[entry.progname].append(entry)
176 else:
177 progmap[entry.progname] = [entry,]
178
179 # Traverse the program map and delete everything but the last version
180 for prog in progmap:
181 lastVersion = None
182 versions = progmap[prog]
183 for version in versions:
184 if lastVersion is None or version >= lastVersion:
185 lastVersion = version
186 if lastVersion:
187 for version in versions:
188 if version != lastVersion:
189 version.deleteFile()
190 if opt_dryrun:
191 print "Keeping", lastVersion.filename
192
193 return 0
194
195 if __name__ == "__main__":
196 sys.exit(main(sys.argv))