dl_cleanup.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. # Copyright (C) 2013 OpenWrt.org
  8. """
  9. import sys
  10. import os
  11. import re
  12. import getopt
  13. # Commandline options
  14. opt_dryrun = False
  15. def parseVer_1234(match, filepath):
  16. progname = match.group(1)
  17. progversion = (int(match.group(2)) << 64) |\
  18. (int(match.group(3)) << 48) |\
  19. (int(match.group(4)) << 32) |\
  20. (int(match.group(5)) << 16)
  21. return (progname, progversion)
  22. def parseVer_123(match, filepath):
  23. progname = match.group(1)
  24. try:
  25. patchlevel = match.group(5)
  26. except (IndexError), e:
  27. patchlevel = None
  28. if patchlevel:
  29. patchlevel = ord(patchlevel[0])
  30. else:
  31. patchlevel = 0
  32. progversion = (int(match.group(2)) << 64) |\
  33. (int(match.group(3)) << 48) |\
  34. (int(match.group(4)) << 32) |\
  35. patchlevel
  36. return (progname, progversion)
  37. def parseVer_12(match, filepath):
  38. progname = match.group(1)
  39. try:
  40. patchlevel = match.group(4)
  41. except (IndexError), e:
  42. patchlevel = None
  43. if patchlevel:
  44. patchlevel = ord(patchlevel[0])
  45. else:
  46. patchlevel = 0
  47. progversion = (int(match.group(2)) << 64) |\
  48. (int(match.group(3)) << 48) |\
  49. patchlevel
  50. return (progname, progversion)
  51. def parseVer_r(match, filepath):
  52. progname = match.group(1)
  53. progversion = (int(match.group(2)) << 64)
  54. return (progname, progversion)
  55. def parseVer_ymd(match, filepath):
  56. progname = match.group(1)
  57. progversion = (int(match.group(2)) << 64) |\
  58. (int(match.group(3)) << 48) |\
  59. (int(match.group(4)) << 32)
  60. return (progname, progversion)
  61. def parseVer_GIT(match, filepath):
  62. progname = match.group(1)
  63. st = os.stat(filepath)
  64. progversion = int(st.st_mtime) << 64
  65. return (progname, progversion)
  66. extensions = (
  67. ".tar.gz",
  68. ".tar.bz2",
  69. ".tar.xz",
  70. ".orig.tar.gz",
  71. ".orig.tar.bz2",
  72. ".orig.tar.xz",
  73. ".zip",
  74. ".tgz",
  75. ".tbz",
  76. ".txz",
  77. )
  78. versionRegex = (
  79. (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
  80. (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
  81. (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
  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
  86. )
  87. blacklist = [
  88. ("linux", re.compile(r"linux-.*")),
  89. ("gcc", re.compile(r"gcc-.*")),
  90. ("wl_apsta", re.compile(r"wl_apsta.*")),
  91. (".fw", re.compile(r".*\.fw")),
  92. (".arm", re.compile(r".*\.arm")),
  93. (".bin", re.compile(r".*\.bin")),
  94. ("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
  95. ]
  96. class EntryParseError(Exception): pass
  97. class Entry:
  98. def __init__(self, directory, filename):
  99. self.directory = directory
  100. self.filename = filename
  101. self.progname = ""
  102. self.fileext = ""
  103. for ext in extensions:
  104. if filename.endswith(ext):
  105. filename = filename[0:0-len(ext)]
  106. self.fileext = ext
  107. break
  108. else:
  109. print self.filename, "has an unknown file-extension"
  110. raise EntryParseError("ext")
  111. for (regex, parseVersion) in versionRegex:
  112. match = regex.match(filename)
  113. if match:
  114. (self.progname, self.version) = parseVersion(
  115. match, directory + "/" + filename + self.fileext)
  116. break
  117. else:
  118. print self.filename, "has an unknown version pattern"
  119. raise EntryParseError("ver")
  120. def deleteFile(self):
  121. path = (self.directory + "/" + self.filename).replace("//", "/")
  122. print "Deleting", path
  123. if not opt_dryrun:
  124. os.unlink(path)
  125. def __eq__(self, y):
  126. return self.filename == y.filename
  127. def __ge__(self, y):
  128. return self.version >= y.version
  129. def usage():
  130. print "OpenWrt download directory cleanup utility"
  131. print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
  132. print ""
  133. print " -d|--dry-run Do a dry-run. Don't delete any files"
  134. print " -B|--show-blacklist Show the blacklist and exit"
  135. print " -w|--whitelist ITEM Remove ITEM from blacklist"
  136. def main(argv):
  137. global opt_dryrun
  138. try:
  139. (opts, args) = getopt.getopt(argv[1:],
  140. "hdBw:",
  141. [ "help", "dry-run", "show-blacklist", "whitelist=", ])
  142. if len(args) != 1:
  143. usage()
  144. return 1
  145. except getopt.GetoptError:
  146. usage()
  147. return 1
  148. directory = args[0]
  149. for (o, v) in opts:
  150. if o in ("-h", "--help"):
  151. usage()
  152. return 0
  153. if o in ("-d", "--dry-run"):
  154. opt_dryrun = True
  155. if o in ("-w", "--whitelist"):
  156. for i in range(0, len(blacklist)):
  157. (name, regex) = blacklist[i]
  158. if name == v:
  159. del blacklist[i]
  160. break
  161. else:
  162. print "Whitelist error: Item", v,\
  163. "is not in blacklist"
  164. return 1
  165. if o in ("-B", "--show-blacklist"):
  166. for (name, regex) in blacklist:
  167. print name
  168. return 0
  169. # Create a directory listing and parse the file names.
  170. entries = []
  171. for filename in os.listdir(directory):
  172. if filename == "." or filename == "..":
  173. continue
  174. for (name, regex) in blacklist:
  175. if regex.match(filename):
  176. if opt_dryrun:
  177. print filename, "is blacklisted"
  178. break
  179. else:
  180. try:
  181. entries.append(Entry(directory, filename))
  182. except (EntryParseError), e: pass
  183. # Create a map of programs
  184. progmap = {}
  185. for entry in entries:
  186. if entry.progname in progmap.keys():
  187. progmap[entry.progname].append(entry)
  188. else:
  189. progmap[entry.progname] = [entry,]
  190. # Traverse the program map and delete everything but the last version
  191. for prog in progmap:
  192. lastVersion = None
  193. versions = progmap[prog]
  194. for version in versions:
  195. if lastVersion is None or version >= lastVersion:
  196. lastVersion = version
  197. if lastVersion:
  198. for version in versions:
  199. if version != lastVersion:
  200. version.deleteFile()
  201. if opt_dryrun:
  202. print "Keeping", lastVersion.filename
  203. return 0
  204. if __name__ == "__main__":
  205. sys.exit(main(sys.argv))