【问题标题】:Running custom setuptools build during install在安装期间运行自定义 setuptools 构建
【发布时间】:2013-12-10 06:24:49
【问题描述】:

我尝试在 setuptools 的 build 期间实现 Compass 编译,但以下代码在显式 build 命令期间运行编译,而在 install 期间不运行。

#!/usr/bin/env python

import os
import setuptools
from distutils.command.build import build


SETUP_DIR = os.path.dirname(os.path.abspath(__file__))


class BuildCSS(setuptools.Command):
    description = 'build CSS from SCSS'

    user_options = []

    def initialize_options(self):
        pass

    def run(self):
        os.chdir(os.path.join(SETUP_DIR, 'django_project_dir', 'compass_project_dir'))
        import platform
        if 'Windows' == platform.system():
            command = 'compass.bat compile'
        else:
            command = 'compass compile'
        import subprocess
        try:
            subprocess.check_call(command.split())
        except (subprocess.CalledProcessError, OSError):
            print 'ERROR: problems with compiling Sass. Is Compass installed?'
            raise SystemExit
        os.chdir(SETUP_DIR)

    def finalize_options(self):
        pass


class Build(build):
    sub_commands = build.sub_commands + [('build_css', None)]


setuptools.setup(
    # Custom attrs here.
    cmdclass={
        'build': Build,
        'build_css': BuildCSS,
    },
)

Build.run 的任何自定义指令(例如一些打印)也不适用于install,但dist 实例在commands 属性中仅包含我的build 命令实现实例。极好的!但我认为问题在于setuptoolsdistutils 之间的复杂关系。有人知道如何在 Python 2.7 上的 install 期间运行自定义构建吗?

更新:发现install肯定不会调用build命令,而是调用bdist_egg运行build_ext。似乎我应该实现“指南针”构建扩展。

【问题讨论】:

    标签: python setuptools distutils


    【解决方案1】:

    很遗憾,我还没有找到答案。似乎能够正确运行安装后脚本有only at Distutils 2. 现在您可以使用此解决方法:

    更新:由于 setuptools 的堆栈检查,我们应该覆盖 install.do_egg_install,而不是 run 方法:

    from setuptools.command.install import install
    
    class Install(install):
        def do_egg_install(self):
            self.run_command('build_css')
            install.do_egg_install(self)
    

    Update2: easy_install 完全运行 bdist_egg 也被 install 使用的命令,所以最正确的方法(尤其是如果你想让 easy_install 工作)是覆盖bdist_egg 命令。完整代码:

    #!/usr/bin/env python
    
    import setuptools
    from distutils.command.build import build as _build
    from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
    
    
    class bdist_egg(_bdist_egg):
        def run(self):
            self.run_command('build_css')
            _bdist_egg.run(self)
    
    
    class build_css(setuptools.Command):
        description = 'build CSS from SCSS'
    
        user_options = []
    
        def initialize_options(self):
            pass
    
        def finalize_options(self):
            pass
    
        def run(self):
            pass # Here goes CSS compilation.
    
    
    class build(_build):
        sub_commands = _build.sub_commands + [('build_css', None)]
    
    
    setuptools.setup(
    
        # Here your setup args.
    
        cmdclass={
            'bdist_egg': bdist_egg,
            'build': build,
            'build_css': build_css,
        },
    )
    

    你可能会看到我是如何使用这个here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多