Add patch from kojiro to clean binary package metadata (Bug #266996 and to clean...
authorfuzzyray <fuzzyray@gentoo.org>
Tue, 5 May 2009 01:49:58 +0000 (01:49 -0000)
committerfuzzyray <fuzzyray@gentoo.org>
Tue, 5 May 2009 01:49:58 +0000 (01:49 -0000)
svn path=/; revision=581

trunk/src/eclean/eclean

index 4fdec10fcb9997e4fe3788bd4138ef2d75c26e39..55cc2a74b736ee629090a977684853381648424d 100644 (file)
@@ -1,8 +1,9 @@
-#!/usr/bin/env python
+#!/usr/bin/python
 # Copyright 2003-2005 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 # $Header: $
 
+from __future__ import with_statement
 
 ###############################################################################
 # Meta:
@@ -15,6 +16,7 @@ __description__ = "A cleaning tool for Gentoo distfiles and binaries."
 
 ###############################################################################
 # Python imports:
+
 import sys
 import os, stat
 import re
@@ -40,7 +42,6 @@ port_settings = portage.settings
 distdir = port_settings["DISTDIR"]
 pkgdir = port_settings["PKGDIR"]
 
-
 ###############################################################################
 # printVersion:
 def printVersion():
@@ -507,12 +508,13 @@ def exclDictMatch(excl_dict,pkg):
 # findDistfiles: find all obsolete distfiles.
 # XXX: what about cvs ebuilds? i should install some to see where it goes...
 def findDistfiles( \
+               myoptions, \
                exclude_dict={}, \
                destructive=False,\
                fetch_restricted=False, \
                package_names=False, \
                time_limit=0, \
-               size_limit=0):
+               size_limit=0,):
        # this regexp extracts files names from SRC_URI. It is not very precise,
        # but we don't care (may return empty strings, etc.), since it is fast.
        file_regexp = re.compile('([a-zA-Z0-9_,\.\-\+\~]*)[\s\)]')
@@ -553,7 +555,12 @@ def findDistfiles( \
                        except KeyError: continue
                del pkg_list
 
-       # create a dictionary of files which should be deleted 
+       # create a dictionary of files which should be deleted
+       if not (os.path.isdir(distdir)):
+               eerror("%s does not appear to be a directory." % distdir, myoptions['nocolor'])
+               eerror("Please set DISTDIR to a sane value.", myoptions['nocolor'])
+               eerror("(Check your /etc/make.conf and environment).", myoptions['nocolor'])
+               exit(1)
        for file in os.listdir(distdir):
                filepath = os.path.join(distdir, file)
                try: file_stat = os.stat(filepath)
@@ -594,12 +601,19 @@ def findDistfiles( \
 # XXX: packages are found only by symlinks. Maybe i should also return .tbz2
 #      files from All/ that have no corresponding symlinks.
 def findPackages( \
+               myoptions, \
                exclude_dict={}, \
                destructive=False, \
                time_limit=0, \
                package_names=False):
        clean_dict = {}
-       # create a full package dictionnary
+       # create a full package dictionary
+       
+       if not (os.path.isdir(pkgdir)):
+               eerror("%s does not appear to be a directory." % pkgdir, myoptions['nocolor'])
+               eerror("Please set PKGDIR to a sane value.", myoptions['nocolor'])
+               eerror("(Check your /etc/make.conf and environment).", myoptions['nocolor'])
+               exit(1)
        for root, dirs, files in os.walk(pkgdir):
                if root[-3:] == 'All': continue
                for file in files:
@@ -636,7 +650,7 @@ def findPackages( \
                        del clean_dict[mycpv]
                        continue
                if portage.cpv_getkey(mycpv) in cp_all:
-                       # exlusion because of --package-names 
+                       # exlusion because of --package-names
                        del clean_dict[mycpv]
 
        return clean_dict
@@ -678,7 +692,12 @@ def doCleanup(clean_dict,action,myoptions):
                                       "Do you want to delete this " \
                                       + file_type+"?"):
                        # non-interactive mode or positive answer. 
-                       # For each file,...
+                       # For each file, try to delete the file and clean it out
+                       # of Packages metadata file
+                       if action == 'packages':
+                               metadata = portage.getbinpkg.PackageIndex()
+                               with open(os.path.join(pkgdir, 'Packages')) as metadata_file:
+                                       metadata.read(metadata_file)
                        for file in clean_dict[mykey]:
                                # ...get its size...
                                filesize = 0
@@ -688,11 +707,21 @@ def doCleanup(clean_dict,action,myoptions):
                                        except: eerror("Could not read size of "\
                                                       +file, myoptions['nocolor'])
                                # ...and try to delete it.
-                               try: os.unlink(file)
-                               except: eerror("Could not delete "+file, \
-                                              myoptions['nocolor'])
+                               try:
+                                       os.unlink(file)
+                               except:
+                                       eerror("Could not delete "+file, \
+                                        myoptions['nocolor'])
                                # only count size if successfully deleted
-                               else: clean_size += filesize
+                               else:
+                                       clean_size += filesize
+                                       if action == 'packages':
+                                               metadata.packages[:] = [p for p in metadata.packages if 'CPV' in p and p['CPV'] != file]
+                               
+                       if action == 'packages':
+                               with open(os.path.join(pkgdir, 'Packages'), 'w') as metadata_file:
+                                       metadata.write(metadata_file)
+
        # return total size of deleted or to delete files
        return clean_size
 
@@ -709,13 +738,15 @@ def doAction(action,myoptions,exclude_dict={}):
                einfo("Building file list for "+action+" cleaning...", \
                      myoptions['nocolor'])
        if action == 'packages':
-               clean_dict = findPackages( \
+               clean_dict = findPackages(
+                       myoptions, \
                        exclude_dict=exclude_dict, \
                        destructive=myoptions['destructive'], \
                        package_names=myoptions['package-names'], \
                        time_limit=myoptions['time-limit'])
        else: 
                clean_dict = findDistfiles( \
+                       myoptions, \
                        exclude_dict=exclude_dict, \
                        destructive=myoptions['destructive'], \
                        fetch_restricted=myoptions['fetch-restricted'], \