From 7c8d598c0f406e9c87cd8f4db45a62de23a747e6 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:37:16 -0500 Subject: [PATCH] Andrewi/264 output folder prep (#281) * prepared output directory in cangen Fixes #264 * formatted with ruff * changed order of cleaning output dir * refactored cleanig output dir * reverting last commit * deleted folder contents instead of folder itself * formated with ruff * changed prepare output dir --- scripts/cangen/cangen/can_generator.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 548bd5ae1..2b8348ed3 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -187,16 +187,6 @@ def _generate_from_jinja2_template( template = env.get_template(template_path) rendered_code = template.render(**context_dict) - # Write the rendered code to a file - output_dir = os.path.dirname(output_path) - os.makedirs(output_dir, exist_ok=True) - - # Create a git ignore for everything in the generated path. Ignore everything. - gitignore_path = os.path.join(output_dir, ".gitignore") - if not os.path.exists(gitignore_path): - with open(gitignore_path, "w") as f: - f.write("*") - with open(output_path, "w") as output_file: output_file.write(rendered_code) @@ -247,13 +237,22 @@ def generate_code(bus: Bus, config: Config): logger.info("Code generation complete") +def _prepare_output_directory(output_dir): + """Deletes previously generated files and creates a gitignore for the directory""" + if os.path.exists(output_dir): + shutil.rmtree(output_dir) + os.makedirs(output_dir, exist_ok=True) + + gitignore_path = os.path.join(output_dir, ".gitignore") + with open(gitignore_path, "w") as f: + f.write("*") + + def generate_can_from_dbc(project_folder_name: str): os.chdir(project_folder_name) config = Config.from_yaml("config.yaml") - # Deletes output path folder and files within, before creating new ones - if os.path.exists(config.output_dir): - shutil.rmtree(config.output_dir) + _prepare_output_directory(config.output_dir) for bus in config.busses: generate_code(bus, config)