format_ignore.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Copyright (c) 2006-2023, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2023-05-16 dejavudwh the first version
  9. #
  10. import yaml
  11. import logging
  12. import os
  13. import subprocess
  14. def init_logger():
  15. log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
  16. date_format = '%Y-%m-%d %H:%M:%S %a '
  17. logging.basicConfig(level=logging.INFO,
  18. format=log_format,
  19. datefmt=date_format,
  20. )
  21. class CheckOut:
  22. def __init__(self):
  23. pass
  24. def __exclude_file(self, file_path):
  25. dir_number = file_path.split('/')
  26. ignore_path = file_path
  27. # gets the file path depth.
  28. for i in dir_number:
  29. # current directory.
  30. dir_name = os.path.dirname(ignore_path)
  31. ignore_path = dir_name
  32. # judge the ignore file exists in the current directory.
  33. ignore_file_path = os.path.join(dir_name, ".ignore_format.yml")
  34. if not os.path.exists(ignore_file_path):
  35. continue
  36. try:
  37. with open(ignore_file_path) as f:
  38. ignore_config = yaml.safe_load(f.read())
  39. file_ignore = ignore_config.get("file_path", [])
  40. dir_ignore = ignore_config.get("dir_path", [])
  41. except Exception as e:
  42. logging.error(e)
  43. continue
  44. logging.debug("ignore file path: {}".format(ignore_file_path))
  45. logging.debug("file_ignore: {}".format(file_ignore))
  46. logging.debug("dir_ignore: {}".format(dir_ignore))
  47. try:
  48. # judge file_path in the ignore file.
  49. for file in file_ignore:
  50. if file is not None:
  51. file_real_path = os.path.join(dir_name, file)
  52. if file_real_path == file_path:
  53. logging.info("ignore file path: {}".format(file_real_path))
  54. return 0
  55. file_dir_path = os.path.dirname(file_path)
  56. for _dir in dir_ignore:
  57. if _dir is not None:
  58. dir_real_path = os.path.join(dir_name, _dir)
  59. if file_dir_path.startswith(dir_real_path):
  60. logging.info("ignore dir path: {}".format(dir_real_path))
  61. return 0
  62. except Exception as e:
  63. logging.error(e)
  64. continue
  65. return 1
  66. def get_new_file(self):
  67. result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE)
  68. file_list = result.stdout.decode().strip().split('\n')
  69. new_files = []
  70. for line in file_list:
  71. logging.info("modified file -> {}".format(line))
  72. result = self.__exclude_file(line)
  73. if result != 0:
  74. new_files.append(line)
  75. return new_files