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>
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
)
24 def parseVer_123(match
):
25 progname
= match
.group(1)
26 patchlevel
= match
.group(5)
28 patchlevel
= ord(patchlevel
[0])
31 progversion
= (int(match
.group(2)) << 64) |\
32 (int(match
.group(3)) << 48) |\
33 (int(match
.group(4)) << 32) |\
35 return (progname
, progversion
)
37 def parseVer_12(match
):
38 progname
= match
.group(1)
39 patchlevel
= match
.group(4)
41 patchlevel
= ord(patchlevel
[0])
44 progversion
= (int(match
.group(2)) << 64) |\
45 (int(match
.group(3)) << 48) |\
47 return (progname
, progversion
)
49 def parseVer_r(match
):
50 progname
= match
.group(1)
51 progversion
= (int(match
.group(2)) << 64)
52 return (progname
, progversion
)
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
)
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
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.*"),
88 class EntryParseError(Exception): pass
91 def __init__(self
, directory
, filename
):
92 self
.directory
= directory
93 self
.filename
= filename
96 for ext
in extensions
:
97 if filename
.endswith(ext
):
98 filename
= filename
[0:0-len(ext
)]
102 print "Extension did not match on", filename
103 raise EntryParseError("ext")
104 for (regex
, parseVersion
) in versionRegex
:
105 match
= regex
.match(filename
)
107 (self
.progname
, self
.version
) = parseVersion(match
)
111 print "Version regex did not match on", filename
112 raise EntryParseError("ver")
114 def deleteFile(self
):
115 path
= (self
.directory
+ "/" + self
.filename
).replace("//", "/")
116 print "Deleting", path
121 return self
.filename
== y
.filename
124 return self
.version
>= y
.version
127 print "OpenWRT download directory cleanup utility"
128 print "Usage: " + sys
.argv
[0] + " path/to/dl"
136 # Create a directory listing and parse the file names.
138 for filename
in os
.listdir(directory
):
139 if filename
== "." or filename
== "..":
141 for black
in blacklist
:
142 if black
.match(filename
):
144 print filename
, "is blacklisted"
148 entries
.append(Entry(directory
, filename
))
149 except (EntryParseError
), e
: pass
151 # Create a map of programs
153 for entry
in entries
:
154 if entry
.progname
in progmap
.keys():
155 progmap
[entry
.progname
].append(entry
)
157 progmap
[entry
.progname
] = [entry
,]
159 # Traverse the program map and delete everything but the last version
162 versions
= progmap
[prog
]
163 for version
in versions
:
164 if lastVersion
is None or version
>= lastVersion
:
165 lastVersion
= version
167 for version
in versions
:
168 if version
!= lastVersion
:
171 print "Keeping", lastVersion
.filename
175 if __name__
== "__main__":
176 sys
.exit(main(sys
.argv
))
This page took 0.056235 seconds and 5 git commands to generate.