Skip to content

Commit afa06ee

Browse files
authored
Refactor file handling in build.py to use pathlib for improved readability (#6450)
1 parent 9da0035 commit afa06ee

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

.automation/build.py

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from shutil import copyfile, which
1515
from typing import Any
1616
from urllib import parse as parse_urllib
17+
from pathlib import Path
1718

1819
import git
1920
import jsonschema
@@ -242,9 +243,8 @@ def generate_flavor(flavor, flavor_info):
242243
color: "green"
243244
"""
244245
main_action_yml = "action.yml"
245-
with open(main_action_yml, "w", encoding="utf-8") as file:
246-
file.write(action_yml)
247-
logging.info(f"Updated {main_action_yml}")
246+
Path(main_action_yml).write_text(action_yml, encoding="utf-8")
247+
logging.info(f"Updated {main_action_yml}")
248248
else:
249249
# Flavor json
250250
flavor_file = f"{FLAVORS_DIR}/{flavor}/flavor.json"
@@ -306,9 +306,8 @@ def generate_flavor(flavor, flavor_info):
306306
color: "green"
307307
"""
308308
flavor_action_yml = f"{FLAVORS_DIR}/{flavor}/action.yml"
309-
with open(flavor_action_yml, "w", encoding="utf-8") as file:
310-
file.write(action_yml)
311-
logging.info(f"Updated {flavor_action_yml}")
309+
Path(flavor_action_yml).write_text(action_yml, encoding="utf-8")
310+
logging.info(f"Updated {flavor_action_yml}")
312311
extra_lines = []
313312
if CUSTOM_FLAVOR is True:
314313
current_date_time_iso = datetime.now().isoformat()
@@ -832,9 +831,7 @@ def generate_linter_dockerfiles():
832831
logging.info(f"Updated {linters_matrix_file}")
833832

834833
# Write MD file
835-
file = open(f"{REPO_HOME}/docs/standalone-linters.md", "w", encoding="utf-8")
836-
file.write(linters_md + "\n")
837-
file.close()
834+
Path(f"{REPO_HOME}/docs/standalone-linters.md").write_text(linters_md + "\n", encoding="utf-8")
838835

839836

840837
# Automatically generate a test class for each linter class
@@ -1166,9 +1163,8 @@ def generate_descriptor_documentation(descriptor):
11661163
descriptor_md += ["", "### Installation", ""]
11671164
descriptor_md += get_install_md(descriptor)
11681165
# Write MD file
1169-
file = open(f"{REPO_HOME}/docs/descriptors/{lang_lower}.md", "w", encoding="utf-8")
1170-
file.write("\n".join(descriptor_md) + "\n")
1171-
file.close()
1166+
file = Path(f"{REPO_HOME}/docs/descriptors/{lang_lower}.md")
1167+
file.write_text("\n".join(descriptor_md) + "\n", encoding="utf-8")
11721168
logging.info("Updated " + file.name)
11731169

11741170

@@ -1233,9 +1229,7 @@ def generate_flavor_documentation(flavor_id, flavor, linters_tables_md):
12331229
flavor_doc_md += filtered_table_md
12341230
# Write MD file
12351231
flavor_doc_file = f"{REPO_HOME}/docs/flavors/{flavor_id}.md"
1236-
file = open(flavor_doc_file, "w", encoding="utf-8")
1237-
file.write("\n".join(flavor_doc_md) + "\n")
1238-
file.close()
1232+
Path(flavor_doc_file).write_text("\n".join(flavor_doc_md) + "\n", encoding="utf-8")
12391233
logging.info("Updated " + flavor_doc_file)
12401234

12411235

@@ -1992,13 +1986,8 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
19921986
linter_doc_md += ["```"]
19931987

19941988
# Write md file
1995-
file = open(
1996-
f"{REPO_HOME}/docs/descriptors/{lang_lower}_{linter_name_lower}.md",
1997-
"w",
1998-
encoding="utf-8",
1999-
)
2000-
file.write("\n".join(linter_doc_md) + "\n")
2001-
file.close()
1989+
file = Path(f"{REPO_HOME}/docs/descriptors/{lang_lower}_{linter_name_lower}.md")
1990+
file.write_text("\n".join(linter_doc_md) + "\n", encoding="utf-8")
20021991
logging.info("Updated " + file.name)
20031992
linters_tables_md += [""]
20041993
return linters_tables_md
@@ -2473,8 +2462,7 @@ def get_arg_variable_value(package_version):
24732462

24742463
def replace_in_file(file_path, start, end, content, add_new_line=True):
24752464
# Read in the file
2476-
with open(file_path, "r", encoding="utf-8") as file:
2477-
file_content = file.read()
2465+
file_content = Path(file_path).read_text(encoding="utf-8")
24782466
# Detect markdown headers if in replacement
24792467
header_content = None
24802468
header_matches = re.findall(
@@ -2513,8 +2501,8 @@ def replace_in_file(file_path, start, end, content, add_new_line=True):
25132501
else:
25142502
file_content = header_content + "\n" + file_content
25152503
# Write the file out again
2516-
with open(file_path, "w", encoding="utf-8") as file:
2517-
file.write(file_content)
2504+
file = Path(file_path)
2505+
file.write_text(file_content, encoding="utf-8")
25182506
logging.info("Updated " + file.name + " between " + start + " and " + end)
25192507

25202508

@@ -2570,8 +2558,7 @@ def copy_md_file(source_file, target_file):
25702558

25712559
def move_to_file(file_path, start, end, target_file, keep_in_source=False):
25722560
# Read in the file
2573-
with open(file_path, "r", encoding="utf-8") as file:
2574-
file_content = file.read()
2561+
file_content = Path(file_path).read_text(encoding="utf-8")
25752562
# Replace the target string
25762563
replacement_content = ""
25772564
replacement = f"{start}\n{replacement_content}\n{end}"
@@ -2584,9 +2571,8 @@ def move_to_file(file_path, start, end, target_file, keep_in_source=False):
25842571
if keep_in_source is False:
25852572
file_content = re.sub(regex, replacement, file_content, 1, re.DOTALL)
25862573
# Write the file out again
2587-
with open(file_path, "w", encoding="utf-8") as file:
2588-
file.write(file_content)
2589-
logging.info("Updated " + file.name + " between " + start + " and " + end)
2574+
Path(file_path).write_text(file_content, encoding="utf-8")
2575+
logging.info("Updated " + file_path + " between " + start + " and " + end)
25902576
if "<!-- install-" in start or "<!-- config-" in start:
25912577
bracket_content = (
25922578
bracket_content.replace("####", "#TWO#")
@@ -2624,8 +2610,7 @@ def replace_full_url_links(target_file, full_url__base, shorten_url=""):
26242610

26252611

26262612
def replace_anchors_by_links(file_path, moves):
2627-
with open(file_path, "r", encoding="utf-8") as file:
2628-
file_content = file.read()
2613+
file_content = Path(file_path).read_text(encoding="utf-8")
26292614
file_content_new = file_content
26302615
for move in moves:
26312616
file_content_new = file_content_new.replace(f"(#{move})", f"({move}.md)")
@@ -2645,8 +2630,7 @@ def replace_anchors_by_links(file_path, moves):
26452630
]:
26462631
file_content_new = file_content_new.replace(f"(#{pair[0]})", f"({pair[1]})")
26472632
if file_content_new != file_content:
2648-
with open(file_path, "w", encoding="utf-8") as file:
2649-
file.write(file_content_new)
2633+
Path(file_path).write_text(file_content_new, encoding="utf-8")
26502634
logging.info(f"Updated links in {file_path}")
26512635

26522636

@@ -3268,8 +3252,9 @@ def generate_documentation_all_users():
32683252
f"(https://github.com/{repo_full}){{target=_blank}}",
32693253
]
32703254
# pylint: enable=no-member
3271-
with open(f"{REPO_HOME}/docs/all_users.md", "w", encoding="utf-8") as file:
3272-
file.write("\n".join(linter_doc_md) + "\n")
3255+
Path(f"{REPO_HOME}/docs/all_users.md").write_text(
3256+
"\n".join(linter_doc_md) + "\n", encoding="utf-8"
3257+
)
32733258
logging.info(f"Generated {REPO_HOME}/docs/all_users.md")
32743259

32753260

@@ -3505,8 +3490,7 @@ def generate_version():
35053490
# Update changelog
35063491
if UPDATE_CHANGELOG is True:
35073492
changelog_file = f"{REPO_HOME}/CHANGELOG.md"
3508-
with open(changelog_file, "r", encoding="utf-8") as md_file:
3509-
changelog_content = md_file.read()
3493+
changelog_content = Path(changelog_file).read_text(encoding="utf-8")
35103494
changelog_content = changelog_content.replace(
35113495
"<!-- linter-versions-end -->", ""
35123496
)
@@ -3521,8 +3505,7 @@ def generate_version():
35213505
changelog_content = changelog_content.replace(
35223506
"<!-- unreleased-content-marker -->", "\n".join(new_release_lines)
35233507
)
3524-
with open(changelog_file, "w", encoding="utf-8") as file:
3525-
file.write(changelog_content)
3508+
Path(changelog_file).write_text(changelog_content, encoding="utf-8")
35263509

35273510
# git add , commit & tag
35283511
repo = git.Repo(os.getcwd())
@@ -3597,9 +3580,8 @@ def generate_custom_flavor():
35973580
)
35983581
shutil.rmtree(dockerfile_tmp_dir, ignore_errors=True)
35993582
# Display dockerfile content in log
3600-
with open(dockerfile, "r", encoding="utf-8") as f:
3601-
dockerfile_content = f.read()
3602-
logging.info(f"Generated custom flavor dockerfile:\n\n{dockerfile_content}\n")
3583+
dockerfile_content = Path(dockerfile).read_text(encoding="utf-8")
3584+
logging.info(f"Generated custom flavor dockerfile:\n\n{dockerfile_content}\n")
36033585
return dockerfile
36043586

36053587

0 commit comments

Comments
 (0)