qua*_*oke 9 python distutils setuptools
我尝试在setuptools期间实现Compass编译build,但是下面的代码在显式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,
},
)
Run Code Online (Sandbox Code Playgroud)
任何自定义指令Build.run(例如某些打印)也不适用install,但dist实例commands仅在属性中包含我的build命令实现实例.难以置信!但我认为问题在于setuptools和之间的复杂关系distutils.有没有人知道如何install在Python 2.7上运行自定义构建?
更新:发现install肯定没有调用build命令,但它调用bdist_egg哪个运行build_ext.好像我应该实现"Compass"构建扩展.
不幸的是,我还没有找到答案.似乎只能在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)
Run Code Online (Sandbox Code Playgroud)
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,
},
)
Run Code Online (Sandbox Code Playgroud)