2013-11-25 21 views
9

Tôi đã cố gắng triển khai tính năng biên soạn Compass trong các công cụ thiết lập build, nhưng mã sau chạy trình biên dịch trong lệnh build rõ ràng và không chạy trong install.Chạy các công cụ cài đặt tùy chỉnh được xây dựng trong khi cài đặt

#!/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, 
    }, 
) 

Bất kỳ hướng dẫn tùy chỉnh tại Build.run (ví dụ một số in) không áp dụng trong install quá, nhưng dist dụ chứa trong commands thuộc tính duy nhất của tôi build trường hợp thực hiện lệnh. Đáng kinh ngạc! Nhưng tôi nghĩ rằng rắc rối là trong quan hệ phức tạp giữa setuptoolsdistutils. Có ai biết làm thế nào để làm cho xây dựng tùy chỉnh chạy trong install trên Python 2,7?

Cập nhật: Tìm thấy rằng install chắc chắn không gọi build lệnh, nhưng nó gọi bdist_egg chạy build_ext. Có vẻ như tôi nên triển khai tiện ích mở rộng "Compass".

Trả lời

5

Unfortunatelly, tôi chưa tìm thấy câu trả lời. Có vẻ như khả năng chạy cài đặt bài kịch bản một cách chính xác có only at Distutils 2. Bây giờ bạn có thể sử dụng công việc xung quanh này:

Cập nhật: Vì chồng kiểm tra setuptools', chúng ta nên ghi đè install.do_egg_install, không run phương pháp:

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 chạy chính xác bdist_egg lệnh được sử dụng bởi install quá, vì vậy cách đúng nhất (espetially nếu bạn muốn chắc easy_install làm việc) là để ghi đè bdist_egg lệnh. Toàn bộ mã:

#!/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, 
    }, 
) 

Bạn có thể thấy làm thế nào tôi đã sử dụng này here.

Các vấn đề liên quan