| 117 | |
| 118 | |
| 119 | class FormatCheck: |
| 120 | def __init__(self, file_list): |
| 121 | self.file_list = file_list |
| 122 | |
| 123 | def __check_rt_errorcode(self, line): |
| 124 | pattern = re.compile(r'return\s+(RT_ERROR|RT_ETIMEOUT|RT_EFULL|RT_EEMPTY|RT_ENOMEM|RT_ENOSYS|RT_EBUSY|RT_EIO|RT_EINTR|RT_EINVAL|RT_ENOENT|RT_ENOSPC|RT_EPERM|RT_ETRAP|RT_EFAULT)') |
| 125 | match = pattern.search(line) |
| 126 | if match: |
| 127 | return False |
| 128 | else: |
| 129 | return True |
| 130 | |
| 131 | def __check_file(self, file_lines, file_path): |
| 132 | line_num = 0 |
| 133 | check_result = True |
| 134 | for line in file_lines: |
| 135 | line_num += 1 |
| 136 | # check line start |
| 137 | line_start = line.replace(' ', '') |
| 138 | # find tab |
| 139 | if line_start.startswith('\t'): |
| 140 | logging.error("{} line[{}]: please use space replace tab at the start of this line.".format(file_path, line_num)) |
| 141 | check_result = False |
| 142 | # check line end |
| 143 | line_end = line.split('\n')[0] |
| 144 | if line_end.endswith(' ') or line_end.endswith('\t'): |
| 145 | logging.error("{} line[{}]: please delete extra space at the end of this line.".format(file_path, line_num)) |
| 146 | check_result = False |
| 147 | if self.__check_rt_errorcode(line) == False: |
| 148 | logging.error("{} line[{}]: the RT-Thread error code should return negative value. e.g. return -RT_ERROR".format(file_path, line_num)) |
| 149 | check_result = False |
| 150 | return check_result |
| 151 | |
| 152 | def check(self): |
| 153 | logging.info("Start to check files format.") |
| 154 | if len(self.file_list) == 0: |
| 155 | logging.warning("There are no files to check format.") |
| 156 | return True |
| 157 | encoding_check_result = True |
| 158 | format_check_fail_files = 0 |
| 159 | for file_path in self.file_list: |
| 160 | code = '' |
| 161 | if file_path.endswith(".c") or file_path.endswith(".h"): |
| 162 | try: |
| 163 | with open(file_path, 'rb') as f: |
| 164 | file = f.read() |
| 165 | # get file encoding |
| 166 | chardet_report = chardet.detect(file) |
| 167 | code = chardet_report['encoding'] |
| 168 | confidence = chardet_report['confidence'] |
| 169 | except Exception as e: |
| 170 | logging.error(e) |
| 171 | else: |
| 172 | continue |
| 173 | |
| 174 | if code != 'utf-8' and code != 'ascii' and confidence > 0.8: |
| 175 | logging.error("[{0}]: encoding {1} not utf-8, please format it.".format(file_path, code)) |
| 176 | encoding_check_result = False |