Skip to content

Commit d6361d8

Browse files
committed
Use string interpolation
1 parent 12e4ad2 commit d6361d8

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

misc/scripts/library-coverage/compare-files-comment-pr.py

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
def check_file_exists(file):
1818
if not os.path.exists(file):
19-
print("Expected file '" + file + "' doesn't exist.", file=sys.stderr)
19+
print(f"Expected file '{file}' doesn't exist.", file=sys.stderr)
2020
return False
2121
return True
2222

@@ -73,7 +73,7 @@ def get_comment_text(output_file, repo, run_id):
7373
return
7474

7575
comment = ":warning: The head of this PR and the base branch were compared for differences in the framework coverage reports. " + \
76-
"The generated reports are available in the [artifacts of this workflow run](https://github.com/" + repo + "/actions/runs/" + str(run_id) + "). " + \
76+
f"The generated reports are available in the [artifacts of this workflow run](https://github.com/{repo}/actions/runs/{run_id}). " + \
7777
"The differences will be picked up by the nightly job after the PR gets merged. "
7878

7979
if size < 2000:
@@ -83,8 +83,7 @@ def get_comment_text(output_file, repo, run_id):
8383
comment += file.read()
8484
else:
8585
print("There's a large change in the CSV framework coverage reports")
86-
comment += "The differences can be found in the " + \
87-
output_file + " artifact of this job."
86+
comment += f"The differences can be found in the {output_file} artifact of this job."
8887

8988
return comment
9089

@@ -114,12 +113,11 @@ def comment_pr(output_file, repo, run_id):
114113
write_diff_for_run(prev_output_file, repo, prev_run_id)
115114

116115
if filecmp.cmp(output_file, prev_output_file, shallow=False):
117-
print("Previous run " + str(prev_run_id) +
118-
" resulted in the same diff, so not commenting again.")
116+
print(
117+
f"Previous run {prev_run_id} resulted in the same diff, so not commenting again.")
119118
return
120119
else:
121-
print("Diff of previous run " +
122-
str(prev_run_id) + " differs, commenting.")
120+
print(f"Diff of previous run {prev_run_id} differs, commenting.")
123121
except Exception:
124122
# this is not mecessarily a failure, it can also mean that there was no previous run yet.
125123
print("Couldn't generate diff for previous run:", sys.exc_info()[1])
@@ -132,7 +130,7 @@ def comment_pr(output_file, repo, run_id):
132130

133131

134132
def post_comment(comment, repo, pr_number):
135-
print("Posting comment to PR #" + str(pr_number))
133+
print(f"Posting comment to PR #{pr_number}")
136134
utils.subprocess_run(["gh", "pr", "comment", str(pr_number),
137135
"--repo", repo, "--body", comment])
138136

@@ -155,41 +153,33 @@ def compare_folders(folder1, folder2, output_file):
155153
language=lang)
156154

157155
# check if files exist in both folder1 and folder 2
158-
if not check_file_exists(folder1 + "/" + generated_output_rst):
159-
expected_files += "- " + generated_output_rst + \
160-
" doesn't exist in folder " + folder1 + "\n"
161-
if not check_file_exists(folder2 + "/" + generated_output_rst):
162-
expected_files += "- " + generated_output_rst + \
163-
" doesn't exist in folder " + folder2 + "\n"
164-
if not check_file_exists(folder1 + "/" + generated_output_csv):
165-
expected_files += "- " + generated_output_csv + \
166-
" doesn't exist in folder " + folder1 + "\n"
167-
if not check_file_exists(folder2 + "/" + generated_output_csv):
168-
expected_files += "- " + generated_output_csv + \
169-
" doesn't exist in folder " + folder2 + "\n"
156+
if not check_file_exists(f"{folder1}/{generated_output_rst}"):
157+
expected_files += f"- {generated_output_rst} doesn't exist in folder {folder1}\n"
158+
if not check_file_exists(f"{folder2}/{generated_output_rst}"):
159+
expected_files += f"- {generated_output_rst} doesn't exist in folder {folder2}\n"
160+
if not check_file_exists(f"{folder1}/{generated_output_csv}"):
161+
expected_files += f"- {generated_output_csv} doesn't exist in folder {folder1}\n"
162+
if not check_file_exists(f"{folder2}/{generated_output_csv}"):
163+
expected_files += f"- {generated_output_csv} doesn't exist in folder {folder2}\n"
170164

