dl_cleanup: Port to Python 3
SVN-Revision: 47156
This commit is contained in:
parent
f0b83fd20f
commit
9644e33c65
1 changed files with 29 additions and 26 deletions
|
@ -1,12 +1,14 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
# OpenWrt download directory cleanup utility.
|
# OpenWrt download directory cleanup utility.
|
||||||
# Delete all but the very last version of the program tarballs.
|
# Delete all but the very last version of the program tarballs.
|
||||||
#
|
#
|
||||||
# Copyright (C) 2010 Michael Buesch <mb@bu3sch.de>
|
# Copyright (C) 2010-2015 Michael Buesch <m@bues.ch>
|
||||||
# Copyright (C) 2013 OpenWrt.org
|
# Copyright (C) 2013-2015 OpenWrt.org
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -28,7 +30,7 @@ def parseVer_123(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
try:
|
try:
|
||||||
patchlevel = match.group(5)
|
patchlevel = match.group(5)
|
||||||
except (IndexError), e:
|
except IndexError as e:
|
||||||
patchlevel = None
|
patchlevel = None
|
||||||
if patchlevel:
|
if patchlevel:
|
||||||
patchlevel = ord(patchlevel[0])
|
patchlevel = ord(patchlevel[0])
|
||||||
|
@ -44,7 +46,7 @@ def parseVer_12(match, filepath):
|
||||||
progname = match.group(1)
|
progname = match.group(1)
|
||||||
try:
|
try:
|
||||||
patchlevel = match.group(4)
|
patchlevel = match.group(4)
|
||||||
except (IndexError), e:
|
except IndexError as e:
|
||||||
patchlevel = None
|
patchlevel = None
|
||||||
if patchlevel:
|
if patchlevel:
|
||||||
patchlevel = ord(patchlevel[0])
|
patchlevel = ord(patchlevel[0])
|
||||||
|
@ -121,7 +123,7 @@ class Entry:
|
||||||
self.fileext = ext
|
self.fileext = ext
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print self.filename, "has an unknown file-extension"
|
print(self.filename, "has an unknown file-extension")
|
||||||
raise EntryParseError("ext")
|
raise EntryParseError("ext")
|
||||||
for (regex, parseVersion) in versionRegex:
|
for (regex, parseVersion) in versionRegex:
|
||||||
match = regex.match(filename)
|
match = regex.match(filename)
|
||||||
|
@ -130,28 +132,28 @@ class Entry:
|
||||||
match, directory + "/" + filename + self.fileext)
|
match, directory + "/" + filename + self.fileext)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print self.filename, "has an unknown version pattern"
|
print(self.filename, "has an unknown version pattern")
|
||||||
raise EntryParseError("ver")
|
raise EntryParseError("ver")
|
||||||
|
|
||||||
|
def getPath(self):
|
||||||
|
return (self.directory + "/" + self.filename).replace("//", "/")
|
||||||
|
|
||||||
def deleteFile(self):
|
def deleteFile(self):
|
||||||
path = (self.directory + "/" + self.filename).replace("//", "/")
|
path = self.getPath()
|
||||||
print "Deleting", path
|
print("Deleting", path)
|
||||||
if not opt_dryrun:
|
if not opt_dryrun:
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
|
|
||||||
def __eq__(self, y):
|
|
||||||
return self.filename == y.filename
|
|
||||||
|
|
||||||
def __ge__(self, y):
|
def __ge__(self, y):
|
||||||
return self.version >= y.version
|
return self.version >= y.version
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print "OpenWrt download directory cleanup utility"
|
print("OpenWrt download directory cleanup utility")
|
||||||
print "Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>"
|
print("Usage: " + sys.argv[0] + " [OPTIONS] <path/to/dl>")
|
||||||
print ""
|
print("")
|
||||||
print " -d|--dry-run Do a dry-run. Don't delete any files"
|
print(" -d|--dry-run Do a dry-run. Don't delete any files")
|
||||||
print " -B|--show-blacklist Show the blacklist and exit"
|
print(" -B|--show-blacklist Show the blacklist and exit")
|
||||||
print " -w|--whitelist ITEM Remove ITEM from blacklist"
|
print(" -w|--whitelist ITEM Remove ITEM from blacklist")
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
global opt_dryrun
|
global opt_dryrun
|
||||||
|
@ -163,7 +165,7 @@ def main(argv):
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
usage()
|
usage()
|
||||||
return 1
|
return 1
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError as e:
|
||||||
usage()
|
usage()
|
||||||
return 1
|
return 1
|
||||||
directory = args[0]
|
directory = args[0]
|
||||||
|
@ -180,12 +182,12 @@ def main(argv):
|
||||||
del blacklist[i]
|
del blacklist[i]
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print "Whitelist error: Item", v,\
|
print("Whitelist error: Item", v,\
|
||||||
"is not in blacklist"
|
"is not in blacklist")
|
||||||
return 1
|
return 1
|
||||||
if o in ("-B", "--show-blacklist"):
|
if o in ("-B", "--show-blacklist"):
|
||||||
for (name, regex) in blacklist:
|
for (name, regex) in blacklist:
|
||||||
print name
|
print(name)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# Create a directory listing and parse the file names.
|
# Create a directory listing and parse the file names.
|
||||||
|
@ -196,12 +198,13 @@ def main(argv):
|
||||||
for (name, regex) in blacklist:
|
for (name, regex) in blacklist:
|
||||||
if regex.match(filename):
|
if regex.match(filename):
|
||||||
if opt_dryrun:
|
if opt_dryrun:
|
||||||
print filename, "is blacklisted"
|
print(filename, "is blacklisted")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
entries.append(Entry(directory, filename))
|
entries.append(Entry(directory, filename))
|
||||||
except (EntryParseError), e: pass
|
except EntryParseError as e:
|
||||||
|
pass
|
||||||
|
|
||||||
# Create a map of programs
|
# Create a map of programs
|
||||||
progmap = {}
|
progmap = {}
|
||||||
|
@ -220,10 +223,10 @@ def main(argv):
|
||||||
lastVersion = version
|
lastVersion = version
|
||||||
if lastVersion:
|
if lastVersion:
|
||||||
for version in versions:
|
for version in versions:
|
||||||
if version != lastVersion:
|
if version is not lastVersion:
|
||||||
version.deleteFile()
|
version.deleteFile()
|
||||||
if opt_dryrun:
|
if opt_dryrun:
|
||||||
print "Keeping", lastVersion.filename
|
print("Keeping", lastVersion.getPath())
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue