22
33import filecmp
44import os
5- import shlex
6- import sys
75from pathlib import Path
86from typing import Any
97
10- from pyhelper_utils .shell import run_command
118from rich .console import Console
129from rich .syntax import Syntax
1310from simple_logger .logger import get_logger
@@ -86,9 +83,10 @@ def generate_resource_file_from_dict(
8683 LOGGER .warning (f"Overwriting { _output_file } " )
8784
8885 else :
89- temp_output_file = _output_file .replace (".py" , "_TEMP.py" )
90- LOGGER .warning (f"{ _output_file } already exists, using { temp_output_file } " )
91- _output_file = temp_output_file
86+ if not dry_run :
87+ temp_output_file = _output_file .replace (".py" , "_TEMP.py" )
88+ LOGGER .warning (f"{ _output_file } already exists, using { temp_output_file } " )
89+ _output_file = temp_output_file
9290
9391 if _user_code .strip () or _user_imports .strip ():
9492 output += f"{ _user_imports } { rendered } { _user_code } "
@@ -114,6 +112,7 @@ def class_generator(
114112 add_tests : bool = False ,
115113 called_from_cli : bool = True ,
116114 update_schema_executed : bool = False ,
115+ called_from_test : bool = False ,
117116) -> list [str ]:
118117 """
119118 Generates a class for a given Kind.
@@ -134,55 +133,50 @@ def class_generator(
134133 LOGGER .info (f"Generating class for { kind } " )
135134 kind = kind .lower ()
136135 kind_and_namespaced_mappings = read_resources_mapping_file ().get (kind )
137- if not kind_and_namespaced_mappings :
136+ if not called_from_test and not kind_and_namespaced_mappings :
138137 if called_from_cli :
139138 if update_schema_executed :
140- LOGGER .error (f"{ kind } not found in { RESOURCES_MAPPING_FILE } after update-schema executed." )
141- sys .exit (1 )
142-
143- # Use a loop to handle retries instead of recursion
144- max_retries = 3
145- retry_count = 0
146-
147- while retry_count < max_retries :
148- # Validate user input with a loop
149- while True :
150- run_update_schema = (
151- input (
152- f"{ kind } not found in { RESOURCES_MAPPING_FILE } , Do you want to run --update-schema and retry? [Y/N]: "
153- )
154- .strip ()
155- .lower ()
156- )
157-
158- if run_update_schema in ["y" , "n" ]:
159- break
160- else :
161- print ("Invalid input. Please enter 'Y' or 'N'." )
162-
163- if run_update_schema == "n" :
164- sys .exit (1 )
165-
166- # User chose 'y' - update schema and retry
167- LOGGER .info (f"Updating schema (attempt { retry_count + 1 } /{ max_retries } )..." )
168- update_kind_schema ()
139+ error_msg = f"{ kind } not found in { RESOURCES_MAPPING_FILE } after update-schema executed."
140+ LOGGER .error (error_msg )
141+ raise RuntimeError (error_msg )
142+
143+ run_update_schema = (
144+ input (
145+ f"{ kind } not found in { RESOURCES_MAPPING_FILE } , Do you want to run --update-schema and retry? [Y/N]: "
146+ )
147+ .strip ()
148+ .lower ()
149+ )
169150
170- # Re-read the mapping file to check if kind is now available
171- kind_and_namespaced_mappings = read_resources_mapping_file ().get (kind )
172- if kind_and_namespaced_mappings :
173- # Kind found, break out of retry loop to continue processing
174- break
175- else :
176- retry_count += 1
177- if retry_count < max_retries :
178- LOGGER .warning (
179- f"{ kind } still not found after schema update. Retry { retry_count } /{ max_retries } ."
180- )
181- else :
182- LOGGER .error (
183- f"{ kind } not found in { RESOURCES_MAPPING_FILE } after { max_retries } update-schema attempts."
184- )
185- sys .exit (1 )
151+ if run_update_schema != "y" :
152+ raise RuntimeError (f"User declined to update schema for { kind } " )
153+
154+ # User chose 'y' - update schema and retry
155+ LOGGER .info ("Updating schema" )
156+ try :
157+ update_kind_schema ()
158+ except (RuntimeError , IOError ) as e :
159+ error_msg = f"Failed to update schema: { e } "
160+ LOGGER .error (error_msg )
161+ raise RuntimeError (error_msg ) from e
162+
163+ # Re-read the mapping file to check if kind is now available
164+ kind_and_namespaced_mappings = read_resources_mapping_file (skip_cache = True ).get (kind )
165+ if kind_and_namespaced_mappings :
166+ return class_generator (
167+ kind = kind ,
168+ overwrite = overwrite ,
169+ dry_run = dry_run ,
170+ output_file = output_file ,
171+ output_dir = output_dir ,
172+ add_tests = add_tests ,
173+ called_from_cli = called_from_cli ,
174+ update_schema_executed = True ,
175+ )
176+ else :
177+ error_msg = f"{ kind } not found in { RESOURCES_MAPPING_FILE } after update-schema attempt."
178+ LOGGER .error (error_msg )
179+ raise RuntimeError (error_msg )
186180
187181 else :
188182 LOGGER .error (f"{ kind } not found in { RESOURCES_MAPPING_FILE } , Please run --update-schema." )
@@ -215,29 +209,7 @@ def class_generator(
215209 output_dir = output_dir ,
216210 )
217211
218- if not dry_run :
219- try :
220- rc , stdout , stderr = run_command (
221- command = shlex .split (f"uvx pre-commit run --files { generated_py_file } " ),
222- verify_stderr = False ,
223- check = False ,
224- )
225- # Check if the command failed
226- if not rc :
227- LOGGER .warning (
228- f"Pre-commit hooks failed for { generated_py_file } . "
229- f"This is non-fatal and generation will continue."
230- )
231- if stderr :
232- LOGGER .debug (f"Pre-commit stderr: { stderr } " )
233- if stdout :
234- LOGGER .debug (f"Pre-commit stdout: { stdout } " )
235- except Exception as e :
236- LOGGER .error (
237- f"Failed to run pre-commit hooks for { generated_py_file } : { e } . "
238- f"This is non-fatal and generation will continue."
239- )
240-
212+ if not dry_run and not called_from_test :
241213 if orig_filename != generated_py_file and filecmp .cmp (orig_filename , generated_py_file ):
242214 LOGGER .warning (f"File { orig_filename } was not updated, deleting { generated_py_file } " )
243215 Path .unlink (Path (generated_py_file ))
0 commit comments