summaryrefslogtreecommitdiff
path: root/scripts/dl_cleanup.py
blob: e83c82ac2c629ad1c2e9ab5e127cec52ec663289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""
# OpenWRT download directory cleanup utility.
# Delete all but the very last version of the program tarballs.
#
# Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
"""

import sys
import os
import re

DEBUG = 0


def parseVer_1234(match):
	progname = match.group(1)
	progversion = (int(match.group(2)) << 64) |\
		      (int(match.group(3)) << 48) |\
		      (int(match.group(4)) << 32) |\
		      (int(match.group(5)) << 16)
	return (progname, progversion)

def parseVer_123(match):
	progname = match.group(1)
	patchlevel = match.group(5)
	if patchlevel:
		patchlevel = ord(patchlevel[0])
	else:
		patchlevel = 0
	progversion = (int(match.group(2)) << 64) |\
		      (int(match.group(3)) << 48) |\
		      (int(match.group(4)) << 32) |\
		      patchlevel
	return (progname, progversion)

def parseVer_12(match):
	progname = match.group(1)
	patchlevel = match.group(4)
	if patchlevel:
		patchlevel = ord(patchlevel[0])
	else:
		patchlevel = 0
	progversion = (int(match.group(2)) << 64) |\
		      (int(match.group(3)) << 48) |\
		      patchlevel
	return (progname, progversion)

def parseVer_r(match):
	progname = match.group(1)
	progversion = (int(match.group(2)) << 64)
	return (progname, progversion)

def parseVer_ymd(match):
	progname = match.group(1)
	progversion = (int(match.group(2)) << 64) |\
		      (int(match.group(3)) << 48) |\
		      (int(match.group(4)) << 32)
	return (progname, progversion)

extensions = (
	".tar.gz",
	".tar.bz2",
	".orig.tar.gz",
	".orig.tar.bz2",
	".zip",
	".tgz",
	".tbz",
)

versionRegex = (
	(re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234),	# xxx-1.2.3.4
	(re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd),	# xxx-YYYY-MM-DD
	(re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123),	# xxx-1.2.3a
	(re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12),		# xxx-1.2a
	(re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r),				# xxx-r1111
)

blacklist = (
	re.compile(r"wl_apsta.*"),
	re.compile(r"boost.*"),
	re.compile(r".*\.fw"),
	re.compile(r".*\.arm"),
	re.compile(r".*\.bin"),
	re.compile(r"RT\d+_Firmware.*"),
)

class EntryParseError(Exception): pass

class Entry:
	def __init__(self, directory, filename):
		self.directory = directory
		self.filename = filename
		self.progname = ""

		for ext in extensions:
			if filename.endswith(ext):
				filename = filename[0:0-len(ext)]
				break
		else:
			if DEBUG:
				print "Extension did not match on", filename
			raise EntryParseError("ext")
		for (regex, parseVersion) in versionRegex:
			match = regex.match(filename)
			if match:
				(self.progname, self.version) = parseVersion(match)
				break
		else:
			if DEBUG:
				print "Version regex did not match on", filename
			raise EntryParseError("ver")

	def deleteFile(self):
		path = (self.directory + "/" + self.filename).replace("//", "/")
		print "Deleting", path
		if not DEBUG:
			os.unlink(path)

	def __eq__(self, y):
		return self.filename == y.filename

	def __ge__(self, y):
		return self.version >= y.version

def usage():
	print "OpenWRT download directory cleanup utility"
	print "Usage: " + sys.argv[0] + " path/to/dl"

def main(argv):
	if len(argv) != 2:
		usage()
		return 1
	directory = argv[1]

	# Create a directory listing and parse the file names.
	entries = []
	for filename in os.listdir(directory):
		if filename == "." or filename == "..":
			continue
		for black in blacklist:
			if black.match(filename):
				if DEBUG:
					print filename, "is blacklisted"
				break
		else:
			try:
				entries.append(Entry(directory, filename))
			except (EntryParseError), e: pass

	# Create a map of programs
	progmap = {}
	for entry in entries:
		if entry.progname in progmap.keys():
			progmap[entry.progname].append(entry)
		else:
			progmap[entry.progname] = [entry,]

	# Traverse the program map and delete everything but the last version
	for prog in progmap:
		lastVersion = None
		versions = progmap[prog]
		for version in versions:
			if lastVersion is None or version >= lastVersion:
				lastVersion = version
		if lastVersion:
			for version in versions:
				if version != lastVersion:
					version.deleteFile()
			if DEBUG:
				print "Keeping", lastVersion.filename

	return 0

if __name__ == "__main__":
	sys.exit(main(sys.argv))