3 # OpenWRT download directory cleanup utility.
4 # Delete all but the very last version of the program tarballs.
6 # Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
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
)
26 def parseVer_123(match
):
27 progname
= match
.group(1)
29 patchlevel
= match
.group(5)
30 except (IndexError), e
:
33 patchlevel
= ord(patchlevel
[0])
36 progversion
= (int(match
.group(2)) << 64) |\
37 (int(match
.group(3)) << 48) |\
38 (int(match
.group(4)) << 32) |\
40 return (progname
, progversion
)
42 def parseVer_12(match
):
43 progname
= match
.group(1)
45 patchlevel
= match
.group(4)
46 except (IndexError), e
:
49 patchlevel
= ord(patchlevel
[0])
52 progversion
= (int(match
.group(2)) << 64) |\
53 (int(match
.group(3)) << 48) |\
55 return (progname
, progversion
)
57 def parseVer_r(match
):
58 progname
= match
.group(1)
59 progversion
= (int(match
.group(2)) << 64)
60 return (progname
, progversion
)
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
)
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
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.*")),
98 class EntryParseError(Exception): pass
101 def __init__(self
, directory
, filename
):
102 self
.directory
= directory
103 self
.filename
= filename
106 for ext
in extensions
:
107 if filename
.endswith(ext
):
108 filename
= filename
[0:0-len(ext
)]
111 print self
.filename
, "has an unknown file-extension"
112 raise EntryParseError("ext")
113 for (regex
, parseVersion
) in versionRegex
:
114 match
= regex
.match(filename
)
116 (self
.progname
, self
.version
) = parseVersion(match
)
119 print self
.filename
, "has an unknown version pattern"
120 raise EntryParseError("ver")
122 def deleteFile(self
):
123 path
= (self
.directory
+ "/" + self
.filename
).replace("//", "/")
124 print "Deleting", path
129 return self
.filename
== y
.filename
132 return self
.version
>= y
.version
135 print "OpenWRT download directory cleanup utility"
136 print "Usage: " + sys
.argv
[0] + " [OPTIONS] <path/to/dl>"
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"
146 (opts
, args
) = getopt
.getopt(argv
[1:],
148 [ "help", "dry-run", "show-blacklist", "whitelist=", ])
150 raise getopt
.GetoptError()
151 except getopt
.GetoptError
:
156 if o
in ("-h", "--help"):
159 if o
in ("-d", "--dry-run"):
161 if o
in ("-w", "--whitelist"):
162 for i
in range(0, len(blacklist
)):
163 (name
, regex
) = blacklist
[i
]
168 print "Whitelist error: Item", v
,\
169 "is not in blacklist"
171 if o
in ("-B", "--show-blacklist"):
172 for (name
, regex
) in blacklist
:
176 # Create a directory listing and parse the file names.
178 for filename
in os
.listdir(directory
):
179 if filename
== "." or filename
== "..":
181 for (name
, regex
) in blacklist
:
182 if regex
.match(filename
):
184 print filename
, "is blacklisted"
188 entries
.append(Entry(directory
, filename
))
189 except (EntryParseError
), e
: pass
191 # Create a map of programs
193 for entry
in entries
:
194 if entry
.progname
in progmap
.keys():
195 progmap
[entry
.progname
].append(entry
)
197 progmap
[entry
.progname
] = [entry
,]
199 # Traverse the program map and delete everything but the last version
202 versions
= progmap
[prog
]
203 for version
in versions
:
204 if lastVersion
is None or version
>= lastVersion
:
205 lastVersion
= version
207 for version
in versions
:
208 if version
!= lastVersion
:
211 print "Keeping", lastVersion
.filename
215 if __name__
== "__main__":
216 sys
.exit(main(sys
.argv
))
This page took 0.065271 seconds and 5 git commands to generate.