-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildScript.py
More file actions
98 lines (78 loc) · 2.82 KB
/
buildScript.py
File metadata and controls
98 lines (78 loc) · 2.82 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
SUPER TIERD RIGHT NOW :<
Basically this is a script to build the EXE using PyInstaller with all the necessary configurations.
cause i dont know much of front end nor have i ever deployed anything so i will just be making a good ol EXE.
'''
import os
import sys
import subprocess
import shutil
# --------- CONFIGURATION ---------
ENTRY_FILE = "pdf_extractor_main.py"
ASSET_FOLDERS = ["assets", "data"]
HIDDEN_IMPORTS = ["PIL", "cv2"]
ONEFILE = True
CONSOLE = True
ICON = "icon.ico"
# --------------------------
def run(cmd):
print(f"\n>>> {cmd}\n")
subprocess.call(cmd, shell=True)
def ensure_pyinstaller():
try:
import PyInstaller
print("PyInstaller already installed.")
except ImportError:
print("Installing PyInstaller...")
run("pip install pyinstaller")
def clean_old_builds():
for folder in ["build", "dist", "__pycache__"]:
if os.path.exists(folder):
print(f"Deleting old folder: {folder}")
shutil.rmtree(folder, ignore_errors=True)
def build_add_data_args():
args = []
for folder in ASSET_FOLDERS:
if os.path.exists(folder):
args.append(f'--add-data "{folder}{os.pathsep}{folder}"')
return " ".join(args)
def build_hidden_imports():
return " ".join([f"--hidden-import {m}" for m in HIDDEN_IMPORTS])
def create_resource_helper():
helper_code = r'''
# Auto-generated helper for PyInstaller bundled paths
import sys, os
def resource_path(path):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, path)
return path
'''
with open("resource_helper.py", "w") as f:
f.write(helper_code)
print("Created resource_helper.py")
def build_exe():
if ICON and not ICON.endswith(".ico"):
print("ERROR: ICON must be a .ico file")
sys.exit(1)
ensure_pyinstaller()
clean_old_builds()
create_resource_helper()
add_data = build_add_data_args()
hidden = build_hidden_imports()
onefile_flag = "--onefile" if ONEFILE else ""
console_flag = "" if CONSOLE else "--noconsole"
icon_flag = f'--icon "{ICON}"' if ICON else ""
cmd = f'pyinstaller {onefile_flag} {console_flag} {icon_flag} {add_data} {hidden} "{ENTRY_FILE}"'
run(cmd)
print("\n====================================")
print("BUILD COMPLETE")
print("Your EXE is inside the dist/ folder.")
print("====================================\n")
if __name__ == "__main__":
if not os.path.exists(ENTRY_FILE):
print(f"ERROR: Entry file {ENTRY_FILE} not found.")
sys.exit(1)
if ICON and not os.path.exists(ICON):
print(f"ERROR: ICON file not found: {ICON}")
sys.exit(1)
build_exe()