file_check.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #
  2. # Copyright (c) 2006-2021, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2021-04-01 LiuKang the first version
  9. #
  10. import os
  11. import re
  12. import sys
  13. import click
  14. import yaml
  15. import chardet
  16. import logging
  17. import datetime
  18. def init_logger():
  19. log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
  20. date_format = '%Y-%m-%d %H:%M:%S %a '
  21. logging.basicConfig(level=logging.INFO,
  22. format=log_format,
  23. datefmt=date_format,
  24. )
  25. class CheckOut:
  26. def __init__(self, rtt_repo, rtt_branch):
  27. self.root = os.getcwd()
  28. self.rtt_repo = rtt_repo
  29. self.rtt_branch = rtt_branch
  30. def __exclude_file(self, file_path):
  31. dir_number = file_path.split('/')
  32. ignore_path = file_path
  33. # gets the file path depth.
  34. for i in dir_number:
  35. # current directory.
  36. dir_name = os.path.dirname(ignore_path)
  37. ignore_path = dir_name
  38. # judge the ignore file exists in the current directory.
  39. ignore_file_path = os.path.join(dir_name, ".ignore_format.yml")
  40. if not os.path.exists(ignore_file_path):
  41. continue
  42. try:
  43. with open(ignore_file_path) as f:
  44. ignore_config = yaml.safe_load(f.read())
  45. file_ignore = ignore_config.get("file_path", [])
  46. dir_ignore = ignore_config.get("dir_path", [])
  47. except Exception as e:
  48. logging.error(e)
  49. continue
  50. logging.debug("ignore file path: {}".format(ignore_file_path))
  51. logging.debug("file_ignore: {}".format(file_ignore))
  52. logging.debug("dir_ignore: {}".format(dir_ignore))
  53. try:
  54. # judge file_path in the ignore file.
  55. for file in file_ignore:
  56. if file is not None:
  57. file_real_path = os.path.join(dir_name, file)
  58. if file_real_path == file_path:
  59. logging.info("ignore file path: {}".format(file_real_path))
  60. return 0
  61. file_dir_path = os.path.dirname(file_path)
  62. for _dir in dir_ignore:
  63. if _dir is not None:
  64. dir_real_path = os.path.join(dir_name, _dir)
  65. if file_dir_path.startswith(dir_real_path):
  66. logging.info("ignore dir path: {}".format(dir_real_path))
  67. return 0
  68. except Exception as e:
  69. logging.error(e)
  70. continue
  71. return 1
  72. def get_new_file(self):
  73. file_list = list()
  74. try:
  75. os.system('git remote add rtt_repo {}'.format(self.rtt_repo))
  76. os.system('git fetch rtt_repo')
  77. os.system('git reset rtt_repo/{} --soft'.format(self.rtt_branch))
  78. os.system('git status > git.txt')
  79. except Exception as e:
  80. logging.error(e)
  81. return None
  82. try:
  83. with open('git.txt', 'r') as f:
  84. file_lines = f.readlines()
  85. except Exception as e:
  86. logging.error(e)
  87. return None
  88. file_path = ''
  89. for line in file_lines:
  90. if 'new file' in line:
  91. file_path = line.split('new file:')[1].strip()
  92. logging.info('new file -> {}'.format(file_path))
  93. elif 'deleted' in line:
  94. logging.info('deleted file -> {}'.format(line.split('deleted:')[1].strip()))
  95. elif 'modified' in line:
  96. file_path = line.split('modified:')[1].strip()
  97. logging.info('modified file -> {}'.format(file_path))
  98. else:
  99. continue
  100. result = self.__exclude_file(file_path)
  101. if result != 0:
  102. file_list.append(file_path)
  103. return file_list
  104. class FormatCheck:
  105. def __init__(self, file_list):
  106. self.file_list = file_list
  107. def __check_file(self, file_lines, file_path):
  108. line_num = 1
  109. check_result = True
  110. for line in file_lines:
  111. # check line start
  112. line_start = line.replace(' ', '')
  113. # find tab
  114. if line_start.startswith('\t'):
  115. logging.error("{} line[{}]: please use space replace tab at the start of this line.".format(file_path, line_num))
  116. check_result = False
  117. # check line end
  118. lin_end = line.split('\n')[0]
  119. if lin_end.endswith(' ') or lin_end.endswith('\t'):
  120. logging.error("{} line[{}]: please delete extra space at the end of this line.".format(file_path, line_num))
  121. check_result = False
  122. line_num += 1
  123. return check_result
  124. def check(self):
  125. logging.info("Start to check files format.")
  126. if len(self.file_list) == 0:
  127. logging.warning("There are no files to check format.")
  128. return True
  129. encoding_check_result = True
  130. format_check_fail_files = 0
  131. for file_path in self.file_list:
  132. code = ''
  133. if file_path.endswith(".c") or file_path.endswith(".h"):
  134. try:
  135. with open(file_path, 'rb') as f:
  136. file = f.read()
  137. # get file encoding
  138. code = chardet.detect(file)['encoding']
  139. except Exception as e:
  140. logging.error(e)
  141. else:
  142. continue
  143. if code != 'utf-8' and code != 'ascii':
  144. logging.error("[{0}]: encoding not utf-8, please format it.".format(file_path))
  145. encoding_check_result = False
  146. else:
  147. logging.info('[{0}]: encoding check success.'.format(file_path))
  148. with open(file_path, 'r', encoding = "utf-8") as f:
  149. file_lines = f.readlines()
  150. if not self.__check_file(file_lines, file_path):
  151. format_check_fail_files += 1
  152. if (not encoding_check_result) or (format_check_fail_files != 0):
  153. logging.error("files format check fail.")
  154. return False
  155. logging.info("files format check success.")
  156. return True
  157. class LicenseCheck:
  158. def __init__(self, file_list):
  159. self.file_list = file_list
  160. def check(self):
  161. current_year = datetime.date.today().year
  162. logging.info("current year: {}".format(current_year))
  163. if len(self.file_list) == 0:
  164. logging.warning("There are no files to check license.")
  165. return 0
  166. logging.info("Start to check files license.")
  167. check_result = True
  168. for file_path in self.file_list:
  169. if file_path.endswith(".c") or file_path.endswith(".h"):
  170. try:
  171. with open(file_path, 'r') as f:
  172. file = f.readlines()
  173. except Exception as e:
  174. logging.error(e)
  175. else:
  176. continue
  177. if 'Copyright' in file[1] and 'SPDX-License-Identifier: Apache-2.0' in file[3]:
  178. try:
  179. license_year = re.search(r'2006-\d{4}', file[1]).group()
  180. true_year = '2006-{}'.format(current_year)
  181. if license_year != true_year:
  182. logging.warning("[{0}]: license year: {} is not true: {}, please update.".format(file_path,
  183. license_year,
  184. true_year))
  185. else:
  186. logging.info("[{0}]: license check success.".format(file_path))
  187. except Exception as e:
  188. logging.error(e)
  189. else:
  190. logging.error("[{0}]: license check fail.".format(file_path))
  191. check_result = False
  192. return check_result
  193. @click.group()
  194. @click.pass_context
  195. def cli(ctx):
  196. pass
  197. @cli.command()
  198. @click.option(
  199. '--license',
  200. "check_license",
  201. required=False,
  202. type=click.BOOL,
  203. flag_value=True,
  204. help="Enable File license check.",
  205. )
  206. @click.argument(
  207. 'repo',
  208. nargs=1,
  209. type=click.STRING,
  210. default='https://github.com/RT-Thread/rt-thread',
  211. )
  212. @click.argument(
  213. 'branch',
  214. nargs=1,
  215. type=click.STRING,
  216. default='master',
  217. )
  218. def check(check_license, repo, branch):
  219. """
  220. check files license and format.
  221. """
  222. init_logger()
  223. # get modified files list
  224. checkout = CheckOut(repo, branch)
  225. file_list = checkout.get_new_file()
  226. if file_list is None:
  227. logging.error("checkout files fail")
  228. sys.exit(1)
  229. # check modified files format
  230. format_check = FormatCheck(file_list)
  231. format_check_result = format_check.check()
  232. license_check_result = True
  233. if check_license:
  234. license_check = LicenseCheck(file_list)
  235. license_check_result = license_check.check()
  236. if not format_check_result or not license_check_result:
  237. logging.error("file format check or license check fail.")
  238. sys.exit(1)
  239. logging.info("check success.")
  240. sys.exit(0)
  241. if __name__ == '__main__':
  242. cli()