加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

比较Python中两个PyInstaller生成的Linux可执行文件

发布时间:2020-12-16 22:28:15 所属栏目:Python 来源:网络整理
导读:问题很简单,但我没有看到任何样本. 我需要比较PyInstaller生成的两个可执行文件,并确定哪个是较新的(但不是简单的时间戳).时间戳可能更新,但内容保持不变.只有当两个时间戳都更新且内容不同时,才需要替换旧文件. 任何示例解决方案例如PyInstaller中的简单版

问题很简单,但我没有看到任何样本.

我需要比较PyInstaller生成的两个可执行文件,并确定哪个是较新的(但不是简单的时间戳).时间戳可能更新,但内容保持不变.只有当两个时间戳都更新且内容不同时,才需要替换旧文件.

任何示例解决方案例如PyInstaller中的简单版本标签(奇怪但无法找到很多信息,在手册中仅说明使用Windows版本文件)

更新:

> Linux可执行文件
>可以访问文件生成过程.
>它是cli应用程序,最好是不使用vcs,一些简单的解决方案.
>实际比较过程将在Python脚本中进行
>按照建议尝试了filecmp – 即使对于生成2次的相同构建,它也返回False(浅= False标志).

就我的观点而言,最好的选择是比较内容和时间戳.如果时间戳较新且内容不同=>意味着新版本.

最佳答案
运行pyinstaller时,必须确保执行可重现的构建.即可用于在可执行文件之间执行比特比较的一个.根据docs:

Python uses a random hash to make dicts and other hashed types,and this affects compiled byte-code as well as PyInstaller internal data structures. As a result,two builds may not produce bit-for-bit identical results even when all the components of the application bundle are the same and the two applications execute in identical ways.

为此,只需在运行pyinstaller之前将PYTHONHASHSEED环境变量设置为常量:

PYTHONHASHSEED=1
export PYTHONHASHSEED
pyinstaller --onefile test.py

unset PYTHONHASHSEED

然后你可以使用你想要比较可执行文件的任何工具/模块,比如filecmp,BeyondCompare等,甚至只是Linux中的一个简单校验和:

cksum dist/test

编辑:关于时间戳或标记二进制文件 – 您可以执行以下操作以在构建后为Linux二进制文件添加其他注释:

# Create a file with the notes or comments to add to the binary. 
# I am adding the current date for versioning info
date > version

# Add notes to the binary
objcopy --add-section .pyversion=version --set-section-flags .pyversion=noload,readonly dist/test dist/test-with-version

# Check the version/notes on the new binary
objdump -sj .pyversion dist/test-with-version

你应该得到类似的东西:

dist/test-with-version:     file format elf64-x86-64
Contents of section .pyversion:
0000 46726920 53657020 31342031 343a3339  Fri Sep 14 14:39
0010 3a333620 41455354 20323031 380a      :36 AEST 2018.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读