2017-11-22 3 views
1

python packaging docs으로 작성된 조언과 구조를 따르려고합니다. 설정 기능에서 테스트의 종속성을 tests_require으로 지정할 수 있습니다. scripts을 지정하면 설치시 스크립트를 실행할 수 있습니다. 하지만 테스트 용 설정의 경우에만 실행되는 스크립트를 사용할 수 있습니까? PyPa setup.py 테스트 스크립트


편집 : 그것은 scripts에있는 파일을 의미하지 않는다

from setuptools import setup 
# To use a consistent encoding 
from codecs import open 
from os import path 
import subprocess 
from setuptools import setup 
from setuptools.command.test import test 

class setupTestRequirements(test, object): 
    def run_script(self): 
     cmd = ['bash', 'bin/test_dnf_requirements.sh'] 
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE) 
     ret = p.communicate() 
     print(ret[0]) 

    def run(self): 
     self.run_script() 
     super(setupTestRequirements, self).run() 

... 
setup(
    ... 
    scripts=['bin/functional_dnf_requirements.sh'], 
    install_requires=['jira', 'PyYAML'], 
    tests_require=['flake8', 'autopep8', 'mock'], 

    cmdclass={'test': setupTestRequirements} 
) 

답변

1

내 setup.py의 중요 부분은 패키지 설치에서 실행됩니다. scripts 키워드는 패키지 설치 후 독립 실행 형 프로그램으로 실행되도록 설정된 패키지의 python 파일을 표시하는 데 사용됩니다 (이름이 약간 잘못되었을 수도 있음). 예 :에 scripts에 추가하여 스크립트로이 파일을 표시하면

#!/usr/bin/env python 

if __name__ == '__main__': 
    print('eggs!') 

setup.py :

from setuptools import setup 

setup(
    ... 
    scripts=['spam'], 
) 

패키지 설치 후 spam를 실행할 수 있습니다 당신은 내용을 가진 파일 spam이 더 inf를위한

$ spam 
eggs! 

읽기 this tutorial : 터미널에서 독립 실행 형 프로그램으로 명령 줄 스크립트에서. 당신이 테스트에 사용자 정의 코드를 실행하려는 경우


지금, 당신은 기본을 test 명령을 무시합니다. 에 setup.py :

$ python setup.py test 
running test 
>>>> this is my custom test command <<<< 
running egg_info 
... 
running build_ext 

---------------------------------------------------------------------- 
Ran 0 tests in 0.000s 

OK 

편집 : 당신이 테스트를 실행하기 전에 사용자 정의 bash는 스크립트를 실행하려는 경우, 적응 테스트를 실행할 때

from setuptools.command.test import test 

class MyCustomTest(test): 

    def run(self): 
     print('>>>> this is my custom test command <<<<') 
     super().run() 

setup(
    ... 
    cmdclass={'test': MyCustomTest} 
) 

이제 추가 인쇄를 알 수 있습니다 MyCustomTest.run() 방법. 예 스크립트 script.sh :

#!/usr/bin/env bash 
echo -n ">>> this is my custom bash script <<<" 

setup.pyMyCustomTest 클래스를 적응 :

import subprocess 
from setuptools import setup 
from setuptools.command.test import test 


class MyCustomTest(test): 

    def run_some_script(self): 
     cmd = ['bash', 'script.sh'] 
     # python3.5 and above 
     # ret = subprocess.run(cmd, stdout=subprocess.PIPE, universal_newlines=True) 
     # print(ret.stdout) 

     # old python2 versions 
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE) 
     ret = p.communicate() 
     print(ret[0]) 

    def run(self): 
     self.run_some_script() 
     super().run() 

출력 : 설명은

$ python setup.py test 
running test 
>>> this is my custom bash script <<< 
running egg_info 
writing spam.egg-info/PKG-INFO 
writing dependency_links to spam.egg-info/dependency_links.txt 
writing top-level names to spam.egg-info/top_level.txt 
reading manifest file 'spam.egg-info/SOURCES.txt' 
writing manifest file 'spam.egg-info/SOURCES.txt' 
running build_ext 

---------------------------------------------------------------------- 
Ran 0 tests in 0.000s 

OK 
+0

감사합니다! 명령 클래스 dict의 값을 파이썬 함수 대신 bash 스크립트로 사용할 수 있습니까? 아니면 스크립트를 호출하는 함수를 작성해야합니까? –

+1

@LiamPieri 올바른 명령 클래스를 덮어 쓰는 클래스 여야합니다 (custom test 명령은'setuptools.command.test' 모듈의'test' 클래스를 오버라이드하는 클래스 여야합니다), custom'build' 명령은 class를 오버라이드하는 클래스 여야합니다 'distutils.command.build' 모듈의'build' 등). 아직도, 아무도 당신의 커맨드 클래스가 할 일을 규정하지 않기 때문에, 거기에서 bash 스크립트를 호출 할 수 있습니다. 나는 예를 들어 답을 곧 업데이트 할 것이다. – hoefling

+1

@LiamPieri는 업데이트 된 답변을 확인합니다. 하단에 bash 스크립트를 실행하는 예제가 추가되었으므로 이제는 'python2'로 실행 가능해야합니다 ... – hoefling