Initial commit
This commit is contained in:
77
build.py
Normal file
77
build.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
APP_NAME = "BackupCopier"
|
||||
VERSION = "1.0.0"
|
||||
MAIN_SCRIPT = "main.py"
|
||||
ICON_PATH = "icon.ico"
|
||||
EXE_PATH = os.path.join("dist", f"{APP_NAME}.exe")
|
||||
ZIP_PATH = os.path.join("dist", f"{APP_NAME}-{VERSION}.zip")
|
||||
|
||||
|
||||
def ensure_build_deps() -> None:
|
||||
try:
|
||||
__import__("PyInstaller")
|
||||
except Exception:
|
||||
print("[ERROR] Missing dependency: PyInstaller")
|
||||
print(f"[ERROR] Install in this interpreter: {sys.executable} -m pip install pyinstaller")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def run_build() -> None:
|
||||
icon_abs = os.path.abspath(ICON_PATH)
|
||||
has_icon = os.path.exists(icon_abs)
|
||||
|
||||
cmd = [
|
||||
sys.executable,
|
||||
"-m",
|
||||
"PyInstaller",
|
||||
"--noconfirm",
|
||||
"--clean",
|
||||
"--onefile",
|
||||
"--windowed",
|
||||
f"--name={APP_NAME}",
|
||||
"--hidden-import=schedule",
|
||||
"--collect-submodules=schedule",
|
||||
"--hidden-import=pystray",
|
||||
"--hidden-import=PIL",
|
||||
"--collect-submodules=pystray",
|
||||
"--collect-submodules=PIL",
|
||||
f"--icon={icon_abs}" if has_icon else "",
|
||||
f"--add-data={icon_abs}{os.pathsep}." if has_icon else "",
|
||||
MAIN_SCRIPT,
|
||||
]
|
||||
cmd = [x for x in cmd if x]
|
||||
subprocess.check_call(cmd)
|
||||
|
||||
|
||||
def write_version_file() -> str:
|
||||
path = os.path.join("dist", "version.txt")
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(VERSION + "\n")
|
||||
return path
|
||||
|
||||
|
||||
def create_zip(version_file: str) -> None:
|
||||
with zipfile.ZipFile(ZIP_PATH, "w", zipfile.ZIP_DEFLATED) as z:
|
||||
z.write(EXE_PATH, arcname=os.path.basename(EXE_PATH))
|
||||
z.write(version_file, arcname="version.txt")
|
||||
if os.path.exists(ICON_PATH):
|
||||
z.write(ICON_PATH, arcname="icon.ico")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ensure_build_deps()
|
||||
|
||||
for folder in ("build", "dist"):
|
||||
if os.path.exists(folder):
|
||||
shutil.rmtree(folder)
|
||||
|
||||
run_build()
|
||||
vfile = write_version_file()
|
||||
create_zip(vfile)
|
||||
print(f"[OK] Built: {EXE_PATH}")
|
||||
print(f"[OK] Archive: {ZIP_PATH}")
|
||||
Reference in New Issue
Block a user