8

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

  • 有没有一种简单的方法可以在创建 tar.gz、zip、rpm 或 deb 之前剥离源文件。
  • 是否有相对干净的每个命令方式来执行此操作(例如,仅用于 tar.gz 或 zip)。
4

4 回答 4

11

The distutils "build_py" command is the one that matters, as it's (indirectly) reused by all the commands that create distributions. If you override the byte_compile(files) method, something like:

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),
    ...
)

You should be able to make it so that the source files are deleted from the build tree before they're copied to the "install" directory (which is a temporary directory when bdist commands invoke them).

Note: I have not tested this code; YMMV.

于 2010-08-09T21:25:54.313 回答
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')]
于 2013-10-09T02:20:17.807 回答
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),
....
于 2015-04-18T14:31:30.570 回答
0

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

你有setuptools安装最新版本的吗?你写了一个setup.py文件吗?

如果是这样,这应该工作:python setup.py bdist_egg --exclude-source-files.

于 2016-09-22T02:24:20.033 回答