2013-11-25 20 views
9

我试图实现北斗期间setuptools的build编译期间,但下面的代码中install明确build命令期间运行的编译和不运行。运行自定义setuptools的建设安装

#!/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。似乎我应该实施“Compass”构建扩展。

回答

5

不幸的是,我还没有找到答案。好像运行后安装脚本正确还有的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太多,所以最正确的方式(espetially如果你想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.