1010EXECUTABLE_NAME = 'RainingKeysPython'
1111MAIN_SCRIPT = 'main.py'
1212
13- def read_config ():
14- """
15- Reads debug_mode from config.ini or config.cfg.
16- Returns boolean: True (Debug), False (Release).
17- Defaults to False if not specified.
18- """
19- config = configparser .ConfigParser ()
20-
21- # Try to read config files
22- found_files = config .read (CONFIG_FILES )
23- if not found_files :
24- print (f"Warning: No config file found ({ '/' .join (CONFIG_FILES )} ). Defaulting to Release mode." )
25- return False
26-
27- # Check for debug_mode in [General] section (or others if widely used)
28- # We prioritize [General] > [Debug] > global search if needed, but [General] is standard.
29- if config .has_section ('General' ):
30- if config .has_option ('General' , 'debug_mode' ):
31- return config .getboolean ('General' , 'debug_mode' )
32-
33- # Fallback: check if it's in a [Debug] section
34- if config .has_section ('Debug' ):
35- if config .has_option ('Debug' , 'debug_mode' ):
36- return config .getboolean ('Debug' , 'debug_mode' )
37-
38- print ("Info: 'debug_mode' not found in config. Defaulting to Release mode." )
39- return False
13+
4014
4115def clean_directories ():
4216 """Removes build and dist directories."""
@@ -53,6 +27,7 @@ def build(debug_mode):
5327 cmd = [
5428 'pyinstaller' ,
5529 '--onedir' ,
30+ '--noconfirm' ,
5631 '--clean' ,
5732 '--name' , EXECUTABLE_NAME ,
5833 MAIN_SCRIPT
@@ -111,32 +86,74 @@ def create_zip(debug_mode):
11186 shutil .make_archive (zip_name , 'zip' , root_dir = OUTPUT_DIR , base_dir = EXECUTABLE_NAME )
11287 print (f"Zip created: { zip_name } .zip" )
11388
114- def main ():
115- # 1. Clean previous builds
116- clean_directories ()
117-
118- # 2. Determine mode
119- debug_mode = read_config ()
120-
121- # 3. Build
89+ def update_config_debug_mode (debug_mode ):
90+ """Updates config.ini to match the current build mode."""
91+ # We read the config to preserve other settings, but force debug_mode
92+ config = configparser .ConfigParser ()
93+ config .read (CONFIG_FILES )
94+
95+ # Ensure sections exist
96+ if not config .has_section ('General' ):
97+ if config .has_section ('Debug' ):
98+ # Legacy support if user uses [Debug]
99+ config .set ('Debug' , 'debug_mode' , str (debug_mode ))
100+ else :
101+ config .add_section ('General' )
102+ config .set ('General' , 'debug_mode' , str (debug_mode ))
103+ else :
104+ config .set ('General' , 'debug_mode' , str (debug_mode ))
105+
106+ # Write back
107+ # We pick the first available config file to write to, usually config.ini
108+ target_cfg = CONFIG_FILES [0 ]
109+ with open (target_cfg , 'w' ) as f :
110+ config .write (f )
111+
112+ def run_build_cycle (debug_mode ):
113+ print (f"\n >>> Starting { 'DEBUG' if debug_mode else 'RELEASE' } Build Cycle <<<" )
114+
115+ # Update config file so the built executable reads the correct mode at runtime
116+ # AND so that the copy_config_to_dist puts the correct config in the dist folder
117+ update_config_debug_mode (debug_mode )
118+
119+ # Build
122120 try :
123121 build (debug_mode )
124122 except subprocess .CalledProcessError as e :
125123 print (f"Error during build: { e } " )
126124 sys .exit (1 )
125+
126+ # Copy Config
127+ copy_config_to_dist ()
128+
129+ # Zip
130+ create_zip (debug_mode )
131+
132+ def main ():
133+ # 1. Clean previous builds once at the start
134+ clean_directories ()
135+
136+ # 2. Check dependencies
137+ try :
138+ subprocess .call (['pyinstaller' , '--version' ], stdout = subprocess .DEVNULL )
127139 except FileNotFoundError :
128140 print ("Error: 'pyinstaller' not found. Please install it via 'pip install pyinstaller'." )
129141 sys .exit (1 )
130142
131- # 4. Copy config
132- copy_config_to_dist ()
133-
134- # 5. Zip
135- create_zip (debug_mode )
136-
137- print ("\n Build and packaging complete!" )
138- print (f"Output folder: { os .path .join (OUTPUT_DIR , EXECUTABLE_NAME )} " )
139- print (f"Zip file: { EXECUTABLE_NAME + ('-debug' if debug_mode else '' )} .zip" )
143+ # 3. Release Build (Normal)
144+ run_build_cycle (debug_mode = False )
145+
146+ # 4. Debug Build
147+ # We clean build/ between runs to ensure clean state, but NOT dist/ (so we keep the zips)
148+ if os .path .exists (BUILD_DIR ):
149+ shutil .rmtree (BUILD_DIR )
150+
151+ run_build_cycle (debug_mode = True )
152+
153+ print ("\n All builds complete!" )
154+ print (f"Artifacts located in project root:" )
155+ print (f" - { EXECUTABLE_NAME } .zip" )
156+ print (f" - { EXECUTABLE_NAME } -debug.zip" )
140157
141158if __name__ == "__main__" :
142159 main ()
0 commit comments