Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从distutils二进制分布中剥离源?_Python_Bytecode_Distutils - Fatal编程技术网

Python 如何从distutils二进制分布中剥离源?

Python 如何从distutils二进制分布中剥离源?,python,bytecode,distutils,Python,Bytecode,Distutils,我想从distutils创建一个仅字节码的发行版(不,我真的这么做了;我知道我在做什么)。使用setuptools和bdist_egg命令,您可以简单地提供--exclude源参数。不幸的是,标准命令没有这样的选项 在创建tar.gz、zip、rpm或deb之前,是否有一种简单的方法来剥离源文件 是否有一种相对干净的按命令执行的方法(例如仅针对tar.gz或zip) distutils“build\u py”命令是一个重要的命令,因为它(间接地)被创建发行版的所有命令重用。如果重写byte_c

我想从distutils创建一个仅字节码的发行版(不,我真的这么做了;我知道我在做什么)。使用setuptools和bdist_egg命令,您可以简单地提供--exclude源参数。不幸的是,标准命令没有这样的选项

  • 在创建tar.gz、zip、rpm或deb之前,是否有一种简单的方法来剥离源文件
  • 是否有一种相对干净的按命令执行的方法(例如仅针对tar.gz或zip)
distutils“build\u py”命令是一个重要的命令,因为它(间接地)被创建发行版的所有命令重用。如果重写byte_compile(files)方法,则类似于:

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),
    ...
)
您应该能够使源文件在复制到“install”目录(当bdist命令调用它们时,这是一个临时目录)之前从构建树中删除

注意:我没有测试这个代码;YMMV.

试试这个:

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')]

这里可能有完整的工作代码:)

“标准命令没有这样的选项”

您是否安装了最新版本的
setuptools
?您是否编写了
setup.py
文件


如果是这样,这应该可以工作:
python setup.py bdist_egg--排除源文件

非常相似:编译的python文件(pyc/pyo)对于反编译来说相当简单。@Nick:不太可能。我根本没有提到保护,这个问题也没有提到歧视。显然,python字节码很容易分析,现在解决我实际提出的问题怎么样?如果您只想从zip中删除所有的*.py文件:
7z d archive.zip*.py-r
我意识到我可以在以后删除它们,但我不想首先添加.py文件。执行一个外部工具并不好,需要7z更糟糕;因此,我问如何使用distutils来实现它。这正是我所希望的。我没有意识到有一个共同的构建,我可以钩住。我会试试这个,看看它是否需要调整。+1但在Python 2.7.6上它不起作用,因为build_py是一个老式类,不能与super()一起使用。我使用了
build\u py.byte\u compile(self,files)
。(我还重命名了build_py类,这样它就不会破坏导入的build_py。)我在问题中指出,我成功地为bdist_egg实现了这一点。其他输出(如zip)缺少选项。
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),
....