kumquat-buildroot/utils/check-package

221 lines
6.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
# See utils/checkpackagelib/readme.txt before editing this file.
import argparse
import inspect
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
import os
import re
check-package: fix Python3 support This script currently uses "/usr/bin/env python" as shebang but it does not really support Python3. Instead of limiting the script to Python2, fix it to support both versions. So change all imports to absolute imports because Python3 follows PEP328 and dropped implicit relative imports. In order to avoid errors when decoding files with the default 'utf-8' codec, use errors="surrogateescape" when opening files, the docs for open() states: "This is useful for processing files in an unknown encoding.". This argument is not compatible with Python2 open() so import 'six' to use it only when running in Python3. As a consequence the file handler becomes explicit, so use it to close() the file after it got processed. This "surrogateescape" is a simple alternative to the complete solution of opening files with "rb" and changing all functions in the lib*.py files to use bytes objects instead of strings. The only case we can have non-ascii/non-utf-8 files being checked by the script are for patch files when the upstream file to be patched is not ascii or utf-8. There is currently one case in the tree: package/urg/0002-urg-gcc6-fix-narrowing-conversion.patch. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Cc: Arnout Vandecappelle <arnout@mind.be> Reviewed-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Tested-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
2018-08-11 05:48:27 +02:00
import six
import sys
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
import checkpackagelib.base
import checkpackagelib.lib_config
import checkpackagelib.lib_hash
import checkpackagelib.lib_mk
import checkpackagelib.lib_patch
import checkpackagelib.lib_sysv
VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES = 3
flags = None # Command line arguments.
def parse_args():
parser = argparse.ArgumentParser()
# Do not use argparse.FileType("r") here because only files with known
# format will be open based on the filename.
parser.add_argument("files", metavar="F", type=str, nargs="*",
help="list of files")
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
parser.add_argument("--br2-external", "-b", dest='intree_only', action="store_false",
help="do not apply the pathname filters used for intree files")
parser.add_argument("--manual-url", action="store",
default="http://nightly.buildroot.org/",
help="default: %(default)s")
parser.add_argument("--verbose", "-v", action="count", default=0)
parser.add_argument("--quiet", "-q", action="count", default=0)
# Now the debug options in the order they are processed.
parser.add_argument("--include-only", dest="include_list", action="append",
help="run only the specified functions (debug)")
parser.add_argument("--exclude", dest="exclude_list", action="append",
help="do not run the specified functions (debug)")
parser.add_argument("--dry-run", action="store_true", help="print the "
"functions that would be called for each file (debug)")
return parser.parse_args()
CONFIG_IN_FILENAME = re.compile(r"Config\.\S*$")
DO_CHECK_INTREE = re.compile(r"|".join([
r"Config.in",
r"arch/",
r"boot/",
r"fs/",
r"linux/",
r"package/",
r"system/",
r"toolchain/",
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
]))
DO_NOT_CHECK_INTREE = re.compile(r"|".join([
r"boot/barebox/barebox\.mk$",
r"fs/common\.mk$",
r"package/doc-asciidoc\.mk$",
r"package/pkg-\S*\.mk$",
r"toolchain/helpers\.mk$",
r"toolchain/toolchain-external/pkg-toolchain-external\.mk$",
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
]))
SYSV_INIT_SCRIPT_FILENAME = re.compile(r"/S\d\d[^/]+$")
def get_lib_from_filename(fname):
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
if flags.intree_only:
if DO_CHECK_INTREE.match(fname) is None:
return None
if DO_NOT_CHECK_INTREE.match(fname):
return None
else:
if os.path.basename(fname) == "external.mk" and \
os.path.exists(fname[:-2] + "desc"):
return None
if CONFIG_IN_FILENAME.search(fname):
return checkpackagelib.lib_config
if fname.endswith(".hash"):
return checkpackagelib.lib_hash
if fname.endswith(".mk"):
return checkpackagelib.lib_mk
if fname.endswith(".patch"):
return checkpackagelib.lib_patch
if SYSV_INIT_SCRIPT_FILENAME.search(fname):
return checkpackagelib.lib_sysv
return None
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
def common_inspect_rules(m):
# do not call the base class
if m.__name__.startswith("_"):
return False
if flags.include_list and m.__name__ not in flags.include_list:
return False
if flags.exclude_list and m.__name__ in flags.exclude_list:
return False
return True
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
def is_a_check_function(m):
if not inspect.isclass(m):
return False
if not issubclass(m, checkpackagelib.base._CheckFunction):
return False
return common_inspect_rules(m)
def is_external_tool(m):
if not inspect.isclass(m):
return False
if not issubclass(m, checkpackagelib.base._Tool):
return False
return common_inspect_rules(m)
def print_warnings(warnings):
# Avoid the need to use 'return []' at the end of every check function.
if warnings is None:
return 0 # No warning generated.
for level, message in enumerate(warnings):
if flags.verbose >= level:
print(message.replace("\t", "< tab >").rstrip())
return 1 # One more warning to count.
def check_file_using_lib(fname):
# Count number of warnings generated and lines processed.
nwarnings = 0
nlines = 0
lib = get_lib_from_filename(fname)
if not lib:
if flags.verbose >= VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES:
print("{}: ignored".format(fname))
return nwarnings, nlines
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
internal_functions = inspect.getmembers(lib, is_a_check_function)
external_tools = inspect.getmembers(lib, is_external_tool)
all_checks = internal_functions + external_tools
if flags.dry_run:
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
functions_to_run = [c[0] for c in all_checks]
print("{}: would run: {}".format(fname, functions_to_run))
return nwarnings, nlines
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
objects = [c[1](fname, flags.manual_url) for c in internal_functions]
for cf in objects:
nwarnings += print_warnings(cf.before())
check-package: fix Python3 support This script currently uses "/usr/bin/env python" as shebang but it does not really support Python3. Instead of limiting the script to Python2, fix it to support both versions. So change all imports to absolute imports because Python3 follows PEP328 and dropped implicit relative imports. In order to avoid errors when decoding files with the default 'utf-8' codec, use errors="surrogateescape" when opening files, the docs for open() states: "This is useful for processing files in an unknown encoding.". This argument is not compatible with Python2 open() so import 'six' to use it only when running in Python3. As a consequence the file handler becomes explicit, so use it to close() the file after it got processed. This "surrogateescape" is a simple alternative to the complete solution of opening files with "rb" and changing all functions in the lib*.py files to use bytes objects instead of strings. The only case we can have non-ascii/non-utf-8 files being checked by the script are for patch files when the upstream file to be patched is not ascii or utf-8. There is currently one case in the tree: package/urg/0002-urg-gcc6-fix-narrowing-conversion.patch. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Cc: Arnout Vandecappelle <arnout@mind.be> Reviewed-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Tested-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
2018-08-11 05:48:27 +02:00
if six.PY3:
f = open(fname, "r", errors="surrogateescape")
else:
f = open(fname, "r")
lastline = ""
check-package: fix Python3 support This script currently uses "/usr/bin/env python" as shebang but it does not really support Python3. Instead of limiting the script to Python2, fix it to support both versions. So change all imports to absolute imports because Python3 follows PEP328 and dropped implicit relative imports. In order to avoid errors when decoding files with the default 'utf-8' codec, use errors="surrogateescape" when opening files, the docs for open() states: "This is useful for processing files in an unknown encoding.". This argument is not compatible with Python2 open() so import 'six' to use it only when running in Python3. As a consequence the file handler becomes explicit, so use it to close() the file after it got processed. This "surrogateescape" is a simple alternative to the complete solution of opening files with "rb" and changing all functions in the lib*.py files to use bytes objects instead of strings. The only case we can have non-ascii/non-utf-8 files being checked by the script are for patch files when the upstream file to be patched is not ascii or utf-8. There is currently one case in the tree: package/urg/0002-urg-gcc6-fix-narrowing-conversion.patch. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Cc: Arnout Vandecappelle <arnout@mind.be> Reviewed-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Tested-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
2018-08-11 05:48:27 +02:00
for lineno, text in enumerate(f.readlines()):
nlines += 1
for cf in objects:
if cf.disable.search(lastline):
continue
nwarnings += print_warnings(cf.check_line(lineno + 1, text))
lastline = text
check-package: fix Python3 support This script currently uses "/usr/bin/env python" as shebang but it does not really support Python3. Instead of limiting the script to Python2, fix it to support both versions. So change all imports to absolute imports because Python3 follows PEP328 and dropped implicit relative imports. In order to avoid errors when decoding files with the default 'utf-8' codec, use errors="surrogateescape" when opening files, the docs for open() states: "This is useful for processing files in an unknown encoding.". This argument is not compatible with Python2 open() so import 'six' to use it only when running in Python3. As a consequence the file handler becomes explicit, so use it to close() the file after it got processed. This "surrogateescape" is a simple alternative to the complete solution of opening files with "rb" and changing all functions in the lib*.py files to use bytes objects instead of strings. The only case we can have non-ascii/non-utf-8 files being checked by the script are for patch files when the upstream file to be patched is not ascii or utf-8. There is currently one case in the tree: package/urg/0002-urg-gcc6-fix-narrowing-conversion.patch. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Cc: Arnout Vandecappelle <arnout@mind.be> Reviewed-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Tested-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
2018-08-11 05:48:27 +02:00
f.close()
for cf in objects:
nwarnings += print_warnings(cf.after())
utils/check-package: prepare to run external tools Some file formats have well-established syntax checkers. One example of this is the tool 'shellcheck' that can analyse shell scripts for common mistakes. There is no reason to reimplement such tools in check-package, when we can just call them. Add the ability to check-package to call external tools that will run once for each file to be analysed. For simplicity, when the tool generated one or more warnings, count it as a single warning from check-package, that can display something like this: |$ ./utils/check-package package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |25 lines processed |1 warnings generated |$ ./utils/check-package -vvvvvvvvvvvvvvvv package/unscd/S46unscd |package/unscd/S46unscd:0: run 'shellcheck' and fix the warnings |In package/unscd/S46unscd line 9: | printf "Starting ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 11: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |In package/unscd/S46unscd line 14: | printf "Stopping ${NAME}: " | ^------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo". |In package/unscd/S46unscd line 16: | [ $? -eq 0 ] && echo "OK" || echo "FAIL" | ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. |For more information: | https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf... | https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g... |25 lines processed |1 warnings generated In this first commit, add only the ability for check-package to call external tools and not an example of such tool, as adding each tool to call may need update to the docker image and can lead to it's own discussion on how to implement. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-12-26 19:49:15 +01:00
tools = [c[1](fname) for c in external_tools]
for tool in tools:
nwarnings += print_warnings(tool.run())
return nwarnings, nlines
def __main__():
global flags
flags = parse_args()
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
if flags.intree_only:
# change all paths received to be relative to the base dir
base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
files_to_check = [os.path.relpath(os.path.abspath(f), base_dir) for f in flags.files]
# move current dir so the script find the files
os.chdir(base_dir)
else:
files_to_check = flags.files
if len(files_to_check) == 0:
print("No files to check style")
sys.exit(1)
# Accumulate number of warnings generated and lines processed.
total_warnings = 0
total_lines = 0
check-package: prepare to extend to other directories Currently the script only checks files inside the package/ directory. Upcoming patches will enable it for other directories. In order to reliably test for file names, i.e. the Config.in in the base directory, normalize the path of files to check to a relative path to the base directory. Rename the variable that holds the compiled regexp to better represent its content and rearrange how it is declared to make easy to later add new directories to check. As a consequence the files that declare package infra types would not be ignored anymore, so create a new variable to list the files intree to be ignored during the check. The same variable will be used by upcoming patches to ignore other files. Ignore pkg-*.mk and doc-asciidoc.mk since they are package infra files. In order to not produce weird results when used for files outside the tree (i.e. in a private br2-external) add an explicit command line option (-b) that bypasses any checks that would make a file be ignored by the path that contains it. When in this out-of-tree mode, the user is responsible for providing a list of files to check that do not contain files the script does not understand, e.g. package infra files. As a result of this patch, besides the known use: $ ./utils/check-package package/new-package/* someone with the utils/ directory in the path can now also run: $ cd package/new-package/ $ check-package * or $ check-package -b /path/to/br2-ext-tree/package/staging-package/* Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-04-01 07:08:14 +02:00
for fname in files_to_check:
nwarnings, nlines = check_file_using_lib(fname)
total_warnings += nwarnings
total_lines += nlines
# The warning messages are printed to stdout and can be post-processed
# (e.g. counted by 'wc'), so for stats use stderr. Wait all warnings are
# printed, for the case there are many of them, before printing stats.
sys.stdout.flush()
if not flags.quiet:
print("{} lines processed".format(total_lines), file=sys.stderr)
print("{} warnings generated".format(total_warnings), file=sys.stderr)
if total_warnings > 0:
sys.exit(1)
__main__()