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()
这不是一个完整的答案,但我目前所发现的。在
根据我对pyinstaller工作原理的理解,二进制文件是由一个嵌入python解释器和引导加载脚本的小型C程序构建的。PyInstaller构造的EXE在包含python代码资源的实际二进制文件结束后包含一个归档文件。这在这里解释http://www.pyinstaller.org/export/develop/project/doc/Manual.html#pyinstaller-archives。在
有每年从Pyinstaller/loader/每年Docs。您应该能够创建一个导入钩子来从二进制文件导入。google搜索pyinstaller反汇编程序发现了https://bitbucket.org/Trundle/exetractor/src/00df9ce00e1a/exetractor/pyinstaller.py,看起来它可能提取出必要的部分。在
另一部分是二进制文件中的所有资源都将被编译成python代码。很有可能,覆盖率.py在正常情况下运行时,将以与命中任何其他已编译模块相同的方式提供无用的输出。在
突出显示使用
cover_pylib=True
我知道你问这个问题已经很久了,但我只是想知道答案。:)
使用
coverage.py
的当前位桶源,我能够成功地从PyInstaller
生成的EXE文件中收集覆盖率数据。在在我的应用程序的主要来源中,我有条件地告诉保险范围开始收集保险范围,如下所示:
这只在我想要的时候才开始收集保险范围。使用data_后缀可以让我更容易地利用
cov.combine()
进行覆盖率文件的合并。version.GetFullString()
只是我的应用程序版本号。在在这里,
cover_pylib
被设置为True
,因为所有标准的Python库模块__file__
属性都像这样...\_MEIXXXXX\random.pyc
,因此与包中不存在的其他代码无法区分(基于路径)。在当应用程序准备退出时,我有一个小片段:
^{pr2}$一旦我的应用程序运行完毕覆盖率.py还是不能自动生成它的HTML报告。需要清理覆盖率数据,以便将
...\_MEIXXXX\...
文件引用转换为指向实际源代码的绝对文件路径。在我通过运行以下代码片段来实现这一点:
这里的for循环只是为了确保所有要合并的覆盖率文件都被清理干净。在
注意:默认情况下,此代码不清除的唯一覆盖率数据是
PyInstaller
相关文件,这些文件的_MEIXXX
数据不包含在其__file__
属性中。在现在您可以成功地生成一个HTML或XML(或其他任何东西)
coverage.py
报告。在在我的情况下,它看起来像这样:
在构造函数中使用data_文件是为了确保load/combine能够正确识别所有清理后的覆盖文件。在
html_report
调用告诉coverage.py
忽略标准的python库(以及签入我的版本控制树中的python库),只关注我的应用程序代码。在我希望这有帮助。在
相关问题 更多 >
编程相关推荐