Skip to content

Commit 3aea09f

Browse files
committed
Use Path in more places
1 parent 30fd704 commit 3aea09f

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

post/clang_tidy_review/clang_tidy_review/__init__.py

+21-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import contextlib
99
import datetime
1010
import fnmatch
11-
import glob
1211
import io
1312
import itertools
1413
import json
@@ -24,7 +23,6 @@
2423
import tempfile
2524
import textwrap
2625
import threading
27-
import traceback
2826
import zipfile
2927
from operator import itemgetter
3028
from pathlib import Path
@@ -40,10 +38,10 @@
4038
from github.WorkflowRun import WorkflowRun
4139

4240
DIFF_HEADER_LINE_LENGTH = 5
43-
FIXES_FILE = "clang_tidy_review.yaml"
44-
METADATA_FILE = "clang-tidy-review-metadata.json"
45-
REVIEW_FILE = "clang-tidy-review-output.json"
46-
PROFILE_DIR = "clang-tidy-review-profile"
41+
FIXES_FILE = Path("clang_tidy_review.yaml")
42+
METADATA_FILE = Path("clang-tidy-review-metadata.json")
43+
REVIEW_FILE = Path("clang-tidy-review-output.json")
44+
PROFILE_DIR = Path("clang-tidy-review-profile")
4745
MAX_ANNOTATIONS = 10
4846

4947

@@ -161,7 +159,7 @@ def get_auth_from_arguments(args: argparse.Namespace) -> Auth.Auth:
161159
def build_clang_tidy_warnings(
162160
base_invocation: List,
163161
env: dict,
164-
tmpdir: str,
162+
tmpdir: Path,
165163
task_queue: queue.Queue,
166164
lock: threading.Lock,
167165
failed_files: List,
@@ -184,7 +182,6 @@ def build_clang_tidy_warnings(
184182
invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
185183
)
186184
output, err = proc.communicate()
187-
end = datetime.datetime.now()
188185

189186
if proc.returncode != 0:
190187
if proc.returncode < 0:
@@ -246,14 +243,15 @@ def config_file_or_checks(
246243
return "--config"
247244

248245

249-
def merge_replacement_files(tmpdir: str, mergefile: str):
246+
def merge_replacement_files(tmpdir: Path, mergefile: Path):
250247
"""Merge all replacement files in a directory into a single file"""
251248
# The fixes suggested by clang-tidy >= 4.0.0 are given under
252249
# the top level key 'Diagnostics' in the output yaml files
253250
mergekey = "Diagnostics"
254251
merged = []
255-
for replacefile in glob.iglob(os.path.join(tmpdir, "*.yaml")):
256-
content = yaml.safe_load(open(replacefile, "r"))
252+
for replacefile in tmpdir.glob("*.yaml"):
253+
with replacefile.open() as f:
254+
content = yaml.safe_load(f)
257255
if not content:
258256
continue # Skip empty files.
259257
merged.extend(content.get(mergekey, []))
@@ -264,15 +262,15 @@ def merge_replacement_files(tmpdir: str, mergefile: str):
264262
# is actually never used inside clang-apply-replacements,
265263
# so we set it to '' here.
266264
output = {"MainSourceFile": "", mergekey: merged}
267-
with open(mergefile, "w") as out:
265+
with mergefile.open("w") as out:
268266
yaml.safe_dump(output, out)
269267

270268

271269
def load_clang_tidy_warnings(fixes_file) -> Dict:
272270
"""Read clang-tidy warnings from fixes_file. Can be produced by build_clang_tidy_warnings"""
273271
try:
274-
with Path(FIXES_FILE).open() as fixes_file:
275-
return yaml.safe_load(fixes_file)
272+
with fixes_file.open() as f:
273+
return yaml.safe_load(f)
276274
except FileNotFoundError:
277275
return {}
278276

@@ -672,7 +670,7 @@ def fix_absolute_paths(build_compile_commands, base_dir):
672670
"""
673671

674672
basedir = pathlib.Path(base_dir).resolve()
675-
newbasedir = pathlib.Path().resolve()
673+
newbasedir = Path.cwd()
676674

677675
if basedir == newbasedir:
678676
return
@@ -992,8 +990,7 @@ def create_review(
992990
username = pull_request.get_pr_author() or "your name here"
993991

994992
# Run clang-tidy with the configured parameters and produce the CLANG_TIDY_FIXES file
995-
return_code = 0
996-
export_fixes_dir = tempfile.mkdtemp()
993+
export_fixes_dir = Path(tempfile.mkdtemp())
997994
env = dict(os.environ, USER=username)
998995
config = config_file_or_checks(clang_tidy_binary, clang_tidy_checks, config_file)
999996
base_invocation = [
@@ -1038,8 +1035,6 @@ def create_review(
10381035

10391036
# Wait for all threads to be done.
10401037
task_queue.join()
1041-
if len(failed_files):
1042-
return_code = 1
10431038

10441039
except KeyboardInterrupt:
10451040
# This is a sad hack. Unfortunately subprocess goes
@@ -1050,7 +1045,7 @@ def create_review(
10501045
real_duration = datetime.datetime.now() - start
10511046

10521047
# Read and parse the CLANG_TIDY_FIXES file
1053-
print("Writing fixes to " + FIXES_FILE + " ...")
1048+
print(f"Writing fixes to {FIXES_FILE} ...")
10541049
merge_replacement_files(export_fixes_dir, FIXES_FILE)
10551050
shutil.rmtree(export_fixes_dir)
10561051
clang_tidy_warnings = load_clang_tidy_warnings(FIXES_FILE)
@@ -1115,9 +1110,13 @@ def download_artifacts(pull: PullRequest, workflow_id: int):
11151110
filenames = data.namelist()
11161111

11171112
metadata = (
1118-
json.loads(data.read(METADATA_FILE)) if METADATA_FILE in filenames else None
1113+
json.loads(data.read(str(METADATA_FILE)))
1114+
if METADATA_FILE in filenames
1115+
else None
1116+
)
1117+
review = (
1118+
json.loads(data.read(str(REVIEW_FILE))) if REVIEW_FILE in filenames else None
11191119
)
1120-
review = json.loads(data.read(REVIEW_FILE)) if REVIEW_FILE in filenames else None
11211120
return metadata, review
11221121

11231122

tests/test_review.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ def test_line_ranges():
238238

239239

240240
def test_load_clang_tidy_warnings():
241-
warnings = ctr.load_clang_tidy_warnings(
242-
str(TEST_DIR / f"src/test_{ctr.FIXES_FILE}")
243-
)
241+
warnings = ctr.load_clang_tidy_warnings(TEST_DIR / f"src/test_{ctr.FIXES_FILE}")
244242

245243
assert sorted(list(warnings.keys())) == ["Diagnostics", "MainSourceFile"]
246244
assert warnings["MainSourceFile"] == "/clang_tidy_review/src/hello.cxx"

0 commit comments

Comments
 (0)