“版本文件”是什么样子的?

2024-04-19 18:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用谷歌搜索了好几年都没有结果了。手册上说:

--version-file=FILE
    add a version resource from FILE to the exe

听起来不错。我想把版本信息放到我的可执行文件中。问题是我不知道“版本文件”是什么样子的,我找不到一个可以使用的例子。我会考虑一个版本文件的例子作为这个问题的一个可接受的答案。


我尝试过的

手册还说:

version
Windows NT family only. version='myversion.txt'. Use GrabVersion.py to steal a version resource from an executable, and then edit the ouput to create your own. (The syntax of version resources is so arcane that I wouldn't attempt to write one from scratch.)

我已经尝试了无数的可执行文件从我的系统现在。我只是不断地犯这些错误:

Traceback (most recent call last):
  File "C:\pyinstaller-2.0\utils\GrabVersion.py", line 42, in 
    vs  = versioninfo.decode(sys.argv[1])
  File "C:\pyinstaller-2.0\PyInstaller\utils\versioninfo.py", line 33, in decode
    nm = win32api.EnumResourceNames(h, RT_VERSION)[0]
IndexError: list index out of range

在没有版本信息的可执行文件上,并且:

Traceback (most recent call last):
  File "C:\pyinstaller-2.0\utils\GrabVersion.py", line 43, in 
    print vs
  File "C:\pyinstaller-2.0\PyInstaller\utils\versioninfo.py", line 147, in __repr__
    % (indent, self.ffi.__repr__(indent), indent,
  File "C:\pyinstaller-2.0\PyInstaller\utils\versioninfo.py", line 251, in __repr__
    "filevers=%s," % fv,
TypeError: not all arguments converted during string formatting

其余的。


Tags: toinfrompy可执行文件versionlineutils
3条回答

只是简单地看了一下资料来源。似乎版本文件应该是Python源代码本身,作为提供的版本文件,先读取,然后eval

GrabVersion.py脚本似乎会生成您已经发现的错误,因此我修改了FixedFileInfo__repr__函数,将元组参数手动转换为字符串。

Windowscmd.exe嵌入了一个Windows版本资源,这是GrabVersion.py的输出,您可以将其保存到文件并馈送到PyInstaller。

VSVersionInfo(
  ffi=FixedFileInfo(
    filevers=(6, 1, 7601, 17514),
    prodvers=(6, 1, 7601, 17514),
    mask=0x3f,
    flags=0x0,
    OS=0x40004,
    fileType=0x1,
    subtype=0x0,
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'040904B0',
        [StringStruct(u'CompanyName', u'Microsoft Corporation'),
        StringStruct(u'FileDescription', u'Windows Command Processor'),
        StringStruct(u'FileVersion', u'6.1.7601.17514 (win7sp1_rtm.101119-1850)'),
        StringStruct(u'InternalName', u'cmd'),
        StringStruct(u'LegalCopyright', u'\xa9 Microsoft Corporation. All rights reserved.'),
        StringStruct(u'OriginalFilename', u'Cmd.Exe'),
        StringStruct(u'ProductName', u'Microsoft\xae Windows\xae Operating System'),
        StringStruct(u'ProductVersion', u'6.1.7601.17514')])
      ]), 
    VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
  ]
)

我还没有尝试用PyInstaller设置版本资源,所以我不确定这是否有效,我会对你的反馈感兴趣。

我可能在前面的答案中遗漏了这一点,或者PyInstaller在最初提供这些答案之后已经更新了,但是PyInstaller的当前文档使用PyInstaller提供的命令行工具来教一个fairly simple method for this(尽管我在最初几次查看文档时遗漏了这一部分)。

将此工具指向系统上具有“好看的”版本信息的.exe文件,它将创建一个可读的、带注释的、可编辑的版本资源文件,您可以将其用作起点。

pyi-grab_version executable_with_version_resource

默认情况下,它会将文件file_version_info.txt写入工作目录。

在svn.exe的本地副本上运行上述命令将生成:

# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
  ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=(1, 9, 7, 30920),
prodvers=(1, 9, 7, 30920),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x0,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x4,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x1,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
  kids=[
StringFileInfo(
  [
  StringTable(
    u'040904B0',
    [StringStruct(u'CompanyName', u'Apache Software Foundation'),
    StringStruct(u'FileDescription', u'svn'),
    StringStruct(u'FileVersion', u'1.9.7'),
    StringStruct(u'InternalName', u'SVN'),
    StringStruct(u'LegalCopyright', u'Copyright (c) The Apache Software Foundation'),
    StringStruct(u'OriginalFilename', u'svn.exe'),
    StringStruct(u'ProductName', u'Subversion'),
    StringStruct(u'ProductVersion', u'1.9.7 (r1800392)')])
  ]), 
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
  ]
)

根据您的需要进行编辑,并将其作为--version-file反馈给PyInstaller,例如

pyinstaller [options] myscript.py --version-file file_version_info.txt

使用前面的答案创建版本文件另存为version.rc

找到filename.spec文件,打开它。 接下来,在该脚本中,找到:

exe = EXE(pyz,...)

在整个部分的末尾添加这段代码,以自动将版本信息嵌入到您的exe文件中

version='version.rc'

保存它,然后再次启动pyinstaller,这次使用以下代码运行安装程序:

pyinstaller filename.spec 

这不仅会创建exe文件本身,还包括所有的版本信息。

如果您可能没有想过,请用程序的文件名替换filename

相关问题 更多 >