1

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 is a bit terse.

Luke
  • 11,374
  • 2
  • 48
  • 61

1 Answers1

0

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})
Luke
  • 11,374
  • 2
  • 48
  • 61