Index: pym/portage.py =================================================================== --- pym/portage.py (revision 2794) +++ pym/portage.py (working copy) @@ -2743,8 +2743,10 @@ # if any of these are being called, handle them -- running them out of the sandbox -- and stop now. if mydo=="clean": - logfile=None - if mydo in ["help","clean","setup"]: + import portage_ebuild + portage_ebuild.ebuild_clean(mysettings) + return 0 + elif mydo in ["help","setup"]: return spawn(EBUILD_SH_BINARY+" "+mydo,mysettings,debug=debug,free=1,logfile=logfile) elif mydo in ["prerm","postrm","preinst","postinst","config"]: mysettings.load_infodir(pkg_dir) Index: pym/portage_ebuild.py =================================================================== --- pym/portage_ebuild.py (revision 0) +++ pym/portage_ebuild.py (revision 0) @@ -0,0 +1,70 @@ + +from portage_exec import spawn +from portage_util import writemsg +import errno, os, shutil + +def ebuild_clean(mysettings): + """This function is called from doebuild to perform the clean phase.""" + + chflags = {"BSD":"noschg,nouchg,nosappnd,nouappnd,nosunlnk,nouunlnk", + "Darwin":"noschg,nouchg,nosappnd,nouappnd"} + if mysettings["USERLAND"] in chflags: + spawn(["chflags", "-R", chflags[mysettings["USERLAND"]], + mysettings["PORTAGE_BUILDDIR"]]) + + for subdir in ("image", "distdir"): + shutil.rmtree(os.path.join(mysettings["PORTAGE_BUILDDIR"], subdir), ignore_errors=True) + + features = mysettings["FEATURES"].split() + if "keeptemp" in features: + try: + os.rename(os.path.join(mysettings["T"], "environment"), + os.path.join(mysettings["T"], "environment.keeptemp")) + except OSError, oe: + if oe.errno == errno.ENOENT: + pass + elif oe.errno in (errno.EPERM, errno.EACCES): + writemsg("%s\n" % oe) + else: + raise oe + else: + shutil.rmtree(mysettings["T"], ignore_errors=True) + if "keepwork" not in features: + for x in (".compiled", ".installed", ".packaged", ".tested", ".unpacked"): + try: + os.unlink(os.path.join(mysettings["PORTAGE_BUILDDIR"], x)) + except OSError, oe: + if oe.errno == errno.ENOENT: + pass + elif oe.errno in (errno.EPERM, errno.EACCES): + writemsg("%s\n" % oe) + else: + raise oe + shutil.rmtree(os.path.join(mysettings["PORTAGE_BUILDDIR"], "build-info"), ignore_errors=True) + shutil.rmtree(mysettings["WORKDIR"], ignore_errors=True) + + # remove directories only if they are empty, leaving any files intact + if os.path.isfile(os.path.join(mysettings["PORTAGE_BUILDDIR"], ".unpacked")): + for dirpath, dirnames, filenames in os.walk(mysettings["PORTAGE_BUILDDIR"], topdown=False): + for name in dirnames: + mypath = os.path.join(dirpath, name) + if not mypath.startswith(mysettings["WORKDIR"]): + try: + os.rmdir(mypath) + except OSError, oe: + if oe.errno in (errno.ENOTEMPTY, errno.ENOENT): + pass + elif oe.errno in (errno.EPERM, errno.EACCES): + writemsg("%s\n" % oe) + else: + raise oe + + try: + os.rmdir(mysettings["PORTAGE_BUILDDIR"]) + except OSError, oe: + if oe.errno in (errno.ENOTEMPTY, errno.ENOENT): + pass + elif oe.errno in (errno.EPERM, errno.EACCES): + writemsg("%s\n" % oe) + else: + raise oe