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:

  1. if user has used python interpreter other 1 picked path, post install script executed different interpreter might cause problem.
  2. 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:

  1. get full path python interpreter executing setup.py sys.executable.
  2. classes inheriting distutils.cmd.command (such distutils.command.install.install use here) implement execute 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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -