From 5682e8026685279ae1c7922f94f437f2097dc353 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 24 Jan 2023 18:08:49 +0000 Subject: [PATCH] Output diff without capturing it Instead of capturing the output of diff and printing it, let diff do its own outputting and se the return code to decide what to do. This also means that the conversion of stdout to UTF-8 is not necessary, as the reason it was needed was for printing diffs of files with UTF-8 characters in them. Signed-off-by: David Horstmann --- scripts/code_style.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index f333c643c0..d9c61a5afd 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -115,13 +115,14 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: # file with the extension ".uncrustify". To get the changes (if any) # simply diff the 2 files. diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"] - result = subprocess.run(diff_cmd, stdout=subprocess.PIPE, \ - stderr=STDERR_UTF8, check=False) - if len(result.stdout) > 0: - print(src_file + " - Incorrect code style.", file=STDOUT_UTF8) - print("File changed - diff:", file=STDOUT_UTF8) - print(str(result.stdout, "utf-8"), file=STDOUT_UTF8) + cp = subprocess.run(diff_cmd, check=False) + + if cp.returncode == 1: + print(src_file + " changed - code style is incorrect.", file=STDOUT_UTF8) style_correct = False + elif cp.returncode != 0: + raise subprocess.CalledProcessError(cp.returncode, cp.args, + cp.stdout, cp.stderr) # Tidy up artifact os.remove(src_file + ".uncrustify")