file_check.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_result = True
  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. format_check_result = self.__check_file(file_lines, file_path)
  151. if not encoding_check_result or not format_check_result:
  152. logging.error("files format check fail.")
  153. return False
  154. logging.info("files format check success.")
  155. return True
  156. class LicenseCheck:
  157. def __init__(self, file_list):
  158. self.file_list = file_list
  159. def check(self):
  160. current_year = datetime.date.today().year
  161. logging.info("current year: {}".format(current_year))
  162. if len(self.file_list) == 0:
  163. logging.warning("There are no files to check license.")
  164. return 0
  165. logging.info("Start to check files license.")
  166. check_result = True
  167. for file_path in self.file_list:
  168. if file_path.endswith(".c") or file_path.endswith(".h"):
  169. try:
  170. with open(file_path, 'r') as f:
  171. file = f.readlines()
  172. except Exception as e:
  173. logging.error(e)
  174. else:
  175. continue
  176. if 'Copyright' in file[1] and 'SPDX-License-Identifier: Apache-2.0' in file[3]:
  177. try:
  178. license_year = re.search(r'2006-\d{4}', file[1]).group()
  179. true_year = '2006-{}'.format(current_year)
  180. if license_year != true_year:
  181. logging.warning("[{0}]: license year: {} is not true: {}, please update.".format(file_path,
  182. license_year,
  183. true_year))
  184. else:
  185. logging.info("[{0}]: license check success.".format(file_path))
  186. except Exception as e:
  187. logging.error(e)
  188. else:
  189. logging.error("[{0}]: license check fail.".format(file_path))
  190. check_result = False
  191. return check_result
  192. @click.group()
  193. @click.pass_context
  194. def cli(ctx):
  195. pass
  196. @cli.command()
  197. @click.option(
  198. '--license',
  199. "check_license",
  200. required=False,
  201. type=click.BOOL,
  202. flag_value=True,
  203. help="Enable File license check.",
  204. )
  205. @click.argument(
  206. 'repo',
  207. nargs=1,
  208. type=click.STRING,
  209. default='https://github.com/RT-Thread/rt-thread',
  210. )
  211. @click.argument(
  212. 'branch',
  213. nargs=1,
  214. type=click.STRING,
  215. default='master',
  216. )
  217. def check(check_license, repo, branch):
  218. """
  219. check files license and format.
  220. """
  221. init_logger()
  222. # get modified files list
  223. checkout = CheckOut(repo, branch)
  224. file_list = checkout.get_new_file()
  225. if file_list is None:
  226. logging.error("checkout files fail")
  227. sys.exit(1)
  228. # check modified files format
  229. format_check = FormatCheck(file_list)
  230. format_check_result = format_check.check()
  231. license_check_result = True
  232. if check_license:
  233. license_check = LicenseCheck(file_list)
  234. license_check_result = license_check.check()
  235. if not format_check_result or not license_check_result:
  236. logging.error("file format check or license check fail.")
  237. sys.exit(1)
  238. logging.info("check success.")
  239. sys.exit(0)
  240. if __name__ == '__main__':
  241. cli()