在ins期间运行自定义设置工具生成

2024-05-17 04:36:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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命令实现实例。简直 不可思议!但我认为问题在于setuptools和{}之间的复杂关系。有人知道如何在python2.7上的install期间运行自定义构建吗?在

更新:发现install绝对不调用build命令,但它调用运行build_ext的{}。似乎我应该实现“Compass”构建扩展。在


Tags: installpathimport命令buildselfcompassos
1条回答
网友
1楼 · 发布于 2024-05-17 04:36:42

不幸的是,我还没有找到答案。似乎可以正确运行安装后脚本only at Distutils 2.现在您可以使用以下解决方法:

更新:由于setuptools的堆栈检查,我们应该重写install.do_egg_install,而不是{}方法:

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命令,该命令也由{}使用,因此最正确的方法(尤其是如果您想使easy_install工作)是重写{}命令。完整代码:

^{pr2}$

你可以看看我是怎么用这个here.

相关问题 更多 >