2010-08-09 4 views
8

distutils에서 바이트 코드 전용 배포판을 만들고 싶습니다 (실제로는 없습니다, 내가하는 일을 알고 있습니다). setuptools 및 bdist_egg 명령을 사용하면 --exclude-source 매개 변수를 제공 할 수 있습니다. 불행히도 표준 명령에는 그러한 옵션이 없습니다.distutils 바이너리 배포판에서 소스를 제거하는 방법은 무엇입니까?

  • tar.gz, zip, rpm 또는 deb 생성 직전에 소스 파일을 쉽게 제거 할 수 있습니까?
  • 이렇게하려면 명령별로 비교적 깨끗한 방법이 있습니까 (예 : tar.gz 또는 zip).
+1

매우 비슷합니다 : http://stackoverflow.com/questions/261638/how-do-i-protect-python-code 컴파일 된 파이썬 파일 (pyc/pyo)은 디 컴파일하기 매우 쉽습니다. –

+8

@ 닉 :별로. 나는 보호에 대해 전혀 언급하지 않았으며 그 질문은 distutils에 대해서는 언급하지 않았다. 분명히 파이썬 바이트 코드는 분석하기 쉽습니다. 이제 실제로 질문 한 문제를 해결하는 방법은 무엇입니까? – Draemon

+0

zip에서 모든 * .py 파일을 그냥 제거하고 싶다면 :'7z d archive.zip * .py -r' –

답변

11

distutils "build_py"명령은 배포판을 만드는 모든 명령에 의해 (간접적으로) 재사용되므로 중요합니다. 당신이 byte_compile (파일) 방법, 뭔가 같은 오버라이드 (override)하는 경우 :

try: 
    from setuptools.command.build_py import build_py 
except ImportError: 
    from distutils.command.build_py import build_py 

class build_py(build_py) 
    def byte_compile(self, files): 
     super(build_py, self).byte_compile(files) 
     for file in files: 
      if file.endswith('.py'): 
       os.unlink(file) 

setup(
    ... 
    cmdclass = dict(build_py=build_py), 
    ... 
) 

을 소스 파일들이 ("설치"디렉토리에 복사되기 전에 빌드 트리에서 삭제되도록 당신은 그것을 만들 수 있어야 bdist 명령이 명령을 호출 할 때 임시 디렉토리입니다).

참고 :이 코드는 테스트하지 않았습니다. YMMV.

+0

+1. 이것은 내가 바라는 종류 일뿐입니다. 나는 일반적인 build_py가있을 수 있다는 것을 깨닫지 못했습니다. 나는 이것을 시험해보고 그것이 어떤 조정이 필요한지 알게 될 것이다. – Draemon

+1

+1하지만 파이썬 2.7.6에서는 build_py가 이전 스타일의 클래스이고 super()와 함께 사용할 수 없으므로 작동하지 않습니다. 대신'build_py.byte_compile (self, files)'를 사용했습니다. (나는 또한 build_py 클래스를 가져 와서 가져온 build_py를 clobber하지 않았다.) –

1

이 시도 : 여기

from distutils.command.install_lib import install_lib 

class install_lib(install_lib, object): 

    """ Class to overload install_lib so we remove .py files from the resulting 
    RPM """ 

    def run(self): 

     """ Overload the run method and remove all .py files after compilation 
     """ 

     super(install_lib, self).run() 
     for filename in self.install(): 
      if filename.endswith('.py'): 
       os.unlink(filename) 

    def get_outputs(self): 

     """ Overload the get_outputs method and remove any .py entries in the 
     file list """ 

     filenames = super(install_lib, self).get_outputs() 
     return [filename for filename in filenames 
       if not filename.endswith('.py')] 
1

어쩌면 전체 작업 코드 :

try: 
     from setuptools.command.build_py import build_py 
except ImportError: 
     from distutils.command.build_py import build_py 

import os 
import py_compile 

class custom_build_pyc(build_py): 
    def byte_compile(self, files): 
     for file in files: 
      if file.endswith('.py'): 
       py_compile.compile(file) 
       os.unlink(file) 
.... 
setup(
    name= 'sample project', 
    cmdclass = dict(build_py=custom_build_pyc), 
.... 
0

"표준 명령이 그러한 옵션이 없습니다"?

setuptools의 최신 버전을 설치 했습니까? 그리고 setup.py 파일을 작성하셨습니까?

그렇다면 작동합니다 : python setup.py bdist_egg --exclude-source-files.

+0

필자는 bdist_egg에 대해이 작업을 수행했다는 점에 주목했다. 옵션이없는 것은 다른 출력 (예 : zip)이었습니다. – Draemon

관련 문제