171165
if expected_files != "":
172166
print("Expected files are missing", file=sys.stderr)
173-
return_md += "\n### " + lang + "\n\n#### Expected files are missing for " + \
174-
lang + "\n" + expected_files + "\n"
167+
return_md += f"\n### {lang}\n\n#### Expected files are missing for {lang}\n{expected_files}\n"
175168
continue
176169

177170
# compare contents of files
178171
cmp1 = compare_files_str(
179-
folder1 + "/" + generated_output_rst, folder2 + "/" + generated_output_rst)
172+
f"{folder1}/{generated_output_rst}", f"{folder2}/{generated_output_rst}")
180173
cmp2 = compare_files_str(
181-
folder1 + "/" + generated_output_csv, folder2 + "/" + generated_output_csv)
174+
f"{folder1}/{generated_output_csv}", f"{folder2}/{generated_output_csv}")
182175

183176
if cmp1 != "" or cmp2 != "":
184177
print("Generated file contents are not matching", file=sys.stderr)
185-
return_md += "\n### " + lang + "\n\n#### Generated file changes for " + \
186-
lang + "\n\n"
178+
return_md += f"\n### {lang}\n\n#### Generated file changes for {lang}\n\n"
187179
if cmp1 != "":
188-
return_md += "- Changes to " + generated_output_rst + \
189-
":\n```diff\n" + cmp1 + "```\n\n"
180+
return_md += f"- Changes to {generated_output_rst}:\n```diff\n{cmp1}```\n\n"
190181
if cmp2 != "":
191-
return_md += "- Changes to " + generated_output_csv + \
192-
":\n```diff\n" + cmp2 + "```\n\n"
182+
return_md += f"- Changes to {generated_output_csv}:\n```diff\n{cmp2}```\n\n"
193183

194184
with open(output_file, 'w', newline='') as out:
195185
out.write(return_md)
@@ -201,29 +191,29 @@ def get_previous_run_id(repo, run_id, pr_number):
201191
"""
202192

203193
# Get branch and repo from run:
204-
this_run = utils.subprocess_check_output(["gh", "api", "-X", "GET", "repos/" + repo + "/actions/runs/" + str(
205-
run_id), "--jq", "{ head_branch: .head_branch, head_repository: .head_repository.full_name }"])
194+
this_run = utils.subprocess_check_output(
195+
["gh", "api", "-X", "GET", f"repos/{repo}/actions/runs/{run_id}", "--jq", "{ head_branch: .head_branch, head_repository: .head_repository.full_name }"])
206196

207197
this_run = json.loads(this_run)
208198
pr_branch = this_run["head_branch"]
209199
pr_repo = this_run["head_repository"]
210200

211201
# Get all previous runs that match branch, repo and workflow name:
212-
ids = utils.subprocess_check_output(["gh", "api", "-X", "GET", "repos/" + repo + "/actions/runs", "-f", "event=pull_request", "-f", "status=success", "-f", "name=\"" + artifacts_workflow_name + "\"", "--jq",
213-
"[.workflow_runs.[] | select(.head_branch==\"" + pr_branch + "\" and .head_repository.full_name==\"" + pr_repo + "\") | { created_at: .created_at, run_id: .id}] | sort_by(.created_at) | reverse | [.[].run_id]"])
202+
ids = utils.subprocess_check_output(["gh", "api", "-X", "GET", f"repos/{repo}/actions/runs", "-f", "event=pull_request", "-f", "status=success", "-f", "name=\"" + artifacts_workflow_name + "\"", "--jq",
203+
f"[.workflow_runs.[] | select(.head_branch==\"{pr_branch}\" and .head_repository.full_name==\"{pr_repo}\") | {{ created_at: .created_at, run_id: .id}}] | sort_by(.created_at) | reverse | [.[].run_id]"])
214204

215205
ids = json.loads(ids)
216206
if ids[0] != int(run_id):
217-
raise Exception("Expected to find " + str(run_id) +
218-
" in the list of matching runs.")
207+
raise Exception(
208+
f"Expected to find {run_id} in the list of matching runs.")
219209

220210
for previous_run_id in ids[1:]:
221211
download_artifact(repo, "pr", "prev_run_pr", previous_run_id)
222212

223213
try:
224214
with open("prev_run_pr/NR") as file:
225215
prev_pr_number = int(file.read())
226-
print("PR number: " + str(prev_pr_number))
216+
print(f"PR number: {prev_pr_number}")
227217
finally:
228218
if os.path.isdir("prev_run_pr"):
229219
shutil.rmtree("prev_run_pr")

0 commit comments

Comments
 (0)