7adc475d4da5d2812ba913843468f6b88402b2db
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>
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
)
28 def parseVer_123(match
):
29 progname
= match
.group(1)
31 patchlevel
= match
.group(5)
32 except (IndexError), e
:
35 patchlevel
= ord(patchlevel
[0])
38 progversion
= (int(match
.group(2)) << 64) |\
39 (int(match
.group(3)) << 48) |\
40 (int(match
.group(4)) << 32) |\
42 return (progname
, progversion
)
44 def parseVer_12(match
):
45 progname
= match
.group(1)
47 patchlevel
= match
.group(4)
48 except (IndexError), e
:
51 patchlevel
= ord(patchlevel
[0])
54 progversion
= (int(match
.group(2)) << 64) |\
55 (int(match
.group(3)) << 48) |\
57 return (progname
, progversion
)
59 def parseVer_r(match
):
60 progname
= match
.group(1)
61 progversion
= (int(match
.group(2)) << 64)
62 return (progname
, progversion
)
64 def parseVer_ymd(match
):
65 progname
= match
.group(1)
66 progversion
= (int(match
.group(2)) << 64) |\
67 (int(match
.group(3)) << 48) |\
68 (int(match
.group(4)) << 32)
69 return (progname
, progversion
)
82 (re
.compile(r
"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234
), # xxx-1.2.3.4
83 (re
.compile(r
"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd
), # xxx-YYYY-MM-DD
84 (re
.compile(r
"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123
), # xxx-1.2.3a
85 (re
.compile(r
"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123
), # xxx-1_2_3
86 (re
.compile(r
"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12
), # xxx-1.2a
87 (re
.compile(r
"(.+)[-_]r?(\d+)"), parseVer_r
), # xxx-r1111
91 ("linux", re
.compile(r
"linux-.*")),
92 ("gcc", re
.compile(r
"gcc-.*")),
93 ("wl_apsta", re
.compile(r
"wl_apsta.*")),
94 (".fw", re
.compile(r
".*\.fw")),
95 (".arm", re
.compile(r
".*\.arm")),
96 (".bin", re
.compile(r
".*\.bin")),
97 ("rt-firmware", re
.compile(r
"RT\d+_Firmware.*")),
100 class EntryParseError(Exception): pass
103 def __init__(self
, directory
, filename
):
104 self
.directory
= directory
105 self
.filename
= filename
108 for ext
in extensions
:
109 if filename
.endswith(ext
):
110 filename
= filename
[0:0-len(ext
)]
114 print "Extension did not match on", filename
115 raise EntryParseError("ext")
116 for (regex
, parseVersion
) in versionRegex
:
117 match
= regex
.match(filename
)
119 (self
.progname
, self
.version
) = parseVersion(match
)
123 print "Version regex did not match on", filename
124 raise EntryParseError("ver")
126 def deleteFile(self
):
127 path
= (self
.directory
+ "/" + self
.filename
).replace("//", "/")
128 print "Deleting", path
133 return self
.filename
== y
.filename
136 return self
.version
>= y
.version
139 print "OpenWRT download directory cleanup utility"
140 print "Usage: " + sys
.argv
[0] + " [OPTIONS] <path/to/dl>"
142 print " -d|--dry-run Do a dry-run. Don't delete any files"
143 print " -B|--show-blacklist Show the blacklist and exit"
144 print " -w|--whitelist ITEM Remove ITEM from blacklist"
150 (opts
, args
) = getopt
.getopt(argv
[1:],
152 [ "help", "dry-run", "show-blacklist", "whitelist=", ])
154 raise getopt
.GetoptError()
155 except getopt
.GetoptError
:
160 if o
in ("-h", "--help"):
163 if o
in ("-d", "--dry-run"):
165 if o
in ("-w", "--whitelist"):
166 for i
in range(0, len(blacklist
)):
167 (name
, regex
) = blacklist
[i
]
172 print "Whitelist error: Item", v
,\
173 "is not in blacklist"
175 if o
in ("-B", "--show-blacklist"):
176 for (name
, regex
) in blacklist
:
180 # Create a directory listing and parse the file names.
182 for filename
in os
.listdir(directory
):
183 if filename
== "." or filename
== "..":
185 for (name
, regex
) in blacklist
:
186 if regex
.match(filename
):
188 print filename
, "is blacklisted"
192 entries
.append(Entry(directory
, filename
))
193 except (EntryParseError
), e
: pass
195 # Create a map of programs
197 for entry
in entries
:
198 if entry
.progname
in progmap
.keys():
199 progmap
[entry
.progname
].append(entry
)
201 progmap
[entry
.progname
] = [entry
,]
203 # Traverse the program map and delete everything but the last version
206 versions
= progmap
[prog
]
207 for version
in versions
:
208 if lastVersion
is None or version
>= lastVersion
:
209 lastVersion
= version
211 for version
in versions
:
212 if version
!= lastVersion
:
215 print "Keeping", lastVersion
.filename
219 if __name__
== "__main__":
220 sys
.exit(main(sys
.argv
))
This page took 0.053209 seconds and 3 git commands to generate.