-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_version_file.py
More file actions
84 lines (71 loc) · 2.55 KB
/
generate_version_file.py
File metadata and controls
84 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# generate_version_file.py
"""
Helper script for generating file_version_info.txt for PyInstaller builds.
This script extracts version and metadata directly from pyproject.toml.
Usage:
python generate_version_file.py
- Reads the [project] section from pyproject.toml.
- Writes a file_version_info.txt file in the format expected by PyInstaller.
- Keeps version and author info in sync with the main project metadata.
"""
import sys
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib # type: ignore[import]
except ImportError:
sys.exit("Error: tomllib or tomli is required. Please run 'pip install tomli'.")
print("Reading metadata from pyproject.toml...")
try:
with open("pyproject.toml", "rb") as f:
pyproject_data = tomllib.load(f)["project"]
APP_VERSION = pyproject_data["version"]
APP_NAME = pyproject_data["name"]
APP_AUTHOR = pyproject_data["authors"][0]["name"]
APP_DESCRIPTION = pyproject_data["description"]
except (FileNotFoundError, KeyError) as e:
sys.exit(f"Error: Could not read required metadata from pyproject.toml. {e}")
# PyInstaller expects a 4-part version tuple (e.g., 1, 2, 3, 0)
version_parts = APP_VERSION.split(".") + ["0"] * (4 - len(APP_VERSION.split(".")))
version_tuple_str = ", ".join(version_parts[:4])
VERSION_FILE_PATH = "file_version_info.txt"
print(f"Generating {VERSION_FILE_PATH} for version {APP_VERSION}...")
# Template for the version info file (used by PyInstaller)
file_content = f"""
# UTF-8
VSVersionInfo(
ffi=FixedFileInfo(
filevers=({version_tuple_str}),
prodvers=({version_tuple_str}),
mask=0x3f,
flags=0x0,
OS=0x40004,
fileType=0x1,
subtype=0x0,
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
'040904B0',
[StringStruct('CompanyName', '{APP_AUTHOR}'),
StringStruct('FileDescription', '{APP_DESCRIPTION}'),
StringStruct('FileVersion', '{APP_VERSION}'),
StringStruct('InternalName', '{APP_NAME}'),
StringStruct('LegalCopyright', 'Copyright © 2025 {APP_AUTHOR}'),
StringStruct('OriginalFilename', '{APP_NAME}.exe'),
StringStruct('ProductName', '{APP_NAME}'),
StringStruct('ProductVersion', '{APP_VERSION}')])
]),
VarFileInfo([VarStruct('Translation', [1033, 1200])])
]
)
"""
try:
with open(VERSION_FILE_PATH, "w", encoding="utf-8") as f:
f.write(file_content)
print("Successfully created file_version_info.txt.")
except IOError as e:
sys.exit(f"Error writing to {VERSION_FILE_PATH}: {e}")