utils/check-package: cleanup line reading
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/4 utils/check-package: cleanup line reading
Cleanup the implementation for reading lines by having files processed in context managers and utilizing the iterable file object for line reading (instead of needing to call readlines()).
--- utils/check-package | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
utils/check-package +12 -10
@@ -229,16 +229,18 @@ def check_file_using_lib(fname): nwarnings += warn lastline = "" - for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()): - nlines += 1 - for name, cf in objects: - if cf.disable.search(lastline): - continue - warn, fail = print_warnings(cf.check_line(lineno + 1, text), name in xfail) - if fail > 0: - failed.add(name) - nwarnings += warn - lastline = text + with open(fname, "r", errors="surrogateescape") as f: + for lineno, text in enumerate(f): + nlines += 1 + for name, cf in objects: + if cf.disable.search(lastline): + continue + line_sts = cf.check_line(fstate, lineno + 1, text) + warn, fail = print_warnings(line_sts, name in xfail) + if fail > 0: + failed.add(name) + nwarnings += warn + lastline = text for name, cf in objects: warn, fail = print_warnings(cf.after(), name in xfail)
Signature · 6 lines
2.40.1.windows.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
Hello James,
Quoted 24 lines · On Sat, 29 Apr 2023 14:12:02 -0400 James Knight <[email protected]> wrote:
Cleanup the implementation for reading lines by having files processed in context managers and utilizing the iterable file object for line reading (instead of needing to call
readlines()).Signed-off-by James Knight <[email protected]>--- utils/check-package | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)utils/check-package +0
-7@@ -229,16 +229,18 @@ def check_file_using_lib(fname): nwarnings += warn lastline = "" - for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()): - nlines += 1 - for name, cf in objects: - if cf.disable.search(lastline): - continue - warn, fail = print_warnings(cf.check_line(lineno + 1, text), name in xfail) - if fail > 0:
utils/check-package def check_file_using_lib(fname): · line 238
Quoted 10 lines
utils/check-package +7
-3- failed.add(name) - nwarnings += warn - lastline = text + with open(fname, "r", errors="surrogateescape") as f: + for lineno, text in enumerate(f): + nlines += 1 + for name, cf in objects: + if cf.disable.search(lastline): + continue + line_sts = cf.check_line(fstate, lineno + 1, text)
This variable "fstate" doesn't exist as of PATCH 1/4. I wanted to apply only this patch for now, as I'm not sure about PATCH 2/4 to PATCH 4/4, but PATCH 1/4 on its own doesn't work. Could you have a look?
Thanks!
Thomas
Signature · 7 lines
Thomas Petazzoni, co-owner and CEO, Bootlin Embedded Linux and Kernel engineering and training https://bootlin.com _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
PATCH 2/4 utils/check-package: support a file-state instance for library types
In reply to a message not in the archiveThe following adds support to the checkpackage library to have a file-specific state instance that can be changed across multiple check_line calls. This allows a given library type to provide hints across different lines (if needed).
This is in preparation for a future commit to provide support for linter suppression that is configured in the files being processed.
--- utils/check-package | 8 ++++++++ utils/checkpackagelib/base.py | 2 +- utils/checkpackagelib/lib.py | 10 +++++----- utils/checkpackagelib/lib_config.py | 10 +++++----- utils/checkpackagelib/lib_hash.py | 6 +++--- utils/checkpackagelib/lib_mk.py | 22 +++++++++++----------- utils/checkpackagelib/lib_patch.py | 6 +++--- utils/checkpackagelib/lib_sysv.py | 4 ++-- utils/checkpackagelib/test_util.py | 4 ++-- 9 files changed, 40 insertions(+), 32 deletions(-)
utils/check-package +8 -0
@@ -220,6 +220,12 @@ def check_file_using_lib(fname): print("{}: would run: {}".format(fname, functions_to_run)) return nwarnings, nlines + # Check if there is a state tracker we can create for this library type, + # and then forward to each `check_line` invoked + fstate = None + if hasattr(lib, 'State') and inspect.isclass(lib.State): + fstate = lib.State() + objects = [[c[0], c[1](fname, flags.manual_url)] for c in internal_functions] for name, cf in objects:
@@ -231,6 +237,8 @@ def check_file_using_lib(fname): lastline = "" with open(fname, "r", errors="surrogateescape") as f: for lineno, text in enumerate(f): + if fstate: + fstate.process(text) nlines += 1 for name, cf in objects: if cf.disable.search(lastline):
utils/checkpackagelib/base.py +1 -1
@@ -11,7 +11,7 @@ class _CheckFunction(object): def before(self): pass - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): pass def after(self):
utils/checkpackagelib/lib.py +5 -5
@@ -7,7 +7,7 @@ class ConsecutiveEmptyLines(_CheckFunction): def before(self): self.lastline = "non empty" - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if text.strip() == "" == self.lastline.strip(): return ["{}:{}: consecutive empty lines" .format(self.filename, lineno)]
@@ -19,7 +19,7 @@ class EmptyLastLine(_CheckFunction): self.lastlineno = 0 self.lastline = "non empty" - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): self.lastlineno = lineno self.lastline = text
@@ -34,7 +34,7 @@ class NewlineAtEof(_CheckFunction): self.lastlineno = 0 self.lastline = "\n" - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): self.lastlineno = lineno self.lastline = text
@@ -46,7 +46,7 @@ class NewlineAtEof(_CheckFunction): class TrailingSpace(_CheckFunction): - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): line = text.rstrip("\r\n") if line != line.rstrip(): return ["{}:{}: line contains trailing whitespace"
@@ -61,7 +61,7 @@ class Utf8Characters(_CheckFunction): except TypeError: return False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if not self.is_ascii(text): return ["{}:{}: line contains UTF-8 characters" .format(self.filename, lineno),
utils/checkpackagelib/lib_config.py +5 -5
@@ -37,7 +37,7 @@ class AttributesOrder(_CheckFunction): def before(self): self.state = 0 - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_or_comment(text) or _part_of_help_text(text): return
@@ -87,7 +87,7 @@ class CommentsMenusPackagesOrder(_CheckFunction): self.level = self.get_level() self.initialize_package_level_elements(text) - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): # We only want to force sorting for the top-level menus if self.filename not in ["fs/Config.in", "package/Config.in",
@@ -159,7 +159,7 @@ class HelpText(_CheckFunction): def before(self): self.help_text = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_or_comment(text): return
@@ -194,7 +194,7 @@ class Indent(_CheckFunction): def before(self): self.backslash = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_or_comment(text) or _part_of_help_text(text): self.backslash = False return
@@ -244,7 +244,7 @@ class RedefinedConfig(_CheckFunction): self.configs = {} self.conditional = [] - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_or_comment(text) or _part_of_help_text(text): return
utils/checkpackagelib/lib_hash.py +3 -3
@@ -18,7 +18,7 @@ def _empty_line_or_comment(text): class HashNumberOfFields(_CheckFunction): - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_line_or_comment(text): return
@@ -33,7 +33,7 @@ class HashType(_CheckFunction): len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64, "sha384": 96, "sha512": 128} - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_line_or_comment(text): return
@@ -55,7 +55,7 @@ class HashType(_CheckFunction): class HashSpaces(_CheckFunction): - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if _empty_line_or_comment(text): return
utils/checkpackagelib/lib_mk.py +11 -11
@@ -24,7 +24,7 @@ end_conditional = ["endif"] class DoNotInstallToHostdirUsr(_CheckFunction): INSTALL_TO_HOSTDIR_USR = re.compile(r"^[^#].*\$\(HOST_DIR\)/usr") - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.INSTALL_TO_HOSTDIR_USR.match(text.rstrip()): return ["{}:{}: install files to $(HOST_DIR)/ instead of $(HOST_DIR)/usr/" .format(self.filename, lineno),
@@ -34,7 +34,7 @@ class DoNotInstallToHostdirUsr(_CheckFunction): class Ifdef(_CheckFunction): IFDEF = re.compile(r"^\s*(else\s+|)(ifdef|ifndef)\s") - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): m = self.IFDEF.search(text) if m is None: return
@@ -62,7 +62,7 @@ class Indent(_CheckFunction): self.backslash = False self.makefile_target = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.START_DEFINE.search(text): self.define = True return
@@ -126,7 +126,7 @@ class OverriddenVariable(_CheckFunction): self.unconditionally_set = [] self.conditionally_set = [] - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.START_CONDITIONAL.search(text): self.conditional += 1 return
@@ -178,7 +178,7 @@ class PackageHeader(_CheckFunction): def before(self): self.skip = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.skip or lineno > 6: return
@@ -214,7 +214,7 @@ class RemoveDefaultPackageSourceVariable(_CheckFunction): r"^{}_SOURCE\s*=\s*{}-\$\({}_VERSION\)\.tar\.gz" .format(package_upper, package, package_upper)) - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.FIND_SOURCE.search(text): if self.package in self.packages_that_may_contain_default_source:
@@ -229,7 +229,7 @@ class RemoveDefaultPackageSourceVariable(_CheckFunction): class SpaceBeforeBackslash(_CheckFunction): TAB_OR_MULTIPLE_SPACES_BEFORE_BACKSLASH = re.compile(r"^.*( |\t ?)\\$") - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.TAB_OR_MULTIPLE_SPACES_BEFORE_BACKSLASH.match(text.rstrip()): return ["{}:{}: use only one space before backslash" .format(self.filename, lineno),
@@ -242,7 +242,7 @@ class TrailingBackslash(_CheckFunction): def before(self): self.backslash = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): last_line_ends_in_backslash = self.backslash # calculate for next line
@@ -294,7 +294,7 @@ class TypoInPackageVariable(_CheckFunction): r"^{}_PROVIDES\s*(\+|)=\s*(.*)".format(package)) self.virtual = [] - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): m = self.VARIABLE.search(text) if m is None: return
@@ -333,7 +333,7 @@ class UselessFlag(_CheckFunction): def before(self): self.conditional = 0 - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.START_CONDITIONAL.search(text): self.conditional += 1 return
@@ -361,7 +361,7 @@ class UselessFlag(_CheckFunction): class VariableWithBraces(_CheckFunction): VARIABLE_WITH_BRACES = re.compile(r"^[^#].*[^$]\${\w+}") - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.VARIABLE_WITH_BRACES.match(text.rstrip()): return ["{}:{}: use $() to delimit variables, not ${{}}" .format(self.filename, lineno),
utils/checkpackagelib/lib_patch.py +3 -3
@@ -29,7 +29,7 @@ class NumberedSubject(_CheckFunction): self.lineno = 0 self.text = None - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if text.startswith("diff --git"): self.git_patch = True return
@@ -50,7 +50,7 @@ class Sob(_CheckFunction): def before(self): self.found = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.found: return if self.SOB_ENTRY.search(text):
@@ -68,7 +68,7 @@ class Upstream(_CheckFunction): def before(self): self.found = False - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.found: return if self.UPSTREAM_ENTRY.search(text):
utils/checkpackagelib/lib_sysv.py +2 -2
@@ -13,7 +13,7 @@ from checkpackagelib.tool import Shellcheck # noqa: F401 class Indent(_CheckFunction): INDENTED_WITH_SPACES = re.compile(r"^[\t]* ") - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): if self.INDENTED_WITH_SPACES.search(text.rstrip()): return ["{}:{}: should be indented with tabs ({}#adding-packages-start-script)" .format(self.filename, lineno, self.url_to_manual),
@@ -36,7 +36,7 @@ class Variables(_CheckFunction): def before(self): self.name = None - def check_line(self, lineno, text): + def check_line(self, state, lineno, text): name_found = self.DAEMON_VAR.search(text.rstrip()) if name_found: if self.name:
utils/checkpackagelib/test_util.py +2 -2
@@ -1,8 +1,8 @@ -def check_file(check_function, filename, string): +def check_file(check_function, filename, string, state=None): obj = check_function(filename, 'url') result = [] result.append(obj.before()) for i, line in enumerate(string.splitlines(True)): - result.append(obj.check_line(i + 1, line)) + result.append(obj.check_line(state, i + 1, line)) result.append(obj.after()) return [r for r in result if r is not None]
Signature · 6 lines
2.40.1.windows.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
Re: [Buildroot] [PATCH 2/4] utils/check-package: support a file-state instance for library types
Hello,
Sorry the long delay.
Quoted 10 lines · James Knight
from: James Knight <[email protected]>
date: Sat, Apr 29 02:12 PM -04:00 2023
to: [email protected]
cc: James Knight <[email protected]>, Ricardo Martincoski <[email protected]>
subject: [Buildroot] [PATCH 2/4] utils/check-package: support a file-state instance for library typesThe following adds support to the checkpackage library to have a file-specific state instance that can be changed across multiple
check_linecalls. This allows a given library type to provide hints across different lines (if needed).
This is a great idea.
Quoted 35 lines
This is in preparation for a future commit to provide support for linter suppression that is configured in the files being processed.
Signed-off-by James Knight <[email protected]>--- utils/check-package | 8 ++++++++ utils/checkpackagelib/base.py | 2 +- utils/checkpackagelib/lib.py | 10 +++++----- utils/checkpackagelib/lib_config.py | 10 +++++----- utils/checkpackagelib/lib_hash.py | 6 +++--- utils/checkpackagelib/lib_mk.py | 22 +++++++++++----------- utils/checkpackagelib/lib_patch.py | 6 +++--- utils/checkpackagelib/lib_sysv.py | 4 ++-- utils/checkpackagelib/test_util.py | 4 ++-- 9 files changed, 40 insertions(+), 32 deletions(-)utils/check-package +6
-0@@ -220,6 +220,12 @@ def check_file_using_lib(fname): print("{}: would run: {}".format(fname, functions_to_run)) return nwarnings, nlines + # Check if there is a state tracker we can create for this library type, + # and then forward to each `check_line` invoked + fstate = None + if hasattr(lib, 'State') and inspect.isclass(lib.State): + fstate = lib.State() + objects = [[c[0], c[1](fname, flags.manual_url)] for c in internal_functions] for name, cf in objects:
utils/check-package def check_file_using_lib(fname): · line 244
Quoted 8 lines
utils/check-package +2
-0@@ -231,6 +237,8 @@ def check_file_using_lib(fname): lastline = "" with open(fname, "r", errors="surrogateescape") as f: for lineno, text in enumerate(f): + if fstate: + fstate.process(text) nlines += 1 for name, cf in objects: if cf.disable.search(lastline):
This line of code (that is already there) made me wonder if we can factor-out this code to the base class and add the new implementation there.
The advantages of this alternative are:
- avoid adding code to the main check-package script that currently is not covered by unit tests
- avoid the need to change all users of the base class
I will send shortly a patch with this factor-out.
Regards,
Ricardo
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
PATCH 3/4 utils/check-package: support ignore-indent flag for configurations
In reply to a message not in the archiveThis commit provides the ability for configuration scripts to hint at ignoring linter checks for expected indentations. By adding the line to a configuration file:
# noqa: ignore-indent
Calls to check-package will ignore any custom indentation the configuration script may have. This can be useful for "container" configurations which include packages or other sub-packages, without having to add explicit bypasses into the checkpackagelib library.
This avoids maintaining special file lists inside lib_config.py, as well as allows br2-external tree to easily utilize the linter script without having to worry about indentation on container-like configuration definitions.
--- utils/checkpackagelib/lib_config.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
utils/checkpackagelib/lib_config.py +18 -0
@@ -224,6 +224,8 @@ class Indent(_CheckFunction): text] elif entry in entries_that_should_not_be_indented: if not text.startswith(entry): + if state and 'ignore-indent' in state.noqa_flags: + return # four Config.in files have a special but legitimate indentation rule if self.filename in ["package/Config.in", "package/Config.in.host",
@@ -271,3 +273,19 @@ class RedefinedConfig(_CheckFunction): .format(self.filename, lineno, config, previous_line), text] self.configs[key] = lineno + + +class State: + def __init__(self): + self.noqa_flags = set() + + def process(self, line): + if not _empty_or_comment(line): + return + + # if `noqa:` token is detected, consider trailing options as noqa + # tokens assigned on the file + if "noqa:" in line: + flags = line.partition("noqa:")[2].lower().split() + if flags: + self.noqa_flags.update(flags)
Signature · 6 lines
2.40.1.windows.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
Re: [Buildroot] [PATCH 3/4] utils/check-package: support ignore-indent flag for configurations
Hello,
Sorry the long delay.
Quoted 11 lines · James Knight
from: James Knight <[email protected]>
date: Sat, Apr 29 02:12 PM -04:00 2023
to: [email protected]
cc: James Knight <[email protected]>, Ricardo Martincoski <[email protected]>
subject: [Buildroot] [PATCH 3/4] utils/check-package: support ignore-indent flag for configurationsThis commit provides the ability for configuration scripts to hint at ignoring linter checks for expected indentations. By adding the line to a configuration file:
# noqa: ignore-indent
There is already support for comments in the form
# check-package Indent to not run a CheckFunction named Indent for the next source code line
It seems to me that using 'check-package' in the special comment does make sense because someone reading the source file, for instance a Config.in file, can immediately know it is something related to the check-package script, while a 'noqa' would need to be documented in the manual.
Also, using 'check-package' in the comment would keep consistency with code that already exists.
Finally, it seems to me more future-proof to keep the naming space of all flags/options inside each CheckFunction, so I would suggest a comment in the form:
# check-package Indent_ignore-menu-indent-below
that sets the flag named 'ignore-menu-indent-below' for the CheckFunction named 'Indent' in the library that parses the type of file that has the special comment, in the case of Config.in, lib_config.py.
Regards,
Ricardo
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
PATCH 4/4 utils/check-package: utilize ignore-indent flag for special configs
In reply to a message not in the archiveApplies the ignore-indent flag to multiple configuration files, allowing these special files to removed from an internal list managed inside checkpackagelib/lib_config.py.
--- package/Config.in | 2 ++ package/Config.in.host | 2 ++ package/kodi/Config.in | 2 ++ package/x11r7/Config.in | 2 ++ utils/checkpackagelib/lib_config.py | 6 ------ 5 files changed, 8 insertions(+), 6 deletions(-)
package/Config.in +2 -0
@@ -1,3 +1,5 @@ +# noqa: ignore-indent + menu "Target packages" source "package/busybox/Config.in"
package/Config.in.host +2 -0
@@ -1,3 +1,5 @@ +# noqa: ignore-indent + menu "Host utilities" source "package/abootimg/Config.in.host"
package/kodi/Config.in +2 -0
@@ -1,3 +1,5 @@ +# noqa: ignore-indent + config BR2_PACKAGE_KODI_ARCH_SUPPORTS bool default y if BR2_PACKAGE_FFMPEG_ARCH_SUPPORTS
package/x11r7/Config.in +2 -0
@@ -1,3 +1,5 @@ +# noqa: ignore-indent + menuconfig BR2_PACKAGE_XORG7 bool "X.org X Window System" depends on BR2_USE_WCHAR
utils/checkpackagelib/lib_config.py +0 -6
@@ -226,12 +226,6 @@ class Indent(_CheckFunction): if not text.startswith(entry): if state and 'ignore-indent' in state.noqa_flags: return - # four Config.in files have a special but legitimate indentation rule - if self.filename in ["package/Config.in", - "package/Config.in.host", - "package/kodi/Config.in", - "package/x11r7/Config.in"]: - return return ["{}:{}: should not be indented" .format(self.filename, lineno), text]
Signature · 6 lines
2.40.1.windows.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot