dl_cleanup.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/usr/bin/env python3
  2. """
  3. # libreCMC download directory cleanup utility.
  4. # Delete all but the very last version of the program tarballs.
  5. #
  6. # Copyright (C) 2010-2015 Michael Buesch <m@bues.ch>
  7. # Copyright (C) 2013-2015 OpenWrt.org
  8. """
  9. from __future__ import print_function
  10. import sys
  11. import os
  12. import re
  13. import getopt
  14. import shutil
  15. # Commandline options
  16. opt_dryrun = False
  17. def parseVer_1234(match, filepath):
  18. progname = match.group(1)
  19. progversion = (
  20. (int(match.group(2)) << 64)
  21. | (int(match.group(3)) << 48)
  22. | (int(match.group(4)) << 32)
  23. | (int(match.group(5)) << 16)
  24. )
  25. return (progname, progversion)
  26. def parseVer_123(match, filepath):
  27. progname = match.group(1)
  28. try:
  29. patchlevel = match.group(5)
  30. except IndexError as e:
  31. patchlevel = None
  32. if patchlevel:
  33. patchlevel = ord(patchlevel[0])
  34. else:
  35. patchlevel = 0
  36. progversion = (
  37. (int(match.group(2)) << 64)
  38. | (int(match.group(3)) << 48)
  39. | (int(match.group(4)) << 32)
  40. | patchlevel
  41. )
  42. return (progname, progversion)
  43. def parseVer_12(match, filepath):
  44. progname = match.group(1)
  45. try:
  46. patchlevel = match.group(4)
  47. except IndexError as e:
  48. patchlevel = None
  49. if patchlevel:
  50. patchlevel = ord(patchlevel[0])
  51. else:
  52. patchlevel = 0
  53. progversion = (int(match.group(2)) << 64) | (int(match.group(3)) << 48) | patchlevel
  54. return (progname, progversion)
  55. def parseVer_r(match, filepath):
  56. progname = match.group(1)
  57. progversion = int(match.group(2)) << 64
  58. return (progname, progversion)
  59. def parseVer_ymd_GIT_SHASUM(match, filepath):
  60. progname = match.group(1)
  61. progversion = (
  62. (int(match.group(2)) << 64)
  63. | (int(match.group(3)) << 48)
  64. | (int(match.group(4)) << 32)
  65. )
  66. return (progname, progversion)
  67. def parseVer_ymd(match, filepath):
  68. progname = match.group(1)
  69. progversion = (
  70. (int(match.group(2)) << 64)
  71. | (int(match.group(3)) << 48)
  72. | (int(match.group(4)) << 32)
  73. )
  74. return (progname, progversion)
  75. def parseVer_GIT(match, filepath):
  76. progname = match.group(1)
  77. st = os.stat(filepath)
  78. progversion = int(st.st_mtime) << 64
  79. return (progname, progversion)
  80. extensions = (
  81. ".tar.gz",
  82. ".tar.bz2",
  83. ".tar.xz",
  84. ".orig.tar.gz",
  85. ".orig.tar.bz2",
  86. ".orig.tar.xz",
  87. ".zip",
  88. ".tgz",
  89. ".tbz",
  90. ".txz",
  91. )
  92. versionRegex = (
  93. (re.compile(r"(gcc[-_]\d+)\.(\d+)\.(\d+)"), parseVer_12), # gcc.1.2
  94. (re.compile(r"(linux[-_]\d+\.\d+)\.(\d+)"), parseVer_r), # linux.1
  95. (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)\.(\d+)"), parseVer_1234), # xxx-1.2.3.4
  96. (
  97. re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)-"),
  98. parseVer_ymd_GIT_SHASUM,
  99. ), # xxx-YYYY-MM-DD-GIT_SHASUM
  100. (re.compile(r"(.+)[-_](\d\d\d\d)-?(\d\d)-?(\d\d)"), parseVer_ymd), # xxx-YYYY-MM-DD
  101. (re.compile(r"(.+)[-_]([0-9a-fA-F]{40,40})"), parseVer_GIT), # xxx-GIT_SHASUM
  102. (re.compile(r"(.+)[-_](\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-1.2.3a
  103. (re.compile(r"(.+)[-_]v(\d+)\.(\d+)\.(\d+)(\w?)"), parseVer_123), # xxx-v1.2.3a
  104. (re.compile(r"(.+)[-_](\d+)_(\d+)_(\d+)"), parseVer_123), # xxx-1_2_3
  105. (re.compile(r"(.+)[-_](\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-1.2a
  106. (re.compile(r"(.+)[-_]v(\d+)\.(\d+)(\w?)"), parseVer_12), # xxx-v1.2a
  107. (re.compile(r"(.+)[-_]r?(\d+)"), parseVer_r), # xxx-r1111
  108. )
  109. blacklist = [
  110. ("wl_apsta", re.compile(r"wl_apsta.*")),
  111. (".fw", re.compile(r".*\.fw")),
  112. (".arm", re.compile(r".*\.arm")),
  113. (".bin", re.compile(r".*\.bin")),
  114. ("rt-firmware", re.compile(r"RT[\d\w]+_Firmware.*")),
  115. ]
  116. class EntryParseError(Exception):
  117. pass
  118. class Entry:
  119. def __init__(self, directory, builddir, filename):
  120. self.directory = directory
  121. self.filename = filename
  122. self.builddir = builddir
  123. self.progname = ""
  124. self.fileext = ""
  125. self.filenoext = ""
  126. if os.path.isdir(self.getPath()):
  127. self.filenoext = filename
  128. else:
  129. for ext in extensions:
  130. if filename.endswith(ext):
  131. filename = filename[0 : 0 - len(ext)]
  132. self.filenoext = filename
  133. self.fileext = ext
  134. break
  135. else:
  136. print(self.filename, "has an unknown file-extension")
  137. raise EntryParseError("ext")
  138. for (regex, parseVersion) in versionRegex:
  139. match = regex.match(filename)
  140. if match:
  141. (self.progname, self.version) = parseVersion(
  142. match, directory + "/" + filename + self.fileext
  143. )
  144. break
  145. else:
  146. print(self.filename, "has an unknown version pattern")
  147. raise EntryParseError("ver")
  148. def getPath(self):
  149. return (self.directory + "/" + self.filename).replace("//", "/")
  150. def getBuildPaths(self):
  151. paths = []
  152. for subdir in os.scandir(self.builddir):
  153. package_build_dir = os.path.join(subdir.path, self.filenoext)
  154. if os.path.exists(package_build_dir):
  155. paths.append(package_build_dir)
  156. return paths
  157. def deleteFile(self):
  158. path = self.getPath()
  159. print("Deleting", path)
  160. if not opt_dryrun:
  161. if os.path.isdir(path):
  162. shutil.rmtree(path)
  163. else:
  164. os.unlink(path)
  165. def deleteBuildDir(self):
  166. paths = self.getBuildPaths()
  167. for path in paths:
  168. print("Deleting BuildDir", path)
  169. if not opt_dryrun:
  170. shutil.rmtree(path)
  171. def __ge__(self, y):
  172. return self.version >= y.version
  173. def usage():
  174. print("libreCMC download directory cleanup utility")
  175. print("Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>")
  176. print("")
  177. print(" -d|--dry-run Do a dry-run. Don't delete any files")
  178. print(" -B|--show-blacklist Show the blacklist and exit")
  179. print(" -w|--whitelist ITEM Remove ITEM from blacklist")
  180. print(
  181. " -D|--download-dir Provide path to dl dir to clean also the build directory"
  182. )
  183. print(
  184. " -b|--build-dir Provide path to build dir to clean also the build directory"
  185. )
  186. def main(argv):
  187. global opt_dryrun
  188. try:
  189. (opts, args) = getopt.getopt(
  190. argv[1:],
  191. "hdBw:D:b:",
  192. [
  193. "help",
  194. "dry-run",
  195. "show-blacklist",
  196. "whitelist=",
  197. "download-dir=",
  198. "build-dir=",
  199. ],
  200. )
  201. except getopt.GetoptError as e:
  202. usage()
  203. return 1
  204. directory = "dl/"
  205. builddir = "build_dir/"
  206. for (o, v) in opts:
  207. if o in ("-h", "--help"):
  208. usage()
  209. return 0
  210. if o in ("-d", "--dry-run"):
  211. opt_dryrun = True
  212. if o in ("-w", "--whitelist"):
  213. for i in range(0, len(blacklist)):
  214. (name, regex) = blacklist[i]
  215. if name == v:
  216. del blacklist[i]
  217. break
  218. else:
  219. print("Whitelist error: Item", v, "is not in blacklist")
  220. return 1
  221. if o in ("-B", "--show-blacklist"):
  222. for (name, regex) in blacklist:
  223. sep = "\t\t"
  224. if len(name) >= 8:
  225. sep = "\t"
  226. print("%s%s(%s)" % (name, sep, regex.pattern))
  227. return 0
  228. if o in ("-D", "--download-dir"):
  229. directory = v
  230. if o in ("-b", "--build-dir"):
  231. builddir = v
  232. if args:
  233. directory = args[0]
  234. if not os.path.exists(directory):
  235. print("Can't find download directory", directory)
  236. return 1
  237. if not os.path.exists(builddir):
  238. print("Can't find build directory", builddir)
  239. return 1
  240. # Create a directory listing and parse the file names.
  241. entries = []
  242. for filename in os.listdir(directory):
  243. if filename == "." or filename == "..":
  244. continue
  245. for (name, regex) in blacklist:
  246. if regex.match(filename):
  247. if opt_dryrun:
  248. print(filename, "is blacklisted")
  249. break
  250. else:
  251. try:
  252. entries.append(Entry(directory, builddir, filename))
  253. except EntryParseError as e:
  254. pass
  255. # Create a map of programs
  256. progmap = {}
  257. for entry in entries:
  258. if entry.progname in progmap.keys():
  259. progmap[entry.progname].append(entry)
  260. else:
  261. progmap[entry.progname] = [
  262. entry,
  263. ]
  264. # Traverse the program map and delete everything but the last version
  265. for prog in progmap:
  266. lastVersion = None
  267. versions = progmap[prog]
  268. for version in versions:
  269. if lastVersion:
  270. if os.path.isdir(lastVersion.getPath()) and not os.path.isdir(version.getPath()):
  271. continue
  272. if lastVersion is None or version >= lastVersion:
  273. lastVersion = version
  274. if lastVersion:
  275. for version in versions:
  276. if version is not lastVersion:
  277. version.deleteFile()
  278. if builddir:
  279. version.deleteBuildDir()
  280. if opt_dryrun:
  281. print("Keeping", lastVersion.getPath())
  282. return 0
  283. if __name__ == "__main__":
  284. sys.exit(main(sys.argv))