Files
mind-reader/build_scripts/build_exe.py
T

118 lines
2.9 KiB
Python

import argparse
import os
import shutil
import subprocess
import sys
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
ENTRY_POINT = os.path.join(PROJECT_ROOT, "src", "mind_reader", "app.py")
def make_archive(source_dir, output_archive):
seven_zip_cmd = shutil.which("7z") or shutil.which("7za")
default_7z_paths = [
r"C:\Program Files\7-Zip\7z.exe",
r"C:\Program Files (x86)\7-Zip\7z.exe",
]
for path in default_7z_paths:
if os.path.exists(path):
seven_zip_cmd = path
break
if seven_zip_cmd:
print(f"\nCreating 7z archive using {seven_zip_cmd}...")
cmd = [
seven_zip_cmd,
"a",
"-t7z",
"-mx=9",
"-m0=lzma2",
output_archive,
os.path.join(source_dir, "*"),
]
result = subprocess.run(cmd)
if result.returncode == 0:
print(f"Archive successfully created: {output_archive}")
else:
print("Error while archiving with 7-Zip!")
else:
print("\n7-Zip not found in system! Falling back to standard zip...")
zip_base = output_archive.replace(".7z", "")
archive_path = shutil.make_archive(
base_name=zip_base, format="zip", root_dir=source_dir
)
print(f"Standard ZIP created: {archive_path}")
def build(archive=False):
cmd = [
sys.executable,
"-m",
"PyInstaller",
"--noconfirm",
"--clean",
"--onedir",
"--windowed",
"--name",
"MindReader",
"--paths",
os.path.join(PROJECT_ROOT, "src"),
ENTRY_POINT,
]
result = subprocess.run(cmd, cwd=PROJECT_ROOT)
if result.returncode == 0:
dist_path = os.path.join(PROJECT_ROOT, "dist", "MindReader")
print("\nBuild completed!")
print(f"App dir: {dist_path}")
if archive:
archive_target = os.path.join(
PROJECT_ROOT, "dist", "MindReader-win64.7z"
)
make_archive(dist_path, archive_target)
else:
print("\nError during build!")
def clean():
print("Cleaning...")
dirs_to_remove = [
os.path.join(PROJECT_ROOT, "build"),
os.path.join(PROJECT_ROOT, "dist"),
]
for d in dirs_to_remove:
if os.path.exists(d):
shutil.rmtree(d)
print(f"{d} - deleted")
for item in os.listdir(PROJECT_ROOT):
if item.endswith(".spec"):
spec_path = os.path.join(PROJECT_ROOT, item)
os.remove(spec_path)
print(f"{spec_path} - deleted")
print("Cleaning completed!")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--clean-only", action="store_true")
parser.add_argument("--archive", action="store_true")
args = parser.parse_args()
if args.clean_only:
clean()
else:
build(archive=args.archive)