Skip to content

Commit

Permalink
class-generator: delete temp resource file if identical to original f…
Browse files Browse the repository at this point in the history
…ile (#2033)

* do not write to temp file is no changes

* do not write to temp file is no changes

* do not write to temp file is no changes

* update log level
  • Loading branch information
rnetser authored Aug 13, 2024
1 parent 8d2d103 commit 5f855a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fcn_exclude_functions =
meta,
tmp_path_factory,
tmpdir_factory,
Path,

enable-extensions =
FCN,
19 changes: 12 additions & 7 deletions class_generator/class_generator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import filecmp
import json
import shlex
import os
import sys
from pathlib import Path

from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Tuple
import click
import re

Expand Down Expand Up @@ -401,15 +403,13 @@ def generate_resource_file_from_dict(
output_file: str = "",
interactive: bool = False,
add_tests: bool = False,
) -> str:
) -> Tuple[str, str]:
rendered = render_jinja_template(
template_dict=resource_dict,
template_dir="class_generator/manifests",
template_name="class_generator_template.j2",
)

temp_output_file: str = ""

formatted_kind_str = convert_camel_case_to_snake_case(string_=resource_dict["KIND"])
if add_tests:
overwrite = True
Expand All @@ -419,6 +419,7 @@ def generate_resource_file_from_dict(
else:
_output_file = os.path.join("ocp_resources", f"{formatted_kind_str}.py")

orig_filename = _output_file
if os.path.exists(_output_file):
if overwrite:
LOGGER.warning(f"Overwriting {_output_file}")
Expand All @@ -432,7 +433,7 @@ def generate_resource_file_from_dict(
sys.exit(1)

else:
temp_output_file = f"{_output_file[:-3]}_TEMP.py"
temp_output_file = _output_file.replace(".py", "_TEMP.py")
LOGGER.warning(f"{_output_file} already exists, using {temp_output_file}")
_output_file = temp_output_file

Expand All @@ -443,7 +444,7 @@ def generate_resource_file_from_dict(
else:
write_and_format_rendered(filepath=_output_file, data=rendered)

return _output_file
return orig_filename, _output_file


def parse_explain(
Expand Down Expand Up @@ -626,7 +627,7 @@ def class_generator(
if not resource_dict:
return ""

generated_py_file = generate_resource_file_from_dict(
orig_filename, generated_py_file = generate_resource_file_from_dict(
resource_dict=resource_dict,
overwrite=overwrite,
dry_run=dry_run,
Expand All @@ -642,6 +643,10 @@ def class_generator(
check=False,
)

if orig_filename != generated_py_file and filecmp.cmp(orig_filename, generated_py_file):
LOGGER.warning(f"File {orig_filename} was not updated, deleting {generated_py_file}")
Path.unlink(Path(generated_py_file))

if debug or add_tests:
LOGGER.info(f"Debug output saved to {output_debug_file_path}")

Expand Down

0 comments on commit 5f855a7

Please sign in to comment.