2018-12-06 09:08:18 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2023-09-18 09:12:08 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
|
2021-11-23 08:02:47 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-12-06 09:08:18 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
from __future__ import print_function, unicode_literals
|
2020-10-30 05:28:24 -04:00
|
|
|
|
|
|
|
import argparse
|
2018-12-06 09:08:18 -05:00
|
|
|
import os
|
|
|
|
import re
|
2023-09-18 09:12:08 -04:00
|
|
|
import subprocess
|
2020-10-30 05:28:24 -04:00
|
|
|
import sys
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
from idf_ci_utils import IDF_PATH, get_submodule_dirs
|
2018-12-06 09:08:18 -05:00
|
|
|
|
2019-01-31 07:41:31 -05:00
|
|
|
# regular expression for matching Kconfig files
|
2019-05-14 10:23:19 -04:00
|
|
|
RE_KCONFIG = r'^Kconfig(\.projbuild)?(\.in)?$'
|
2018-12-06 09:08:18 -05:00
|
|
|
|
|
|
|
# ignored directories (makes sense only when run on IDF_PATH)
|
2023-09-18 09:12:08 -04:00
|
|
|
# Note: ignore_dirs is a tuple in order to be able to use it directly with the startswith() built-in function which
|
2018-12-06 09:08:18 -05:00
|
|
|
# accepts tuples but no lists.
|
2023-09-18 09:12:08 -04:00
|
|
|
IGNORE_DIRS: tuple = (
|
2018-12-06 09:08:18 -05:00
|
|
|
# Kconfigs from submodules need to be ignored:
|
2020-10-30 05:28:24 -04:00
|
|
|
os.path.join(IDF_PATH, 'components', 'mqtt', 'esp-mqtt'),
|
2019-05-09 09:43:07 -04:00
|
|
|
# Test Kconfigs are also ignored
|
2020-10-30 05:28:24 -04:00
|
|
|
os.path.join(IDF_PATH, 'tools', 'ldgen', 'test', 'data'),
|
|
|
|
os.path.join(IDF_PATH, 'tools', 'kconfig_new', 'test'),
|
2018-12-06 09:08:18 -05:00
|
|
|
)
|
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
ignore_dirs: tuple = IGNORE_DIRS
|
2018-12-06 09:08:18 -05:00
|
|
|
|
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
def valid_directory(path:str) -> str:
|
2018-12-06 09:08:18 -05:00
|
|
|
if not os.path.isdir(path):
|
2021-01-25 21:49:01 -05:00
|
|
|
raise argparse.ArgumentTypeError('{} is not a valid directory!'.format(path))
|
2023-09-18 09:12:08 -04:00
|
|
|
return str(path)
|
2020-10-30 05:28:24 -04:00
|
|
|
|
2018-12-06 09:08:18 -05:00
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
parser = argparse.ArgumentParser(description='Kconfig style checker')
|
|
|
|
parser.add_argument(
|
|
|
|
'files',
|
|
|
|
nargs='*',
|
|
|
|
help='Kconfig files to check (full paths separated by space)',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--exclude-submodules',
|
|
|
|
action='store_true',
|
|
|
|
help='Exclude submodules',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--includes',
|
|
|
|
'-d',
|
|
|
|
nargs='*',
|
|
|
|
help='Extra paths for recursively searching Kconfig files. (for example $IDF_PATH)',
|
|
|
|
type=valid_directory
|
|
|
|
)
|
2018-12-06 09:08:18 -05:00
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
args, unknown_args = parser.parse_known_args()
|
2018-12-06 09:08:18 -05:00
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
# if the deprecated argument '--exclude-submodules' is used
|
|
|
|
if args.exclude_submodules:
|
|
|
|
ignore_dirs = ignore_dirs + tuple(get_submodule_dirs(full_path=True))
|
2020-10-30 05:28:24 -04:00
|
|
|
|
2024-01-04 10:59:29 -05:00
|
|
|
files_to_check: list = args.files
|
2020-10-30 05:28:24 -04:00
|
|
|
|
2023-09-18 09:12:08 -04:00
|
|
|
# if the deprecated argument '--includes' is used all valid paths are checked for KConfigs
|
|
|
|
# except IGNORE_DIRS and submodules (if exclude is given)
|
|
|
|
# paths to KConfig files are passed to esp-idf-kconfig kconfcheck tool
|
|
|
|
if args.includes:
|
2020-10-30 05:28:24 -04:00
|
|
|
for directory in args.includes:
|
|
|
|
for root, dirnames, filenames in os.walk(directory):
|
|
|
|
for filename in filenames:
|
|
|
|
full_path = os.path.join(root, filename)
|
2023-09-18 09:12:08 -04:00
|
|
|
if full_path.startswith(ignore_dirs):
|
|
|
|
continue
|
2020-10-30 05:28:24 -04:00
|
|
|
if re.search(RE_KCONFIG, filename):
|
2023-09-18 09:12:08 -04:00
|
|
|
files_to_check.append(f'{full_path}')
|
2020-10-30 05:28:24 -04:00
|
|
|
elif re.search(RE_KCONFIG, filename, re.IGNORECASE):
|
|
|
|
# On Windows Kconfig files are working with different cases!
|
2023-09-18 09:12:08 -04:00
|
|
|
print(
|
|
|
|
'{}: Incorrect filename. The case should be "Kconfig"!'.format(
|
|
|
|
full_path
|
|
|
|
)
|
|
|
|
)
|
2018-12-06 09:08:18 -05:00
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if __name__ == '__main__':
|
2023-09-18 09:12:08 -04:00
|
|
|
sys.exit(subprocess.run([sys.executable, '-m', 'kconfcheck'] + files_to_check + unknown_args).returncode)
|