-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[CI] Added feature to filter out files to file_check.py that do not require cppcheck #7499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
supperthomas
merged 14 commits into
RT-Thread:master
from
dejavudwh:CI-filecheck-format-ignore
May 15, 2023
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
77b0e91
Add cppcheck to filecheck.py
dejavudwh 5f8500e
Tests whether directories contained in.ignore_format.yml are cppcheck
dejavudwh 1953012
Tests whether file contained in.ignore_format.yml are cppcheck
dejavudwh d5ca69a
Restore files modified for testing purposes
dejavudwh d931fbe
Add error message about cppcheck
dejavudwh 4a6bc13
Extract the CPPCheck and get_new_file separately
dejavudwh 6763dd4
Tests whether file contained in.ignore_format.yml are cppcheck(new)
dejavudwh f7e4328
Tests whether directories contained in.ignore_format.yml are cppcheck…
dejavudwh 5adfde1
Restore files modified for testing purposes(new)
dejavudwh 973e6a8
Update memheap.c
supperthomas 51efa59
Update file_check.py
supperthomas ee4ed19
Update stm32f0xx_hal.c
supperthomas cdb6d80
Update memheap.c
supperthomas 4f6e718
Update stm32f0xx_hal.c
supperthomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
name: Check File Format and License | ||
name: Check File Format, License and Static Code Analysis(cppcheck) | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
scancode_job: | ||
runs-on: ubuntu-latest | ||
name: Scan code format and license | ||
name: Scan code format, license and static code analysis | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: 3.8 | ||
|
||
- name: Check Format and License | ||
- name: Check Format, License and Static Code Analysis(cppcheck) | ||
shell: bash | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get -qq install cppcheck | ||
cppcheck --version | ||
pip install click chardet PyYaml | ||
python tools/ci/file_check.py check 'https://github.com/RT-Thread/rt-thread' 'master' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
|
||
dir_path: | ||
- Libraries/CH32V10x_StdPeriph_Driver | ||
- Libraries/ch56x_drivers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# | ||
# Copyright (c) 2006-2023, RT-Thread Development Team | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Change Logs: | ||
# Date Author Notes | ||
# 2021-04-01 LiuKang the first version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注释改下。 |
||
# | ||
|
||
import click | ||
import logging | ||
import subprocess | ||
import sys | ||
import format_ignore | ||
|
||
class CPPCheck: | ||
def __init__(self, file_list): | ||
self.file_list = file_list | ||
|
||
def check(self): | ||
file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))] | ||
logging.info("Start to static code analysis.") | ||
check_result = True | ||
for file in file_list_filtered: | ||
result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE) | ||
logging.info(result.stdout.decode()) | ||
logging.info(result.stderr.decode()) | ||
if result.stderr: | ||
check_result = False | ||
return check_result | ||
|
||
@click.group() | ||
@click.pass_context | ||
def cli(ctx): | ||
pass | ||
|
||
@cli.command() | ||
@click.argument( | ||
'repo', | ||
nargs=1, | ||
type=click.STRING, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 去掉 |
||
default='https://github.com/RT-Thread/rt-thread', | ||
) | ||
@click.argument( | ||
'branch', | ||
nargs=1, | ||
type=click.STRING, | ||
default='master', | ||
) | ||
def check(repo, branch): | ||
""" | ||
static code analysis(cppcheck). | ||
""" | ||
format_ignore.init_logger() | ||
# get modified files list | ||
checkout = format_ignore.CheckOut(repo, branch) | ||
file_list = checkout.get_new_file() | ||
if file_list is None: | ||
logging.error("checkout files fail") | ||
sys.exit(1) | ||
|
||
# use cppcheck | ||
cpp_check = CPPCheck(file_list) | ||
cpp_check_result = cpp_check.check() | ||
|
||
if not cpp_check_result: | ||
logging.error("static code analysis(cppcheck) fail.") | ||
sys.exit(1) | ||
logging.info("check success.") | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == '__main__': | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# | ||
# Copyright (c) 2006-2022, RT-Thread Development Team | ||
# Copyright (c) 2006-2023, RT-Thread Development Team | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件不用改 |
||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
@@ -16,7 +16,7 @@ | |
import chardet | ||
import logging | ||
import datetime | ||
|
||
import subprocess | ||
|
||
supperthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def init_logger(): | ||
log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s " | ||
|
@@ -221,6 +221,21 @@ def check(self): | |
|
||
return check_result | ||
|
||
class CPPCheck: | ||
def __init__(self, file_list): | ||
self.file_list = file_list | ||
|
||
def check(self): | ||
file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))] | ||
logging.info("Start to static code analysis.") | ||
check_result = True | ||
for file in file_list_filtered: | ||
result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE) | ||
logging.info(result.stdout.decode()) | ||
logging.info(result.stderr.decode()) | ||
if result.stderr: | ||
check_result = False | ||
return check_result | ||
|
||
@click.group() | ||
@click.pass_context | ||
|
@@ -251,7 +266,7 @@ def cli(ctx): | |
) | ||
def check(check_license, repo, branch): | ||
""" | ||
check files license and format. | ||
check files license, format and static code analysis(cppcheck). | ||
""" | ||
init_logger() | ||
# get modified files list | ||
|
@@ -264,13 +279,15 @@ def check(check_license, repo, branch): | |
# check modified files format | ||
format_check = FormatCheck(file_list) | ||
format_check_result = format_check.check() | ||
cpp_check = CPPCheck(file_list) | ||
cpp_check_result = cpp_check.check() | ||
license_check_result = True | ||
if check_license: | ||
license_check = LicenseCheck(file_list) | ||
license_check_result = license_check.check() | ||
|
||
if not format_check_result or not license_check_result: | ||
logging.error("file format check or license check fail.") | ||
if not format_check_result or not cpp_check_result or not license_check_result: | ||
logging.error("file format check or license check or static code analysis(cppcheck) fail.") | ||
sys.exit(1) | ||
logging.info("check success.") | ||
sys.exit(0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import yaml | ||
supperthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import logging | ||
import os | ||
import subprocess | ||
|
||
def init_logger(): | ||
log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s " | ||
date_format = '%Y-%m-%d %H:%M:%S %a ' | ||
logging.basicConfig(level=logging.INFO, | ||
format=log_format, | ||
datefmt=date_format, | ||
) | ||
|
||
class CheckOut: | ||
def __init__(self, rtt_repo, rtt_branch): | ||
self.root = os.getcwd() | ||
self.rtt_repo = rtt_repo | ||
self.rtt_branch = rtt_branch | ||
|
||
def __exclude_file(self, file_path): | ||
dir_number = file_path.split('/') | ||
ignore_path = file_path | ||
|
||
# gets the file path depth. | ||
for i in dir_number: | ||
# current directory. | ||
dir_name = os.path.dirname(ignore_path) | ||
ignore_path = dir_name | ||
# judge the ignore file exists in the current directory. | ||
ignore_file_path = os.path.join(dir_name, ".ignore_format.yml") | ||
if not os.path.exists(ignore_file_path): | ||
continue | ||
try: | ||
with open(ignore_file_path) as f: | ||
ignore_config = yaml.safe_load(f.read()) | ||
file_ignore = ignore_config.get("file_path", []) | ||
dir_ignore = ignore_config.get("dir_path", []) | ||
except Exception as e: | ||
logging.error(e) | ||
continue | ||
logging.debug("ignore file path: {}".format(ignore_file_path)) | ||
logging.debug("file_ignore: {}".format(file_ignore)) | ||
logging.debug("dir_ignore: {}".format(dir_ignore)) | ||
try: | ||
# judge file_path in the ignore file. | ||
for file in file_ignore: | ||
if file is not None: | ||
file_real_path = os.path.join(dir_name, file) | ||
if file_real_path == file_path: | ||
logging.info("ignore file path: {}".format(file_real_path)) | ||
return 0 | ||
|
||
file_dir_path = os.path.dirname(file_path) | ||
for _dir in dir_ignore: | ||
if _dir is not None: | ||
dir_real_path = os.path.join(dir_name, _dir) | ||
if file_dir_path.startswith(dir_real_path): | ||
logging.info("ignore dir path: {}".format(dir_real_path)) | ||
return 0 | ||
except Exception as e: | ||
logging.error(e) | ||
continue | ||
|
||
return 1 | ||
|
||
def get_new_file(self): | ||
supperthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE) | ||
file_list = result.stdout.decode().strip().split('\n') | ||
new_files = [] | ||
for line in file_list: | ||
logging.info("modified file -> {}".format(line)) | ||
result = self.__exclude_file(line) | ||
if result != 0: | ||
new_files.append(line) | ||
|
||
return new_files |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