Execute a Python script post install using distutils / setuptools -
i'm trying add post-install task python distutils described in how extend distutils simple post install script?. task supposed execute python script in installed lib directory. script generates additional python modules installed package requires.
my first attempt follows:
from distutils.core import setup distutils.command.install import install class post_install(install): def run(self): install.run(self) subprocess import call call(['python', 'scriptname.py'], cwd=self.install_lib + 'packagename') setup( ... cmdclass={'install': post_install}, )
this approach works, far can tell has 2 deficiencies:
- if user has used python interpreter other 1 picked
path
, post install script executed different interpreter might cause problem. - it's not safe against dry-run etc. might able remedy wrapping in function , calling
distutils.cmd.command.execute
.
how improve solution? there recommended way / best practice doing this? i'd avoid pulling in dependency if possible.
the way address these deficiences is:
- get full path python interpreter executing
setup.py
sys.executable
. classes inheriting
distutils.cmd.command
(suchdistutils.command.install.install
use here) implementexecute
method, executes given function in "safe way" i.e. respecting dry-run flag.note the
--dry-run
option broken , not work intended anyway.
i ended following solution:
import os, sys distutils.core import setup distutils.command.install import install _install def _post_install(dir): subprocess import call call([sys.executable, 'scriptname.py'], cwd=os.path.join(dir, 'packagename')) class install(_install): def run(self): _install.run(self) self.execute(_post_install, (self.install_lib,), msg="running post install task") setup( ... cmdclass={'install': install}, )
note use class name install
derived class because python setup.py --help-commands
use.
Comments
Post a Comment