Skip to content

Commit 0c0c371

Browse files
author
thread-liu
committed
[update] format ci, add ignore config file.
1 parent c5a0df2 commit 0c0c371

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

.github/workflows/file_check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ jobs:
1616
- name: Check Format and License
1717
shell: bash
1818
run: |
19-
pip install click chardet
19+
pip install click chardet PyYaml
2020
python tools/file_check.py check 'https://github.com/RT-Thread/rt-thread' 'master'

ignore_format.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# files format check exclude path, please follow the instructions below to modify;
2+
# If you need to exclude an entire folder, add the folder path in dir_path;
3+
# If you need to exclude a file, add the path to the file in file_path.
4+
5+
file_path:
6+
- bsp/stm32/stm32f072-st-nucleo/board/CubeMX_Config/Src/main.c
7+
8+
dir_path:
9+
- bsp/stm32/libraries/STM32F0xx_HAL
10+
- bsp/stm32/libraries/STM32F1xx_HAL
11+
- bsp/stm32/libraries/STM32F2xx_HAL
12+
- bsp/stm32/libraries/STM32F4xx_HAL
13+
- bsp/stm32/libraries/STM32F7xx_HAL
14+
- bsp/stm32/libraries/STM32G0xx_HAL
15+
- bsp/stm32/libraries/STM32G4xx_HAL
16+
- bsp/stm32/libraries/STM32H7xx_HAL
17+
- bsp/stm32/libraries/STM32L0xx_HAL
18+
- bsp/stm32/libraries/STM32L1xx_HAL
19+
- bsp/stm32/libraries/STM32L4xx_HAL
20+
- bsp/stm32/libraries/STM32MPxx_HAL
21+
- bsp/stm32/libraries/STM32WBxx_HAL
22+
- bsp/stm32/libraries/STM32WLxx_HAL

tools/file_check.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import sys
1414
import click
15+
import yaml
1516
import chardet
1617
import logging
1718
import datetime
@@ -25,18 +26,39 @@ def init_logger():
2526
datefmt=date_format,
2627
)
2728

29+
2830
class CheckOut:
2931
def __init__(self, rtt_repo, rtt_branch):
3032
self.root = os.getcwd()
3133
self.rtt_repo = rtt_repo
3234
self.rtt_branch = rtt_branch
3335

36+
def __exclude_file(self, file_path):
37+
ignore_file_path = os.path.join(self.root, 'ignore_format.yml')
38+
try:
39+
with open(ignore_file_path) as f:
40+
ignore_config = yaml.safe_load(f.read())
41+
file_ignore = ignore_config.get("file_path", [])
42+
dir_ignore = ignore_config.get("dir_path", [])
43+
except Exception as e:
44+
logging.error(e)
45+
return 1
46+
47+
if file_path in file_ignore:
48+
return 0
49+
50+
file_dir_path = os.path.dirname(file_path)
51+
if file_dir_path in dir_ignore:
52+
return 0
53+
54+
return 1
55+
3456
def get_new_file(self):
3557
file_list = list()
3658
try:
37-
os.system('git remote add rtt_repo {}'.format(self.rtt_repo))
38-
os.system('git fetch rtt_repo')
39-
os.system('git reset rtt_repo/{} --soft'.format(self.rtt_branch))
59+
os.system('git remote add rtt_repo {} 1>/dev/null'.format(self.rtt_repo))
60+
os.system('git fetch rtt_repo 1>/dev/null')
61+
os.system('git reset rtt_repo/{} --soft 1>/dev/null'.format(self.rtt_branch))
4062
os.system('git status > git.txt')
4163
except Exception as e:
4264
logging.error(e)
@@ -60,7 +82,9 @@ def get_new_file(self):
6082
else:
6183
continue
6284

63-
file_list.append(file_path)
85+
result = self.__exclude_file(file_path)
86+
if result != 0:
87+
file_list.append(file_path)
6488

6589
return file_list
6690

@@ -96,13 +120,11 @@ def check(self):
96120
encoding_check_result = True
97121
format_check_result = True
98122
for file_path in self.file_list:
99-
file_lines = ''
100123
code = ''
101124
if file_path.endswith(".c") or file_path.endswith(".h"):
102125
try:
103-
with open(file_path, 'r') as f:
126+
with open(file_path, 'rb') as f:
104127
file = f.read()
105-
file_lines = f.readlines()
106128
# get file encoding
107129
code = chardet.detect(file)['encoding']
108130
except Exception as e:
@@ -116,6 +138,8 @@ def check(self):
116138
else:
117139
logging.info('[{0}]: encoding check success.'.format(file_path))
118140

141+
with open(file_path, 'r') as f:
142+
file_lines = f.readlines()
119143
format_check_result = self.__check_file(file_lines)
120144

121145
if not encoding_check_result or not format_check_result:
@@ -155,8 +179,8 @@ def check(self):
155179
true_year = '2006-{}'.format(current_year)
156180
if license_year != true_year:
157181
logging.warning("[{0}]: license year: {} is not true: {}, please update.".format(file_path,
158-
license_year,
159-
true_year))
182+
license_year,
183+
true_year))
160184

161185
else:
162186
logging.info("[{0}]: license check success.".format(file_path))

0 commit comments

Comments
 (0)