8
8
import contextlib
9
9
import datetime
10
10
import fnmatch
11
- import glob
12
11
import io
13
12
import itertools
14
13
import json
24
23
import tempfile
25
24
import textwrap
26
25
import threading
27
- import traceback
28
26
import zipfile
29
27
from operator import itemgetter
30
28
from pathlib import Path
40
38
from github .WorkflowRun import WorkflowRun
41
39
42
40
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" )
47
45
MAX_ANNOTATIONS = 10
48
46
49
47
@@ -161,7 +159,7 @@ def get_auth_from_arguments(args: argparse.Namespace) -> Auth.Auth:
161
159
def build_clang_tidy_warnings (
162
160
base_invocation : List ,
163
161
env : dict ,
164
- tmpdir : str ,
162
+ tmpdir : Path ,
165
163
task_queue : queue .Queue ,
166
164
lock : threading .Lock ,
167
165
failed_files : List ,
@@ -184,7 +182,6 @@ def build_clang_tidy_warnings(
184
182
invocation , stdout = subprocess .PIPE , stderr = subprocess .PIPE , env = env
185
183
)
186
184
output , err = proc .communicate ()
187
- end = datetime .datetime .now ()
188
185
189
186
if proc .returncode != 0 :
190
187
if proc .returncode < 0 :
@@ -246,14 +243,15 @@ def config_file_or_checks(
246
243
return "--config"
247
244
248
245
249
- def merge_replacement_files (tmpdir : str , mergefile : str ):
246
+ def merge_replacement_files (tmpdir : Path , mergefile : Path ):
250
247
"""Merge all replacement files in a directory into a single file"""
251
248
# The fixes suggested by clang-tidy >= 4.0.0 are given under
252
249
# the top level key 'Diagnostics' in the output yaml files
253
250
mergekey = "Diagnostics"
254
251
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 )
257
255
if not content :
258
256
continue # Skip empty files.
259
257
merged .extend (content .get (mergekey , []))
@@ -264,15 +262,15 @@ def merge_replacement_files(tmpdir: str, mergefile: str):
264
262
# is actually never used inside clang-apply-replacements,
265
263
# so we set it to '' here.
266
264
output = {"MainSourceFile" : "" , mergekey : merged }
267
- with open (mergefile , "w" ) as out :
265
+ with mergefile . open ("w" ) as out :
268
266
yaml .safe_dump (output , out )
269
267
270
268
271
269
def load_clang_tidy_warnings (fixes_file ) -> Dict :
272
270
"""Read clang-tidy warnings from fixes_file. Can be produced by build_clang_tidy_warnings"""
273
271
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 )
276
274
except FileNotFoundError :
277
275
return {}
278
276
@@ -672,7 +670,7 @@ def fix_absolute_paths(build_compile_commands, base_dir):
672
670
"""
673
671
674
672
basedir = pathlib .Path (base_dir ).resolve ()
675
- newbasedir = pathlib . Path (). resolve ()
673
+ newbasedir = Path . cwd ()
676
674
677
675
if basedir == newbasedir :
678
676
return
@@ -992,8 +990,7 @@ def create_review(
992
990
username = pull_request .get_pr_author () or "your name here"
993
991
994
992
# 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 ())
997
994
env = dict (os .environ , USER = username )
998
995
config = config_file_or_checks (clang_tidy_binary , clang_tidy_checks , config_file )
999
996
base_invocation = [
@@ -1038,8 +1035,6 @@ def create_review(
1038
1035
1039
1036
# Wait for all threads to be done.
1040
1037
task_queue .join ()
1041
- if len (failed_files ):
1042
- return_code = 1
1043
1038
1044
1039
except KeyboardInterrupt :
1045
1040
# This is a sad hack. Unfortunately subprocess goes
@@ -1050,7 +1045,7 @@ def create_review(
1050
1045
real_duration = datetime .datetime .now () - start
1051
1046
1052
1047
# Read and parse the CLANG_TIDY_FIXES file
1053
- print ("Writing fixes to " + FIXES_FILE + " ..." )
1048
+ print (f "Writing fixes to { FIXES_FILE } ..." )
1054
1049
merge_replacement_files (export_fixes_dir , FIXES_FILE )
1055
1050
shutil .rmtree (export_fixes_dir )
1056
1051
clang_tidy_warnings = load_clang_tidy_warnings (FIXES_FILE )
@@ -1115,9 +1110,13 @@ def download_artifacts(pull: PullRequest, workflow_id: int):
1115
1110
filenames = data .namelist ()
1116
1111
1117
1112
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
1119
1119
)
1120
- review = json .loads (data .read (REVIEW_FILE )) if REVIEW_FILE in filenames else None
1121
1120
return metadata , review
1122
1121
1123
1122
0 commit comments