-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.py
55 lines (47 loc) · 1.86 KB
/
compiler.py
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
import os
import subprocess
import sys
import json
def get_version(version_file):
try:
with open(version_file, "r") as file:
data = json.load(file)
return data.get("version", "unknown_version")
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error reading version file: {e}")
sys.exit(1)
def compile_with_pyinstaller():
project_dir = os.getcwd()
script_file = os.path.join(project_dir, "main.py")
icon_path = os.path.abspath(os.path.join(project_dir, "images", "icon.ico"))
images_dir = os.path.abspath(os.path.join(project_dir, "images"))
version_file = os.path.abspath(os.path.join(project_dir, "version.json"))
required_files = [script_file, icon_path, version_file]
missing_files = [f for f in required_files if not os.path.exists(f)]
if missing_files:
print(f"Error: The following required files are missing: {', '.join(missing_files)}")
sys.exit(1)
version = get_version(version_file)
sanitized_version = version.replace(" ", "_").replace("/", "_")
output_name = f"Buoy_v{sanitized_version}"
command = [
sys.executable, "-m", "PyInstaller",
"--onefile",
"--noconsole",
f"--add-data={images_dir};images",
f"--add-data={version_file};.",
f"--icon={icon_path}",
"--name", output_name,
script_file
]
try:
subprocess.run(command, check=True)
print(f"Compilation complete. Executable '{output_name}.exe' created in the 'dist' directory.")
except subprocess.CalledProcessError as e:
print(f"Compilation failed: {e}")
def bundled_file_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.dirname(__file__), relative_path)
if __name__ == "__main__":
compile_with_pyinstaller()