Update image-minimizer for py3

Also switch to using argparse instead of optparse.
This commit is contained in:
Brian C. Lane 2015-05-07 15:15:20 -07:00
parent ca0bb2681e
commit cb7dadb35f

View File

@ -1,8 +1,8 @@
#!/usr/bin/python2 #!/usr/bin/python3
# #
# image-minimizer: removes files and packages on the filesystem # image-minimizer: removes files and packages on the filesystem
# #
# Copyright 2007-2014 Red Hat, Inc. # Copyright 2007-2015 Red Hat, Inc.
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -17,7 +17,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import glob import glob
import optparse import argparse
import os import os
import sys import sys
import rpm import rpm
@ -53,7 +53,7 @@ class ImageMinimizer:
def add_pattern(self, files, pattern): def add_pattern(self, files, pattern):
globs = glob.glob(pattern) globs = glob.glob(pattern)
if self.verbose and len(globs) == 0: if self.verbose and len(globs) == 0:
print "%s file not found" % pattern print("%s file not found" % pattern)
for g in globs: for g in globs:
if os.path.isdir(g): if os.path.isdir(g):
self.add_directory(files, g) self.add_directory(files, g)
@ -72,7 +72,7 @@ class ImageMinimizer:
not_found = False not_found = False
rpms.add(hdr['name']) rpms.add(hdr['name'])
if self.verbose and not_found: if self.verbose and not_found:
print "%s package not found" % pattern print("%s package not found" % pattern)
# Parses each line in the ifle # Parses each line in the ifle
def parse_line(self, line): def parse_line(self, line):
@ -116,34 +116,34 @@ class ImageMinimizer:
self.visited.add(tag) self.visited.add(tag)
else: else:
if self.dryrun: if self.dryrun:
print 'rm ' + tag print('rm ' + tag)
else: else:
if self.verbose: if self.verbose:
print 'rm ' + tag print('rm ' + tag)
os.remove(tag) os.remove(tag)
#remove all empty directory. Every 8k counts! #remove all empty directory. Every 8k counts!
for d in sorted(self.visited, reverse=True): for d in sorted(self.visited, reverse=True):
if len(os.listdir(d)) == 0: if len(os.listdir(d)) == 0:
if self.dryrun: if self.dryrun:
print 'rm -rf ' + d print('rm -rf ' + d)
else: else:
if self.verbose: if self.verbose:
print 'rm -rf ' + d print('rm -rf ' + d)
os.rmdir(d) os.rmdir(d)
def remove_rpm(self): def remove_rpm(self):
def runCallback(reason, amount, total, key, client_data): def runCallback(reason, amount, total, key, client_data):
if self.verbose and reason == rpm.RPMCALLBACK_UNINST_STOP: if self.verbose and reason == rpm.RPMCALLBACK_UNINST_STOP:
print key, "erased" print(key, "erased")
if len(self.drops_rpm) == 0: if len(self.drops_rpm) == 0:
return return
for pkg in self.drops_rpm: for pkg in self.drops_rpm:
if self.dryrun: if self.dryrun:
print "erasing ", pkg print("erasing ", pkg)
else: else:
self.ts.addErase(pkg) self.ts.addErase(pkg)
if not self.dryrun: if not self.dryrun:
@ -158,40 +158,35 @@ class ImageMinimizer:
def parse_options(): def parse_options():
usage = "usage: %prog [options] filename" parser = argparse.ArgumentParser(description="Image Minimizer")
parser = optparse.OptionParser(usage=usage)
parser.set_defaults(root=os.environ.get('INSTALL_ROOT', '/mnt/sysimage/'), dry_run=False) parser.set_defaults(root=os.environ.get('INSTALL_ROOT', '/mnt/sysimage/'), dry_run=False)
parser.add_option("-i", "--installroot", type="string", dest="root", parser.add_argument("-i", "--installroot", metavar="STRING", dest="root",
help="Root path to prepend to all file patterns and installation root for RPM " help="Root path to prepend to all file patterns and installation root for RPM "
"operations. Defaults to INSTALL_ROOT or /mnt/sysimage/") "operations. Defaults to INSTALL_ROOT or /mnt/sysimage/")
parser.add_option("--dryrun", action="store_true", dest="dryrun", parser.add_argument("--dryrun", metavar="BOOL", action="store_true", dest="dryrun",
help="If set, no filesystem changes are made.") help="If set, no filesystem changes are made.")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", parser.add_argument("-v", "--verbose", metavar="BOOL", action="store_true", dest="verbose",
help="Display every action as it is performed.") help="Display every action as it is performed.")
(options, args) = parser.parse_args() parser.add_argument("filename", metavar="STRING", help="Filename to process")
if len(args) == 0:
parser.print_help()
sys.exit(1)
return (options, args) return parser.parse_args()
def main(): def main():
try: try:
(options, args) = parse_options() args = parse_options()
filename = args[0] minimizer = ImageMinimizer(args.filename, args.root, args.dryrun,
minimizer = ImageMinimizer(filename, options.root, options.dryrun, args.verbose)
options.verbose)
minimizer.filter() minimizer.filter()
except SystemExit, e: except SystemExit as e:
sys.exit(e.code) sys.exit(e.code)
except KeyboardInterrupt, e: except KeyboardInterrupt as e:
print >> sys.stderr, "Aborted at user request" print("Aborted at user request", file=sys.stderr)
if __name__ == "__main__": if __name__ == "__main__":
main() main()