[backfire] uhttpd: merge r28792
[openwrt-10.03/.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, filepath):
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, filepath):
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, filepath):
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, filepath):
58         progname = match.group(1)
59         progversion = (int(match.group(2)) << 64)
60         return (progname, progversion)
61
62 def parseVer_ymd(match, filepath):
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 def parseVer_GIT(match, filepath):
70         progname = match.group(1)
71         st = os.stat(filepath)
72         progversion = int(st.st_mtime) << 64
73         return (progname, progversion)
74
75 extensions = (
76         ".tar.gz",
77         ".tar.bz2",
78         ".orig.tar.gz",
79         ".orig.tar.bz2",
80         ".zip",
81         ".tgz",
82         ".tbz",
83 )
84
85 versionRegex = (
86         (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT),            # xxx-GIT_SHASUM
87         (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234),     # xxx-1.2.3.4
88         (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd),      # xxx-YYYY-MM-DD
89         (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123),        # xxx-1.2.3a
90         (re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123),               # xxx-1_2_3
91         (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12),                # xxx-1.2a
92         (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r),                           # xxx-r1111
93 )
94
95 blacklist = [
96         ("linux",               re.compile(r"linux-.*")),
97         ("gcc",                 re.compile(r"gcc-.*")),
98         ("wl_apsta",            re.compile(r"wl_apsta.*")),
99         (".fw",                 re.compile(r".*\.fw")),
100         (".arm",                re.compile(r".*\.arm")),
101         (".bin",                re.compile(r".*\.bin")),
102         ("rt-firmware",         re.compile(r"RT[\d\w]+_Firmware.*")),
103 ]
104
105 class EntryParseError(Exception): pass
106
107 class Entry:
108         def __init__(self, directory, filename):
109                 self.directory = directory
110                 self.filename = filename
111                 self.progname = ""
112                 self.fileext = ""
113
114                 for ext in extensions:
115                         if filename.endswith(ext):
116                                 filename = filename[0:0-len(ext)]
117                                 self.fileext = ext
118                                 break
119                 else:
120                         print self.filename, "has an unknown file-extension"
121                         raise EntryParseError("ext")
122                 for (regex, parseVersion) in versionRegex:
123                         match = regex.match(filename)
124                         if match:
125                                 (self.progname, self.version) = parseVersion(
126                                         match, directory + "/" + filename + self.fileext)
127                                 break
128                 else:
129                         print self.filename, "has an unknown version pattern"
130                         raise EntryParseError("ver")
131
132         def deleteFile(self):
133                 path = (self.directory + "/" + self.filename).replace("//", "/")
134                 print "Deleting", path
135                 if not opt_dryrun:
136                         os.unlink(path)
137
138         def __eq__(self, y):
139                 return self.filename == y.filename
140
141         def __ge__(self, y):
142                 return self.version >= y.version
143
144 def usage():
145         print "OpenWRT download directory cleanup utility"
146         print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
147         print ""
148         print " -d|--dry-run            Do a dry-run. Don't delete any files"
149         print " -B|--show-blacklist     Show the blacklist and exit"
150         print " -w|--whitelist ITEM     Remove ITEM from blacklist"
151
152 def main(argv):
153         global opt_dryrun
154
155         try:
156                 (opts, args) = getopt.getopt(argv[1:],
157                         "hdBw:",
158                         [ "help", "dry-run", "show-blacklist", "whitelist=", ])
159                 if len(args) != 1:
160                         raise getopt.GetoptError()
161         except getopt.GetoptError:
162                 usage()
163                 return 1
164         directory = args[0]
165         for (o, v) in opts:
166                 if o in ("-h", "--help"):
167                         usage()
168                         return 0
169                 if o in ("-d", "--dry-run"):
170                         opt_dryrun = True
171                 if o in ("-w", "--whitelist"):
172                         for i in range(0, len(blacklist)):
173                                 (name, regex) = blacklist[i]
174                                 if name == v:
175                                         del blacklist[i]
176                                         break
177                         else:
178                                 print "Whitelist error: Item", v,\
179                                       "is not in blacklist"
180                                 return 1
181                 if o in ("-B", "--show-blacklist"):
182                         for (name, regex) in blacklist:
183                                 print name
184                         return 0
185
186         # Create a directory listing and parse the file names.
187         entries = []
188         for filename in os.listdir(directory):
189                 if filename == "." or filename == "..":
190                         continue
191                 for (name, regex) in blacklist:
192                         if regex.match(filename):
193                                 if opt_dryrun:
194                                         print filename, "is blacklisted"
195                                 break
196                 else:
197                         try:
198                                 entries.append(Entry(directory, filename))
199                         except (EntryParseError), e: pass
200
201         # Create a map of programs
202         progmap = {}
203         for entry in entries:
204                 if entry.progname in progmap.keys():
205                         progmap[entry.progname].append(entry)
206                 else:
207                         progmap[entry.progname] = [entry,]
208
209         # Traverse the program map and delete everything but the last version
210         for prog in progmap:
211                 lastVersion = None
212                 versions = progmap[prog]
213                 for version in versions:
214                         if lastVersion is None or version >= lastVersion:
215                                 lastVersion = version
216                 if lastVersion:
217                         for version in versions:
218                                 if version != lastVersion:
219                                         version.deleteFile()
220                         if opt_dryrun:
221                                 print "Keeping", lastVersion.filename
222
223         return 0
224
225 if __name__ == "__main__":
226         sys.exit(main(sys.argv))