deinstall (Was: Re: [Distutils] setup.py clean questions )

Thomas Heller thomas.heller@ion-tof.com
Fri Feb 16 05:20:01 2001


> And, while we're at it, I wouldn't mind an uninstall either.

Here is a simple deinstall command, which you can
use in your setup script. This can at least be a base for
a discussion about a more complete one.

It simply does the reverse of install.

class deinstall(Command):
    description = "Remove all installed files"

    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass
        
    def run(self):
        self.run_command('build')
        build = self.get_finalized_command('build')
        install = self.get_finalized_command('install')
        self.announce("removing files")
        for n in 'platlib', 'purelib', 'headers', 'scripts', 'data':
            dstdir = getattr(install, 'install_' + n)
            try:
                srcdir = getattr(build, 'build_' + n)
            except AttributeError:
                pass
            else:
                self._removefiles(dstdir, srcdir)

    def _removefiles(self, dstdir, srcdir):
        if not os.path.isdir(srcdir):
            return
        for n in os.listdir(srcdir):
            name = os.path.join(dstdir, n)
            if os.path.isfile(name):
                self.announce("removing '%s'" % name)
                if not self.dry_run:
                    os.remove(name)
                    if os.path.splitext(name)[1] == '.py':
                        try:
                            os.remove(name + 'c')
                        except:
                            pass
                        try:
                            os.remove(name + 'o')
                        except:
                            pass
            elif os.path.isdir(name):
                self._removefiles(name, os.path.join(srcdir, n))
                if not self.dry_run:
                    os.rmdir(name)
            else:
                self.announce("skipping removal of '%s' (does not exist)" %\
                              name)

############################################################################


setup(...,
      cmdclass = {'deinstall': deinstall},
      ...)

Thomas