27
27
GITHUB_TOKEN ,
28
28
GITHUB_SHA ,
29
29
API_HEADERS ,
30
+ IS_ON_RUNNER ,
30
31
log_response_msg ,
31
32
range_of_changed_lines ,
32
33
assemble_version_exec ,
@@ -314,10 +315,12 @@ def filter_out_non_source_files(
314
315
Globals .FILES = files
315
316
else :
316
317
cast (Dict [str , Any ], Globals .FILES )["files" ] = files
317
- if not os .getenv ("CI" ): # if not executed on a github runner
318
- with open (".changed_files.json" , "w" , encoding = "utf-8" ) as temp :
319
- # dump altered json of changed files
320
- json .dump (Globals .FILES , temp , indent = 2 )
318
+ if not IS_ON_RUNNER : # if not executed on a github runner
319
+ # dump altered json of changed files
320
+ Path (".changed_files.json" ).write_text (
321
+ json .dumps (Globals .FILES , indent = 2 ),
322
+ encoding = "utf-8" ,
323
+ )
321
324
else :
322
325
logger .info ("No source files need checking!" )
323
326
return False
@@ -344,8 +347,7 @@ def verify_files_are_present() -> None:
344
347
Globals .response_buffer = requests .get (file ["raw_url" ])
345
348
# retain the repo's original structure
346
349
os .makedirs (os .path .split (file_name )[0 ], exist_ok = True )
347
- with open (file_name , "w" , encoding = "utf-8" ) as temp :
348
- temp .write (Globals .response_buffer .text )
350
+ Path (file_name ).write_text (Globals .response_buffer .text , encoding = "utf-8" )
349
351
350
352
351
353
def list_source_files (
@@ -424,8 +426,8 @@ def run_clang_tidy(
424
426
"""
425
427
if checks == "-*" : # if all checks are disabled, then clang-tidy is skipped
426
428
# clear the clang-tidy output file and exit function
427
- with open ("clang_tidy_report.txt" , "wb" ) as f_out :
428
- return
429
+ Path ("clang_tidy_report.txt" ). write_bytes ( b"" )
430
+ return
429
431
filename = filename .replace ("/" , os .sep )
430
432
cmds = [
431
433
assemble_version_exec ("clang-tidy" , version ),
@@ -446,12 +448,11 @@ def run_clang_tidy(
446
448
logger .info ("line_filter = %s" , json .dumps ([line_ranges ]))
447
449
cmds .append (f"--line-filter={ json .dumps ([line_ranges ])} " )
448
450
cmds .append (filename )
449
- with open ( "clang_tidy_output. yml" , "wb" ):
450
- pass # clear yml file's content before running clang-tidy
451
+ # clear yml file's content before running clang-tidy
452
+ Path ( "clang_tidy_output. yml" ). write_bytes ( b"" )
451
453
logger .info ('Running "%s"' , " " .join (cmds ))
452
454
results = subprocess .run (cmds , capture_output = True )
453
- with open ("clang_tidy_report.txt" , "wb" ) as f_out :
454
- f_out .write (results .stdout )
455
+ Path ("clang_tidy_report.txt" ).write_bytes (results .stdout )
455
456
logger .debug ("Output from clang-tidy:\n %s" , results .stdout .decode ())
456
457
if os .path .getsize ("clang_tidy_output.yml" ):
457
458
parse_tidy_suggestions_yml () # get clang-tidy fixes from yml
@@ -480,8 +481,8 @@ def run_clang_format(
480
481
diff info.
481
482
"""
482
483
if not style : # if `style` == ""
483
- with open ("clang_format_output.xml" , "wb" ):
484
- return # clear any previous output and exit
484
+ Path ("clang_format_output.xml" ). write_bytes ( b"" )
485
+ return # clear any previous output and exit
485
486
cmds = [
486
487
assemble_version_exec ("clang-format" , version ),
487
488
f"-style={ style } " ,
@@ -494,8 +495,7 @@ def run_clang_format(
494
495
cmds .append (filename .replace ("/" , os .sep ))
495
496
logger .info ('Running "%s"' , " " .join (cmds ))
496
497
results = subprocess .run (cmds , capture_output = True )
497
- with open ("clang_format_output.xml" , "wb" ) as f_out :
498
- f_out .write (results .stdout )
498
+ Path ("clang_format_output.xml" ).write_bytes (results .stdout )
499
499
if results .returncode :
500
500
logger .debug (
501
501
"%s raised the following error(s):\n %s" , cmds [0 ], results .stderr .decode ()
@@ -867,8 +867,8 @@ def main():
867
867
os .chdir (args .repo_root )
868
868
869
869
if GITHUB_EVENT_PATH :
870
- # load event's json info about the workflow run
871
- Globals .EVENT_PAYLOAD = json .load (
870
+ # load event's json info about the workflow run
871
+ Globals .EVENT_PAYLOAD = json .loads (
872
872
Path (GITHUB_EVENT_PATH ).read_text (encoding = "utf-8" )
873
873
)
874
874
if logger .getEffectiveLevel () <= logging .DEBUG :
0 commit comments