在安装过程中运行自定义setuptools构建 [英] Running custom setuptools build during install
问题描述
我试图在setuptools的 build
期间实现Compass编译,但是以下代码在显式的 build
命令,并且不会在安装
期间运行。
I've tried to implement Compass compiling during setuptools' build
, but the following code runs compilation during explicit build
command and doesn't runs during 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
和 distutils
之间的复杂关系。有人知道如何在Python 2.7的安装
期间运行自定义构建吗?
Any custom instructions at Build.run
(e.g. some printing) doesn't apply during install
too, but dist
instance contains in commands
attribute only my build
command implementation instances. Incredible! But I think the trouble is in complex relations between setuptools
and distutils
. Does anybody knows how to make custom building run during install
on Python 2.7?
更新:发现 install
绝对不会调用 build
命令,但会调用 bdist_egg
运行 build_ext
。似乎我应该实现 Compass构建扩展。
Update: Found that install
definitely doesn't calls build
command, but it calls bdist_egg
which runs build_ext
. Seems like I should implement "Compass" build extension.
推荐答案
不幸的是,我没有找到答案。似乎能够正确运行安装后脚本的功能仅在Distutils 2上存在 >现在您可以使用以下解决方法:
Unfortunatelly, I haven't found the answer. Seems like the ability to run post-install scripts correctly there's only at Distutils 2. Now you can use this work-around:
更新:由于setuptools的堆栈检查,我们应该覆盖 install.do_egg_install
,而不是 run
方法:
Update: Because of setuptools' stack checks, we should override install.do_egg_install
, not run
method:
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
命令。整个代码:
Update2: easy_install
runs exactly bdist_egg
command which is used by install
too, so the most correct way (espetially if you want to make easy_install
work) is to override bdist_egg
command. Whole code:
#!/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,
},
)
在这里查看我的用法。
这篇关于在安装过程中运行自定义setuptools构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!