2015-10-16 7 views
8

Cソースからビルドされた外部バイナリを呼び出すPythonモジュールがあります。Pythonのsetuptools/setup.pyを使ってC実行可能ファイルをコンパイルしてインストールしますか?

外部実行可能ファイルのソースは、.tar.gzファイルとして配布されるPythonモジュールの一部です。

解凍し、その外部実行可能ファイルをコンパイルし、setuptools/setup.pyを使用してインストールする方法はありますか?私のバイナリ部分を作る

  • 仮想環境
  • にそのバイナリをインストールsetup.py installを使用して、バイナリのコンパイル/インストールを管理、setup.py buildなど
  • :私は何を達成したいことはある

    pythonモジュールであるため、外部依存関係のない車輪として配布することができます。

+0

この問題を解決しましたか?もしそうなら、ソリューションを共有できますか?ありがとう –

+0

私は最後にやった、私はすぐに答えを追加します。 –

答えて

3

最終的にsetup.pyを使用して、インストールを行ったコマンドのハンドラを追加します。

これを行いsetup.pyの例は次のようになります。

import os 
from setuptools import setup 
from setuptools.command.install import install 
import subprocess 

def get_virtualenv_path(): 
    """Used to work out path to install compiled binaries to.""" 
    if hasattr(sys, 'real_prefix'): 
     return sys.prefix 

    if hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix: 
     return sys.prefix 

    if 'conda' in sys.prefix: 
     return sys.prefix 

    return None 


def compile_and_install_software(): 
    """Used the subprocess module to compile/install the C software.""" 
    src_path = './some_c_package/' 

    # compile the software 
    cmd = "./configure CFLAGS='-03 -w -fPIC'" 
    venv = get_virtualenv_path() 
    if venv: 
     cmd += ' --prefix=' + os.path.abspath(venv) 
    subprocess.check_call(cmd, cwd=src_path, shell=True) 

    # install the software (into the virtualenv bin dir if present) 
    subprocess.check_call('make install', cwd=src_path, shell=True) 


class CustomInstall(install): 
    """Custom handler for the 'install' command.""" 
    def run(self): 
     compile_and_install_software() 
     super().run() 


setup(name='foo', 
     # ...other settings skipped... 
     cmdclass={'install': CustomInstall}) 

は今python setup.py installが呼び出されたときに、カスタムCustomInstallクラスが使用され、これは、コンパイルし、通常のインストール手順を実行する前に、ソフトウェアをインストールします。

興味のある他のステップ(例:build/develop/bdist_eggなど)でも同様の操作を行うことができます。

代替方法は、compile_and_install_software()の機能をsetuptools.Commandのサブクラスにして、完全な本格的なsetuptoolsコマンドを作成することです。

これはもっと複雑ですが、別のコマンドのサブコマンドとして指定したり(例:2回実行しないなど)、コマンドラインでカスタムオプションを渡すような操作を行うことができます。

関連する問題