覆盖冻结的可执行文件



有没有办法针对使用 pyinstaller 构建的可执行文件运行覆盖?我尝试像python脚本一样运行它,它不喜欢可执行文件作为输入(我真的没想到它会工作),我怀疑答案是否定的,没有简单的方法来针对构建的可执行文件运行覆盖....(这是在视窗.exe上)

我正在使用的保险套餐

只是您从 nedbatchelder.com(http://nedbatchelder.com/code/coverage/)获得的"easy_install保险"的正常保险套餐

这不是一个完全公式化的答案,而是我到目前为止发现的。

根据我对pyinstaller工作原理的理解,二进制文件是由一个嵌入python解释器和引导加载脚本的小型C程序构造的。PyInstaller 构造的 EXE 在实际二进制文件结束后包含一个存档,其中包含 python 代码的资源。这在 http://www.pyinstaller.org/export/develop/project/doc/Manual.html#pyinstaller-archives 解释。

有来自 Pyinstaller/loader/iu.py Docs 的 iu.py。您应该能够创建要从二进制文件导入的导入挂钩。谷歌搜索pyinstaller反汇编器发现 https://bitbucket.org/Trundle/exetractor/src/00df9ce00e1a/exetractor/pyinstaller.py 看起来可能会提取必要的部分。

另一部分是二进制存档中的所有资源都将编译为 python 代码。最有可能的是,coverage.py 会给你无用的输出,就像在正常情况下运行时点击任何其他编译模块一样。

突出显示使用cover_pylib=True

我知道这是在你问这个问题很久之后,但我只是需要答案。 :)

使用当前的位桶源进行coverage.py,我能够成功地从PyInstaller生成的 EXE 文件中收集覆盖率数据。

在我的应用程序的主要来源中,我有条件地告诉覆盖范围开始收集这样的覆盖范围:

if os.environ.has_key('COVERAGE') and len(os.environ['COVERAGE']) > 0:
   usingCoverage = True
   import coverage
   import time
   cov = coverage.coverage(data_file='.coverage.' + version.GetFullString(), data_suffix=time.strftime(".%Y_%m_%d_%H_%M.%S", time.localtime()), cover_pylib=True)
   cov.start()

这仅在我想要时才开始承保范围收集。使用该data_suffix使我以后可以更轻松地利用cov.combine()进行覆盖文件合并。 version.GetFullString()只是我的应用程序版本号。

cover_pylib设置为在此处True,因为所有标准 Python 库模块__file__属性看起来像这样..._MEIXXXXXrandom.pyc,因此与包中不存在的其他代码无法区分(基于路径)。

当应用程序准备好退出时,我有这个小片段:

if usingCoverage:
   cov.stop()
   cov.save()

一旦我的应用程序运行完毕 coverage.py 仍然不会为我自动生成其 HTML 报告。需要清理覆盖率数据,以便将..._MEIXXXX...文件引用转换为实际源代码的绝对文件路径。

我通过运行以下代码片段来做到这一点:

import sys
import os.path
from coverage.data import CoverageData
from coverage import coverage
from glob import glob
def cleanupLines(data):
    """
    The coverage data collected via PyInstaller coverage needs the data fixed up
    so that coverage.py's report generation code can analyze the source code.
    PyInstaller __file__ attributes on code objecters are all in subdirectories of the     _MEIXXXX 
    temporary subdirectory. We need to replace the _MEIXXXX temp directory prefix with     the correct 
    prefix for each source file. 
    """
    prefix = None
    for file, lines in data.lines.iteritems():
        origFile = file
        if prefix is None:
            index = file.find('_MEI')
            if index >= 0:
                pathSepIndex = file.find('\', index)
                if pathSepIndex >= 0:
                    prefix = file[:pathSepIndex + 1]
        if prefix is not None and file.find(prefix) >= 0:
            file = file.replace(prefix, "", 1)
            for path in sys.path:
                if os.path.exists(path) and os.path.isdir(path):
                    fileName = os.path.join(path, file)
                    if os.path.exists(fileName) and os.path.isfile(fileName):
                        file = fileName
            if origFile != file:
                del data.lines[origFile]
                data.lines[file] = lines
for file in glob('.coverage.' + version.GetFullString() + '*'):
    print "Cleaning up: ", file
    data = CoverageData(file)
    data.read()
    cleanupLines(data)
    data.write()

此处的 for 循环仅用于确保清理将要合并的所有覆盖文件。

注意:默认情况下,此代码唯一不清理的覆盖率数据是PyInstaller相关文件,这些文件的__file__属性中不包含_MEIXXX数据。

您现在可以成功生成 HTML 或 XML(或其他任何内容),coverage.py正常方式进行报告。

就我而言,它看起来像这样:

cov = coverage(data_file='.coverage.' + version.GetFullString(), data_suffix='.combined')
cov.load()
cov.combine()
cov.save()
cov.load()
cov.html_report(ignore_errors=True,omit=[r'c:python27*', r'..3rdPartyPythonPackages*'])

在构造函数中使用data_file是为了确保加载/组合将正确识别我所有清理的覆盖文件。

html_report调用告诉coverage.py忽略标准 python 库(以及签入我的版本控制树的 Python 库),只关注我的应用程序代码。

我希望这有所帮助。

最新更新