From 8c6325cc8eacd5b463ffa16e9e804cde80928282 Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Mon, 6 Feb 2023 14:29:02 +0800 Subject: [PATCH 1/7] code_style.py: Apply exclusions to the file list This commit rename `--files` options to `--subset` and it means to check a subset of the files known to git. Signed-off-by: Pengyu Lv --- scripts/code_style.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index dd8305faf6..4a5fb68c1b 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -174,22 +174,19 @@ def main() -> int: parser.add_argument('-f', '--fix', action='store_true', help=('modify source files to fix the code style ' '(default: print diff, do not modify files)')) - # --files is almost useless: it only matters if there are no files - # ('code_style.py' without arguments checks all files known to Git, - # 'code_style.py --files' does nothing). In particular, - # 'code_style.py --fix --files ...' is intended as a stable ("porcelain") - # way to restyle a possibly empty set of files. - parser.add_argument('--files', action='store_true', - help='only check the specified files (default with non-option arguments)') + parser.add_argument('--subset', action='store_true', + help=('check a subset of the files known to git ' + '(default: empty FILE means full set)')) parser.add_argument('operands', nargs='*', metavar='FILE', - help='files to check (if none: check files that are known to git)') + help='files to check') args = parser.parse_args() - if args.files or args.operands: - src_files = args.operands - else: - src_files = get_src_files() + all_src_files = get_src_files() + src_files = args.operands if args.operands else all_src_files + if args.subset: + # We are to check a subset of the default list + src_files = [f for f in args.operands if f in all_src_files] if args.fix: # Fix mode From acbeb7fa303338d1dadce9b3add3c6470b9a5bb1 Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Mon, 6 Feb 2023 14:27:30 +0800 Subject: [PATCH 2/7] code_style.py: Add helpers to print warning and skipped files Signed-off-by: Pengyu Lv --- scripts/code_style.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scripts/code_style.py b/scripts/code_style.py index 4a5fb68c1b..61b1ab0e6b 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -33,6 +33,17 @@ CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh" def print_err(*args): print("Error: ", *args, file=sys.stderr) +def print_warn(*args): + print("Warn:", *args, file=sys.stderr) + +# Print the file names that will be skipped and the help message +def print_skip(files_to_skip): + print() + print(*files_to_skip, sep=", SKIP\n", end=", SKIP\n") + print_warn("The listed files will be skipped because\n" + "they are not included in the default list.") + print() + # Match FILENAME(s) in "check SCRIPT (FILENAME...)" CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)", re.ASCII) @@ -187,6 +198,9 @@ def main() -> int: if args.subset: # We are to check a subset of the default list src_files = [f for f in args.operands if f in all_src_files] + skip_src_files = [f for f in args.operands if f not in src_files] + if skip_src_files: + print_skip(skip_src_files) if args.fix: # Fix mode From a4e1eece3d097667f7ed132c679e400927d7c2a3 Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Fri, 10 Feb 2023 10:55:29 +0800 Subject: [PATCH 3/7] print skipped file names to stdout Signed-off-by: Pengyu Lv --- scripts/code_style.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 61b1ab0e6b..85008bec1c 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -33,15 +33,12 @@ CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh" def print_err(*args): print("Error: ", *args, file=sys.stderr) -def print_warn(*args): - print("Warn:", *args, file=sys.stderr) - # Print the file names that will be skipped and the help message def print_skip(files_to_skip): print() print(*files_to_skip, sep=", SKIP\n", end=", SKIP\n") - print_warn("The listed files will be skipped because\n" - "they are not included in the default list.") + print("Warn: The listed files will be skipped because\n" + "they are not included in the default list.") print() # Match FILENAME(s) in "check SCRIPT (FILENAME...)" From b10cf0dd3980a435a8f1d424fd24260748aadcad Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Fri, 10 Feb 2023 11:06:36 +0800 Subject: [PATCH 4/7] adjust help message Signed-off-by: Pengyu Lv --- scripts/code_style.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 85008bec1c..65c9cccfb3 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -184,9 +184,10 @@ def main() -> int: '(default: print diff, do not modify files)')) parser.add_argument('--subset', action='store_true', help=('check a subset of the files known to git ' - '(default: empty FILE means full set)')) + '(default: check all files passed as arguments, ' + 'known to git or not)')) parser.add_argument('operands', nargs='*', metavar='FILE', - help='files to check') + help='files to check (if none: check files that are known to git)') args = parser.parse_args() From e19b51bc533194c7447e47e15d75441450e93408 Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Tue, 14 Feb 2023 10:29:53 +0800 Subject: [PATCH 5/7] Improve readability Signed-off-by: Pengyu Lv --- scripts/code_style.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 65c9cccfb3..eaf1f6b88f 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -191,12 +191,12 @@ def main() -> int: args = parser.parse_args() - all_src_files = get_src_files() - src_files = args.operands if args.operands else all_src_files + covered = frozenset(get_src_files()) + src_files = args.operands if args.operands else covered if args.subset: # We are to check a subset of the default list - src_files = [f for f in args.operands if f in all_src_files] - skip_src_files = [f for f in args.operands if f not in src_files] + src_files = [f for f in args.operands if f in covered] + skip_src_files = [f for f in args.operands if f not in covered] if skip_src_files: print_skip(skip_src_files) From c36743f4e076974076212094de451ad7b51e0988 Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Wed, 15 Feb 2023 10:20:40 +0800 Subject: [PATCH 6/7] Only check files known to git Signed-off-by: Pengyu Lv --- scripts/code_style.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index eaf1f6b88f..e40a20cfc3 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -37,8 +37,8 @@ def print_err(*args): def print_skip(files_to_skip): print() print(*files_to_skip, sep=", SKIP\n", end=", SKIP\n") - print("Warn: The listed files will be skipped because\n" - "they are not included in the default list.") + print("Warning: The listed files will be skipped because\n" + "they are not known to git.") print() # Match FILENAME(s) in "check SCRIPT (FILENAME...)" @@ -182,23 +182,27 @@ def main() -> int: parser.add_argument('-f', '--fix', action='store_true', help=('modify source files to fix the code style ' '(default: print diff, do not modify files)')) + # --subset is almost useless: it only matters if there are no files + # ('code_style.py' without arguments checks all files known to Git, + # 'code_style.py --subset' does nothing). In particular, + # 'code_style.py --fix --subset ...' is intended as a stable ("porcelain") + # way to restyle a possibly empty set of files. parser.add_argument('--subset', action='store_true', - help=('check a subset of the files known to git ' - '(default: check all files passed as arguments, ' - 'known to git or not)')) + help='only check the specified files (default with non-option arguments)') parser.add_argument('operands', nargs='*', metavar='FILE', - help='files to check (if none: check files that are known to git)') + help='files to check (files MUST be known to git, if none: check all)') args = parser.parse_args() covered = frozenset(get_src_files()) - src_files = args.operands if args.operands else covered - if args.subset: - # We are to check a subset of the default list + # We only check files that are known to git + if args.subset or args.operands: src_files = [f for f in args.operands if f in covered] skip_src_files = [f for f in args.operands if f not in covered] if skip_src_files: print_skip(skip_src_files) + else: + src_files = covered if args.fix: # Fix mode From 10f41444a0d4c8d665d98200ffeaf07e3d47abfe Mon Sep 17 00:00:00 2001 From: Pengyu Lv Date: Wed, 15 Feb 2023 16:58:09 +0800 Subject: [PATCH 7/7] Fix CI failure Signed-off-by: Pengyu Lv --- 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 e40a20cfc3..c31fb2949f 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -202,7 +202,7 @@ def main() -> int: if skip_src_files: print_skip(skip_src_files) else: - src_files = covered + src_files = list(covered) if args.fix: # Fix mode