[update] format ci, add ignore config file.

This commit is contained in:
thread-liu 2021-04-06 10:39:22 +08:00
parent c5a0df2254
commit 0c0c371bc6
3 changed files with 56 additions and 10 deletions

View File

@ -16,5 +16,5 @@ jobs:
- name: Check Format and License
shell: bash
run: |
pip install click chardet
pip install click chardet PyYaml
python tools/file_check.py check 'https://github.com/RT-Thread/rt-thread' 'master'

22
ignore_format.yml Normal file
View File

@ -0,0 +1,22 @@
# files format check exclude path, please follow the instructions below to modify;
# If you need to exclude an entire folder, add the folder path in dir_path;
# If you need to exclude a file, add the path to the file in file_path.
file_path:
- bsp/stm32/stm32f072-st-nucleo/board/CubeMX_Config/Src/main.c
dir_path:
- bsp/stm32/libraries/STM32F0xx_HAL
- bsp/stm32/libraries/STM32F1xx_HAL
- bsp/stm32/libraries/STM32F2xx_HAL
- bsp/stm32/libraries/STM32F4xx_HAL
- bsp/stm32/libraries/STM32F7xx_HAL
- bsp/stm32/libraries/STM32G0xx_HAL
- bsp/stm32/libraries/STM32G4xx_HAL
- bsp/stm32/libraries/STM32H7xx_HAL
- bsp/stm32/libraries/STM32L0xx_HAL
- bsp/stm32/libraries/STM32L1xx_HAL
- bsp/stm32/libraries/STM32L4xx_HAL
- bsp/stm32/libraries/STM32MPxx_HAL
- bsp/stm32/libraries/STM32WBxx_HAL
- bsp/stm32/libraries/STM32WLxx_HAL

View File

@ -12,6 +12,7 @@ import os
import re
import sys
import click
import yaml
import chardet
import logging
import datetime
@ -25,18 +26,39 @@ def init_logger():
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):
ignore_file_path = os.path.join(self.root, 'ignore_format.yml')
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)
return 1
if file_path in file_ignore:
return 0
file_dir_path = os.path.dirname(file_path)
if file_dir_path in dir_ignore:
return 0
return 1
def get_new_file(self):
file_list = list()
try:
os.system('git remote add rtt_repo {}'.format(self.rtt_repo))
os.system('git fetch rtt_repo')
os.system('git reset rtt_repo/{} --soft'.format(self.rtt_branch))
os.system('git remote add rtt_repo {} 1>/dev/null'.format(self.rtt_repo))
os.system('git fetch rtt_repo 1>/dev/null')
os.system('git reset rtt_repo/{} --soft 1>/dev/null'.format(self.rtt_branch))
os.system('git status > git.txt')
except Exception as e:
logging.error(e)
@ -60,7 +82,9 @@ class CheckOut:
else:
continue
file_list.append(file_path)
result = self.__exclude_file(file_path)
if result != 0:
file_list.append(file_path)
return file_list
@ -96,13 +120,11 @@ class FormatCheck:
encoding_check_result = True
format_check_result = True
for file_path in self.file_list:
file_lines = ''
code = ''
if file_path.endswith(".c") or file_path.endswith(".h"):
try:
with open(file_path, 'r') as f:
with open(file_path, 'rb') as f:
file = f.read()
file_lines = f.readlines()
# get file encoding
code = chardet.detect(file)['encoding']
except Exception as e:
@ -116,6 +138,8 @@ class FormatCheck:
else:
logging.info('[{0}]: encoding check success.'.format(file_path))
with open(file_path, 'r') as f:
file_lines = f.readlines()
format_check_result = self.__check_file(file_lines)
if not encoding_check_result or not format_check_result:
@ -155,8 +179,8 @@ class LicenseCheck:
true_year = '2006-{}'.format(current_year)
if license_year != true_year:
logging.warning("[{0}]: license year: {} is not true: {}, please update.".format(file_path,
license_year,
true_year))
license_year,
true_year))
else:
logging.info("[{0}]: license check success.".format(file_path))