-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.py
204 lines (179 loc) · 7.1 KB
/
Build.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Pylib
import re
import os
import sys
import uuid
import shutil
import pprint
import py_compile
import subprocess as subP
# prep
pprint = pprint.PrettyPrinter(indent=4)
# Other funcs
def should_ignore(path, ignore_list):
"""Checks if a path should be ignored based on the provided ignore patterns.
Args:
path (str): The path to check.
ignore_list (List[str]): The list of ignore patterns.
Returns:
bool: True if the path should be ignored, False otherwise.
"""
for pattern in ignore_list:
if re.search(pattern, path):
return True
return False
def copy_with_ignore(src, dst, ignore_list):
"""Recursively copies files from src to dst, ignoring files that match the ignore patterns.
Args:
src: Source directory path.
dst: Destination directory path.
ignore_list: List of glob patterns to ignore when copying.
"""
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copy_with_ignore(s, d, ignore_list)
else:
if not should_ignore(s, ignore_list):
shutil.copy2(s, d)
def move_folders(src_dir, dest_dir):
"""Moves all subdirectories from a source directory to a destination directory.
Args:
src_dir (str): The source directory path.
dest_dir (str): The destination directory path.
"""
# Check if destination directory exists, if not create it
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# List all directories in the source directory
for dir_name in os.listdir(src_dir):
full_dir_name = os.path.join(src_dir, dir_name)
# Check if it's a directory and not the destination directory
if os.path.isdir(full_dir_name) and full_dir_name != dest_dir:
# Move the directory to the destination directory
shutil.move(full_dir_name, dest_dir)
def Compile_python(path):
"""Compiles python"""
# Iterate over all files in the directory
for root, dirs, files in os.walk(path):
for filename in files:
# Check if the file is a Python script
if filename.endswith(".py"):
# Compile the Python script to a .pyc file
try:
py_compile.compile(os.path.join(root, filename), optimize=2)
except py_compile.PyCompileError as e:
print(f"Failed to compile {filename}: {e}")
continue
# Define the source file and destination file
src = os.path.join(
root, "__pycache__", filename[:-3] + f".cpython-{sys.version_info.major}{sys.version_info.minor}.opt-2.pyc"
)
dst = os.path.join(root, filename[:-3] + ".pyc")
# Check if the .pyc file exists
if not os.path.exists(src):
print(src)
print(f"Failed to find .pyc file for {filename}")
continue
# Move the .pyc file
try:
shutil.move(src, dst)
except shutil.Error as e:
print(f"Failed to move .pyc file for {filename}: {e}")
continue
# Delete the original .py file
try:
os.remove(os.path.join(root, filename))
except OSError as e:
print(f"Failed to delete .py file for {filename}: {e}")
# Build funcs
def Build_Main():
# Init
RUN_Path_dict = {
"Model info": ("python", "Make_model_info.py"),
"Ruff (Check + Format)": ("Ruff_Auto.cmd"),
"Sync code": ("Update_Code.cmd"),
"Gen Requirements": ("Create_requirements.cmd"),
}
Sync_Utils = False
# Starting...
print("<Build Main> --> Starting...")
# Proc Auto
print("<Build Main> <Proc Auto - Init> --> Run dict:")
for key, value in RUN_Path_dict.items():
print(f" -- Key: {key}, Value: {value}")
print("<Build Main> <Proc Auto - Start> --> Run dict:")
for process in RUN_Path_dict:
print(f"<Build Main> <Proc Auto> --> Running [{process}]...")
# Run
subP.run(RUN_Path_dict[process], shell=False)
# End
print(f"<Build Main> <Proc Auto> --> [{process}] Done.")
# Sync Utils
if Sync_Utils:
print("<Build Main> <Sync Utils> --> Starting...")
Main_utils_path = "Utils"
utils_destinations = ["Interface\\GUI\\Data\\Utils", "Interface\\CLI\\Data\\Utils"]
for utils_destination in utils_destinations:
print(f"<Build Main> <Sync Utils> --> copying utils from {Main_utils_path} to {utils_destination}...")
shutil.copytree(Main_utils_path, utils_destination, dirs_exist_ok=True)
print("<Build Main> <Sync Utils> --> Done.")
# Copy CLI / GUI Build
print("<Build Main> <(CLI / GUI) Build> --> Starting...")
Ignore_list = [
r".*\.h5$",
r".*\.pyc$",
r".*\.tmp$",
r".*\\*logs\\.*$",
r".*\\__pycache__$",
r".*\\GUI_Build\.py$",
r".*\\GUI_DEV\.cmd$",
r".*\\Data\\Gen_lib\.cmd$",
r".*\\model_info\.json$",
]
# CLI
CLI_dir = "Interface\\CLI"
CLI_Build_folder = "Build\\Github\\Releases\\Other\\Interface\\CLI"
CLI_Archive_dir = "Build\\Github\\Releases\\Other\\Interface\\CLI\\Archive"
CLI_Build_dir = f"Build\\Github\\Releases\\Other\\Interface\\CLI\\{uuid.uuid4()}"
print("<Build Main> <(CLI / GUI) Build> --> CLI Build...")
print(f" -- Build dir: {CLI_Build_dir}")
print(" -- Archiving old builds...")
move_folders(CLI_Build_folder, CLI_Archive_dir)
print(" -- Copying new build...")
copy_with_ignore(CLI_dir, CLI_Build_dir, Ignore_list)
print("<Build Main> <(CLI / GUI) Build> --> CLI Build Done.")
# GUI
GUI_dir = "Interface\\GUI"
GUI_Build_folder = "Build\\Github\\Releases\\Other\\Interface\\GUI"
GUI_Archive_dir = "Build\\Github\\Releases\\Other\\Interface\\GUI\\Archive"
GUI_Build_dir = f"Build\\Github\\Releases\\Other\\Interface\\GUI\\{uuid.uuid4()}"
print("<Build Main> <(CLI / GUI) Build> --> GUI Build...")
print(f" -- Build dir: {GUI_Build_dir}")
print(" -- Archiving old builds...")
move_folders(GUI_Build_folder, GUI_Archive_dir)
print(" -- Copying new build...")
copy_with_ignore(GUI_dir, GUI_Build_dir, Ignore_list)
if input("<Build Main> <(CLI / GUI) Build> --> [Beta] Compile GUI? (y/n): ") == "y":
print("<Build Main> <(CLI / GUI) Build> --> Compiling GUI...")
Compile_python(GUI_Build_dir)
print("<Build Main> <(CLI / GUI) Build> --> Compiling GUI Done.")
print("<Build Main> <(CLI / GUI) Build> --> GUI Build Done.")
print("<Build Main> <(CLI / GUI) Build> --> Done.")
# End.
print("<Build Main> --> End.")
# Main
def main():
print("Starting the build... \n")
Build_Main()
if __name__ == "__main__":
try:
main()
except Exception:
print("\nError: ")
raise
else:
print("\nBuild complete.")