ipkg-build: use deterministic timestamps in ipk tarballs
[openwrt/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 # Copyright (C) 2013 OpenWrt.org
8 """
9
10 import sys
11 import os
12 import re
13 import getopt
14
15 # Commandline options
16 opt_dryrun = False
17
18
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)
26
27 def parseVer_123(match, filepath):
28 progname = match.group(1)
29 try:
30 patchlevel = match.group(5)
31 except (IndexError), e:
32 patchlevel = None
33 if patchlevel:
34 patchlevel = ord(patchlevel[0])
35 else:
36 patchlevel = 0
37 progversion = (int(match.group(2)) << 64) |\
38 (int(match.group(3)) << 48) |\
39 (int(match.group(4)) << 32) |\
40 patchlevel
41 return (progname, progversion)
42
43 def parseVer_12(match, filepath):
44 progname = match.group(1)
45 try:
46 patchlevel = match.group(4)
47 except (IndexError), e:
48 patchlevel = None
49 if patchlevel:
50 patchlevel = ord(patchlevel[0])
51 else:
52 patchlevel = 0
53 progversion = (int(match.group(2)) << 64) |\
54 (int(match.group(3)) << 48) |\
55 patchlevel
56 return (progname, progversion)
57
58 def parseVer_r(match, filepath):
59 progname = match.group(1)
60 progversion = (int(match.group(2)) << 64)
61 return (progname, progversion)
62
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)
69
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)
75
76 extensions = (
77 ".tar.gz",
78 ".tar.bz2",
79 ".tar.xz",
80 ".orig.tar.gz",
81 ".orig.tar.bz2",
82 ".orig.tar.xz",
83 ".zip",
84 ".tgz",
85 ".tbz",
86 ".txz",
87 )
88
89 versionRegex = (
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
97 )
98
99 blacklist = [
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.*")),
107 ]
108
109 class EntryParseError(Exception): pass
110
111 class Entry:
112 def __init__(self, directory, filename):
113 self.directory = directory
114 self.filename = filename
115 self.progname = ""
116 self.fileext = ""
117
118 for ext in extensions:
119 if filename.endswith(ext):
120 filename = filename[0:0-len(ext)]
121 self.fileext = ext
122 break
123 else:
124 print self.filename, "has an unknown file-extension"
125 raise EntryParseError("ext")
126 for (regex, parseVersion) in versionRegex:
127 match = regex.match(filename)
128 if match:
129 (self.progname, self.version) = parseVersion(
130 match, directory + "/" + filename + self.fileext)
131 break
132 else:
133 print self.filename, "has an unknown version pattern"
134 raise EntryParseError("ver")
135
136 def deleteFile(self):
137 path = (self.directory + "/" + self.filename).replace("//", "/")
138 print "Deleting", path
139 if not opt_dryrun:
140 os.unlink(path)
141
142 def __eq__(self, y):
143 return self.filename == y.filename
144
145 def __ge__(self, y):
146 return self.version >= y.version
147
148 def usage():
149 print "OpenWrt download directory cleanup utility"
150 print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
151 print ""
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"
155
156 def main(argv):
157 global opt_dryrun
158
159 try:
160 (opts, args) = getopt.getopt(argv[1:],
161 "hdBw:",
162 [ "help", "dry-run", "show-blacklist", "whitelist=", ])
163 if len(args) != 1:
164 usage()
165 return 1
166 except getopt.GetoptError:
167 usage()
168 return 1
169 directory = args[0]
170 for (o, v) in opts:
171 if o in ("-h", "--help"):
172 usage()
173 return 0
174 if o in ("-d", "--dry-run"):
175 opt_dryrun = True
176 if o in ("-w", "--whitelist"):
177 for i in range(0, len(blacklist)):
178 (name, regex) = blacklist[i]
179 if name == v:
180 del blacklist[i]
181 break
182 else:
183 print "Whitelist error: Item", v,\
184 "is not in blacklist"
185 return 1
186 if o in ("-B", "--show-blacklist"):
187 for (name, regex) in blacklist:
188 print name
189 return 0
190
191 # Create a directory listing and parse the file names.
192 entries = []
193 for filename in os.listdir(directory):
194 if filename == "." or filename == "..":
195 continue
196 for (name, regex) in blacklist:
197 if regex.match(filename):
198 if opt_dryrun:
199 print filename, "is blacklisted"
200 break
201 else:
202 try:
203 entries.append(Entry(directory, filename))
204 except (EntryParseError), e: pass
205
206 # Create a map of programs
207 progmap = {}
208 for entry in entries:
209 if entry.progname in progmap.keys():
210 progmap[entry.progname].append(entry)
211 else:
212 progmap[entry.progname] = [entry,]
213
214 # Traverse the program map and delete everything but the last version
215 for prog in progmap:
216 lastVersion = None
217 versions = progmap[prog]
218 for version in versions:
219 if lastVersion is None or version >= lastVersion:
220 lastVersion = version
221 if lastVersion:
222 for version in versions:
223 if version != lastVersion:
224 version.deleteFile()
225 if opt_dryrun:
226 print "Keeping", lastVersion.filename
227
228 return 0
229
230 if __name__ == "__main__":
231 sys.exit(main(sys.argv))