如何从 distutils 二进制发行版中剥离源代码?

发布于 2024-09-13 16:32:34 字数 246 浏览 6 评论 0原文

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

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

I want to create a bytecode-only distribution from distutils (no really, I do; I know what I'm doing). Using setuptools and the bdist_egg command, you can simply provide the --exclude-source parameter. Unfortunately the standard commands don't have such an option.

  • Is there an easy way to strip the source files just before the tar.gz, zip, rpm or deb is created.
  • Is there a relatively clean per-command way to do this (eg just for tar.gz or zip).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻许诺言 2024-09-20 16:32:34

distutils“build_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),
    ...
)

您应该能够做到这一点,以便在将源文件复制到“安装”目录(这是 bdist 时的临时目录)之前从构建树中删除源文件命令调用它们)。

注意:我没有测试过这段代码; YMMV。

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.

亽野灬性zι浪 2024-09-20 16:32:34

试试这个:

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

Try this:

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')]
○闲身 2024-09-20 16:32:34

也许这里有完整的工作代码:)

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

Maybe a full working code here :)

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),
....
双马尾 2024-09-20 16:32:34

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

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

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

"the standard commands don't have such an option"?

Do you have the latest version of setuptools installed? And did you write a setup.py file?

If so, this should work: python setup.py bdist_egg --exclude-source-files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文