How to avoid overwriting previous package installation with distutils
Question: I have a python package that uses distutils. I would like to configure the
setup.py to do either of the following:

  * Detect a previously-installed version of the package and raise an error
  * Offer to remove the previously-installed version before proceeding with the installation

Any hints? A custom subclass of distutils.command.install is probably needed,
but the [documentation](http://docs.python.org/2/distutils/apiref.html#module-
distutils.command.install) is a bit terse.


Answer: Ok, here's my initial answer. Hopefully somebody else has a better plan. I'm
not sure if Install.install_libbase is the correct place to look or if it just
happens to be correct on my system..

    
    
    import distutils.command.install
    
    class Install(distutils.command.install.install):
        def run(self):
            name = self.config_vars['dist_name']
            if name in os.listdir(self.install_libbase):
                raise Exception("It appears another version of %s is already "
                                "installed at %s; remove this before installing." 
                                % (name, self.install_libbase))
            print("Installing to %s" % self.install_libbase)
            return distutils.command.install.install.run(self)
    
    setup(cmdclass={'install': Install})
    

