2012-07-04 31 views
10

Tôi đã thêm một lệnh distutils tùy chỉnh cho một kịch bản setup.py:Hook để thêm lệnh vào các bản dựng xây dựng?

from distutils.command.build_py import build_py 

cmdclass = {} 
cmdclass['build_qt'] = BuildQt 
cmdclass['build_py'] = build_py 

setup(..., cmdclass=cmdclass, ...) 

Có cách nào để làm cho nó để khi chạy ::

python setup.py build 

cuộc gọi đầu tiên này

python setup.py build_qt 

tự động?

Trả lời

13

Bạn có thể ghi đè lên build:

from distutils.command.build import build 

class my_build(build): 
    def run(self): 
     self.run_command("build_qt") 
     build.run(self) 

cmdclass['build'] = my_build 
+0

Câu trả lời hay, mặc dù nó có thể được cải thiện bằng cách hiển thị cho chúng tôi nơi để tìm 'build'. – user1158559

+1

@ user1158559 đã được khắc phục, cảm ơn. – ecatmur

+0

Bạn đã kiếm được tiền của tôi – user1158559

0

Để thêm lệnh của riêng bạn, bạn có thể phân lớp các -Command mặc định build và mở rộng lệnh con của nó:

class _build(build): 
    sub_commands = [('build_qt', None)] + build.sub_commands 

... 
setup(..., cmdclass={'build': _build, ...}) 

Documentation (distutils.cmd. Lệnh):

# 'sub_commands' formalizes the notion of a "family" of commands, 
# eg. "install" as the parent with sub-commands "install_lib", 
# "install_headers", etc. The parent of a family of commands 
# defines 'sub_commands' as a class attribute; it's a list of 
# (command_name : string, predicate : unbound_method | string | None) 
# tuples, where 'predicate' is a method of the parent command that 
# determines whether the corresponding command is applicable in the 
# current situation. (Eg. we "install_headers" is only applicable if 
# we have any C header files to install.) If 'predicate' is None, 
# that command is always applicable. 
# 
# 'sub_commands' is usually defined at the *end* of a class, because 
# predicates can be unbound methods, so they must already have been 
# defined. The canonical example is the "install" command. 
sub_commands = [] 
Các vấn đề liên quan