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