file_check.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. try:
  51. # judge file_path in the ignore file.
  52. for file in file_ignore:
  53. if file is not None:
  54. file_real_path = os.path.join(dir_name, file)
  55. if file_real_path == file_path:
  56. logging.info("ignore file path: {}".format(file_real_path))
  57. return 0
  58. file_dir_path = os.path.dirname(file_path)
  59. for _dir in dir_ignore:
  60. if _dir is not None:
  61. dir_real_path = os.path.join(dir_name, _dir)
  62. if dir_real_path == file_dir_path:
  63. logging.info("ignore dir path: {}".format(dir_real_path))
  64. return 0
  65. except Exception as e:
  66. logging.error(e)
  67. continue
  68. return 1
  69. def get_new_file(self):
  70. file_list = list()
  71. try:
  72. os.system('git remote add rtt_repo {} 1>/dev/null'.format(self.rtt_repo))
  73. os.system('git fetch rtt_repo 1>/dev/null')
  74. os.system('git reset rtt_repo/{} --soft 1>/dev/null'.format(self.rtt_branch))
  75. os.system('git status > git.txt')
  76. except Exception as e:
  77. logging.error(e)
  78. return None
  79. try:
  80. with open('git.txt', 'r') as f:
  81. file_lines = f.readlines()
  82. except Exception as e:
  83. logging.error(e)
  84. return None
  85. file_path = ''
  86. for line in file_lines:
  87. if 'new file' in line:
  88. file_path = line.split('new file:')[1].strip()
  89. logging.info('new file -> {}'.format(file_path))
  90. elif 'deleted' in line:
  91. logging.info('deleted file -> {}'.format(line.split('deleted:')[1].strip()))
  92. elif 'modified' in line:
  93. file_path = line.split('modified:')[1].strip()
  94. logging.info('modified file -> {}'.format(file_path))
  95. else:
  96. continue
  97. result = self.__exclude_file(file_path)
  98. if result != 0:
  99. file_list.append(file_path)
  100. return file_list
  101. class FormatCheck:
  102. def __init__(self, file_list):
  103. self.file_list = file_list
  104. def __check_file(self, file_lines, file_path):
  105. line_num = 1
  106. check_result = False
  107. for line in file_lines:
  108. # check line start
  109. line_start = line.replace(' ', '')
  110. # find tab
  111. if line_start.startswith('\t'):
  112. logging.error("{} line[{}]: please use space replace tab at the start of this line.".format(file_path, line_num))
  113. check_result = False
  114. # check line end
  115. lin_end = line.split('\n')[0]
  116. if lin_end.endswith(' ') or lin_end.endswith('\t'):
  117. logging.error("{} line[{}]: please delete extra space at the end of this line.".format(file_path, line_num))
  118. check_result = False
  119. line_num += 1
  120. return check_result
  121. def check(self):
  122. logging.info("Start to check files format.")
  123. if len(self.file_list) == 0:
  124. logging.warning("There are no files to check license.")
  125. return 0
  126. encoding_check_result = True
  127. format_check_result = True
  128. for file_path in self.file_list:
  129. code = ''
  130. if file_path.endswith(".c") or file_path.endswith(".h"):
  131. try:
  132. with open(file_path, 'rb') as f:
  133. file = f.read()
  134. # get file encoding
  135. code = chardet.detect(file)['encoding']
  136. except Exception as e:
  137. logging.error(e)
  138. else:
  139. continue
  140. if code != 'utf-8':
  141. logging.error("[{0}]: encoding not utf-8, please format it.".format(file_path))
  142. encoding_check_result = False
  143. else:
  144. logging.info('[{0}]: encoding check success.'.format(file_path))
  145. with open(file_path, 'r') as f:
  146. file_lines = f.readlines()
  147. format_check_result = self.__check_file(file_lines, file_path)
  148. if not encoding_check_result or not format_check_result:
  149. logging.error("files format check fail.")
  150. return False
  151. logging.info("files format check success.")
  152. return True
  153. class LicenseCheck:
  154. def __init__(self, file_list):
  155. self.file_list = file_list
  156. def check(self):
  157. current_year = datetime.date.today().year
  158. logging.info("current year: {}".format(current_year))
  159. if len(self.file_list) == 0:
  160. logging.warning("There are no files to check license.")
  161. return 0
  162. logging.info("Start to check files license.")
  163. check_result = True
  164. for file_path in self.file_list:
  165. if file_path.endswith(".c") or file_path.endswith(".h"):
  166. try:
  167. with open(file_path, 'r') as f:
  168. file = f.readlines()
  169. except Exception as e:
  170. logging.error(e)
  171. else:
  172. continue
  173. if 'Copyright' in file[1] and 'SPDX-License-Identifier: Apache-2.0' in file[3]:
  174. try:
  175. license_year = re.search(r'2006-\d{4}', file[1]).group()
  176. true_year = '2006-{}'.format(current_year)
  177. if license_year != true_year:
  178. logging.warning("[{0}]: license year: {} is not true: {}, please update.".format(file_path,
  179. license_year,
  180. true_year))
  181. else:
  182. logging.info("[{0}]: license check success.".format(file_path))
  183. except Exception as e:
  184. logging.error(e)
  185. else:
  186. logging.error("[{0}]: license check fail.".format(file_path))
  187. check_result = False
  188. return check_result
  189. @click.group()
  190. @click.pass_context
  191. def cli(ctx):
  192. pass
  193. @cli.command()
  194. @click.option(
  195. '--license',
  196. "check_license",
  197. required=False,
  198. type=click.BOOL,
  199. flag_value=True,
  200. help="Enable File license check.",
  201. )
  202. @click.argument(
  203. 'repo',
  204. nargs=1,
  205. type=click.STRING,
  206. default='https://github.com/RT-Thread/rt-thread',
  207. )
  208. @click.argument(
  209. 'branch',
  210. nargs=1,
  211. type=click.STRING,
  212. default='master',
  213. )
  214. def check(check_license, repo, branch):
  215. """
  216. check files license and format.
  217. """
  218. init_logger()
  219. # get modified files list
  220. checkout = CheckOut(repo, branch)
  221. file_list = checkout.get_new_file()
  222. if file_list is None:
  223. logging.error("checkout files fail")
  224. sys.exit(1)
  225. # check modified files format
  226. format_check = FormatCheck(file_list)
  227. format_check_result = format_check.check()
  228. license_check_result = True
  229. if check_license:
  230. license_check = LicenseCheck(file_list)
  231. license_check_result = license_check.check()
  232. if not format_check_result or not license_check_result:
  233. logging.error("file format check or license check fail.")
  234. sys.exit(1)
  235. logging.info("check success.")
  236. sys.exit(0)
  237. if __name__ == '__main__':
  238. cli()