From f7b7bf7a5e4e137da732e77e973c01034db55518 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 16 Jan 2023 18:20:08 +0000 Subject: [PATCH 1/9] Remove provisional notice on code style script Since code style is now enforced, the notice is wrong. Remove it to avoid confusion. Signed-off-by: David Horstmann --- scripts/code_style.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 3958e870f..463a349ff 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -1,9 +1,5 @@ #!/usr/bin/env python3 """Check or fix the code style by running Uncrustify. - -Note: The code style enforced by this script is not yet introduced to -Mbed TLS. At present this script will only be used to prepare for a future -change of code style. """ # Copyright The Mbed TLS Contributors # SPDX-License-Identifier: Apache-2.0 From a27d8722288747679db58f1256ad79290db73644 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 16 Jan 2023 18:28:21 +0000 Subject: [PATCH 2/9] Document that the script must be run from the root Signed-off-by: David Horstmann --- scripts/code_style.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/code_style.py b/scripts/code_style.py index 463a349ff..e26d42e9c 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 """Check or fix the code style by running Uncrustify. + +This script must be run from the root of a Git work tree containing Mbed TLS. """ # Copyright The Mbed TLS Contributors # SPDX-License-Identifier: Apache-2.0 From 71ccdaacd1d52f169e18fe5a489d5c0ae9603dee Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 16 Jan 2023 18:30:00 +0000 Subject: [PATCH 3/9] Remove unnecessary "Line length options" heading Signed-off-by: David Horstmann --- .uncrustify.cfg | 2 -- 1 file changed, 2 deletions(-) diff --git a/.uncrustify.cfg b/.uncrustify.cfg index 7ce090533..92b8ce9cd 100644 --- a/.uncrustify.cfg +++ b/.uncrustify.cfg @@ -19,8 +19,6 @@ # limitations under the License. -# Line length options - # Wrap lines at 100 characters code_width = 100 From 64827e4611723023598b9afe1fb6ba90da835f67 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 16 Jan 2023 18:32:56 +0000 Subject: [PATCH 4/9] Change print to print_err for an error message Signed-off-by: David Horstmann --- scripts/code_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index e26d42e9c..6222dbad4 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -158,7 +158,7 @@ def fix_style(src_file_list: List[str]) -> int: # Guard against future changes that cause the codebase to require # more passes. if not check_style_is_correct(src_file_list): - print("Code style still incorrect after second run of Uncrustify.") + print_err("Code style still incorrect after second run of Uncrustify.") return 1 else: return 0 From 3febe1511446e79132f1696a13d789a12e11f70e Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 16 Jan 2023 18:53:01 +0000 Subject: [PATCH 5/9] Remove overly verbose output on success Signed-off-by: David Horstmann --- scripts/code_style.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 6222dbad4..5d05fd8a9 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -122,8 +122,6 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: print("File changed - diff:", file=STDOUT_UTF8) print(str(result.stdout, "utf-8"), file=STDOUT_UTF8) style_correct = False - else: - print(src_file + " - OK.", file=STDOUT_UTF8) # Tidy up artifact os.remove(src_file + ".uncrustify") From 6d02f0cfa04d600abdb7e709d07727f2653f6c40 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 24 Jan 2023 16:56:18 +0000 Subject: [PATCH 6/9] Add basic output on success Whilst it is true that "silence is golden", no output at all could be disconcerting and it makes searching in a CI log more difficult. Add a simple status message that says "Checked N files, style ok". Signed-off-by: David Horstmann --- scripts/code_style.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/code_style.py b/scripts/code_style.py index 5d05fd8a9..f333c643c 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -199,6 +199,8 @@ def main() -> int: else: # Check mode if check_style_is_correct(src_files): + print("Checked {} files, style ok.".format(len(src_files)), + file=STDOUT_UTF8) return 0 else: return 1 From 5682e8026685279ae1c7922f94f437f2097dc353 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 24 Jan 2023 18:08:49 +0000 Subject: [PATCH 7/9] 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 f333c643c..d9c61a5af 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") From 6956cb58ac240c06093c75809bcbf1b3d6630844 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 24 Jan 2023 18:36:41 +0000 Subject: [PATCH 8/9] Don't wrap stdout and stderr in UTF-8 wrapper This is no longer needed as we only print ASCII text directly Signed-off-by: David Horstmann --- scripts/code_style.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index d9c61a5af..d3ae624ec 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -18,7 +18,6 @@ This script must be run from the root of a Git work tree containing Mbed TLS. # See the License for the specific language governing permissions and # limitations under the License. import argparse -import io import os import re import subprocess @@ -29,12 +28,10 @@ UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" CONFIG_FILE = ".uncrustify.cfg" UNCRUSTIFY_EXE = "uncrustify" UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] -STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') -STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh" def print_err(*args): - print("Error: ", *args, file=STDERR_UTF8) + print("Error: ", *args, file=sys.stderr) # Match FILENAME(s) in "check SCRIPT (FILENAME...)" CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)", @@ -67,8 +64,8 @@ def get_src_files() -> List[str]: "tests/suites/*.function", "scripts/data_files/*.fmt"] - result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \ - stderr=STDERR_UTF8, check=False) + result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, + check=False) if result.returncode != 0: print_err("git ls-files returned: " + str(result.returncode)) @@ -118,7 +115,7 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: cp = subprocess.run(diff_cmd, check=False) if cp.returncode == 1: - print(src_file + " changed - code style is incorrect.", file=STDOUT_UTF8) + print(src_file + " changed - code style is incorrect.") style_correct = False elif cp.returncode != 0: raise subprocess.CalledProcessError(cp.returncode, cp.args, @@ -136,8 +133,7 @@ def fix_style_single_pass(src_file_list: List[str]) -> bool: code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"] for src_file in src_file_list: uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file] - result = subprocess.run(uncrustify_cmd, check=False, \ - stdout=STDOUT_UTF8, stderr=STDERR_UTF8) + result = subprocess.run(uncrustify_cmd, check=False) if result.returncode != 0: print_err("Uncrustify with file returned: " + \ str(result.returncode) + " correcting file " + \ @@ -169,9 +165,9 @@ def main() -> int: uncrustify_version = get_uncrustify_version().strip() if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: print("Warning: Using unsupported Uncrustify version '" + - uncrustify_version + "'", file=STDOUT_UTF8) + uncrustify_version + "'") print("Note: The only supported version is " + - UNCRUSTIFY_SUPPORTED_VERSION, file=STDOUT_UTF8) + UNCRUSTIFY_SUPPORTED_VERSION) parser = argparse.ArgumentParser() parser.add_argument('-f', '--fix', action='store_true', @@ -200,8 +196,7 @@ def main() -> int: else: # Check mode if check_style_is_correct(src_files): - print("Checked {} files, style ok.".format(len(src_files)), - file=STDOUT_UTF8) + print("Checked {} files, style ok.".format(len(src_files))) return 0 else: return 1 From 04aaa45ee1f0fb9b995620543d0f5757daa40975 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 25 Jan 2023 11:39:04 +0000 Subject: [PATCH 9/9] Remove unnecessary '\' linebreak characters Signed-off-by: David Horstmann --- scripts/code_style.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index d3ae624ec..dd8305faf 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -85,8 +85,9 @@ def get_uncrustify_version() -> str: """ Get the version string from Uncrustify """ - result = subprocess.run([UNCRUSTIFY_EXE, "--version"], \ - stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) + result = subprocess.run([UNCRUSTIFY_EXE, "--version"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + check=False) if result.returncode != 0: print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8")) return "" @@ -101,11 +102,11 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: style_correct = True for src_file in src_file_list: uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file] - result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, \ - stderr=subprocess.PIPE, check=False) + result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, check=False) if result.returncode != 0: - print_err("Uncrustify returned " + str(result.returncode) + \ - " correcting file " + src_file) + print_err("Uncrustify returned " + str(result.returncode) + + " correcting file " + src_file) return False # Uncrustify makes changes to the code and places the result in a new @@ -135,9 +136,9 @@ def fix_style_single_pass(src_file_list: List[str]) -> bool: uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file] result = subprocess.run(uncrustify_cmd, check=False) if result.returncode != 0: - print_err("Uncrustify with file returned: " + \ - str(result.returncode) + " correcting file " + \ - src_file) + print_err("Uncrustify with file returned: " + + str(result.returncode) + " correcting file " + + src_file) return False return True