directory_check.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import sys
  3. import json
  4. import re
  5. directory_file = sys.argv[1]
  6. docs_path = sys.argv[2]
  7. success = True
  8. def check_md_content(md_file):
  9. global success
  10. if not os.path.exists(md_file):
  11. print(f'{md_file} not exists')
  12. success = False
  13. return
  14. md_content = open(md_file, 'r').read()
  15. if 'ee' in directory_file:
  16. md_content = re.sub(r'{% emqxce %}([\s\S]*?){% endemqxce %}', '', md_content)
  17. else:
  18. md_content = re.sub(r'{% emqxee %}([\s\S]*?){% endemqxee %}', '', md_content)
  19. image_list = re.findall('(.*?)!\[(.*?)\]\((.*?)\)', md_content)
  20. url_list = re.findall('(.*?)\[(.*?)\]\((.*?)\)', md_content)
  21. for url in url_list:
  22. if url[0].endswith('!'):
  23. continue
  24. if url[2].startswith(('http://', 'https://', '<', '#', 'mailto:')) or url[2].find('.md') == -1:
  25. continue
  26. url_path = url[2].split('.md')[0]
  27. ref_md_path = os.path.join(f'{"/".join(md_file.split("/")[:-1])}/', f'{url_path}.md')
  28. if not os.path.exists(ref_md_path):
  29. print(f'In {md_file}:', end='')
  30. print(f'{url[2]} not found or not in {directory_file}')
  31. success = False
  32. for image in image_list:
  33. if image[0].startswith('<!--'):
  34. continue
  35. if image[2].startswith(('http://', 'https://', '<')):
  36. continue
  37. image_path = os.path.join(f'{"/".join(md_file.split("/")[:-1])}/', image[2])
  38. if not os.path.exists(image_path):
  39. print(f'In {md_file}:', end='')
  40. print(image[2], 'does not exist')
  41. success = False
  42. def get_md_files(dir_config, path):
  43. global success
  44. md_list = []
  45. for i in dir_config:
  46. md_name = i.get('path')
  47. md_children = i.get('children')
  48. if md_name and md_children:
  49. print(f'{i.get("title")} has path and children')
  50. success = False
  51. if md_children:
  52. md_list += get_md_files(md_children, path)
  53. else:
  54. if md_name.startswith(('http://', 'https://')):
  55. continue
  56. elif md_name == './':
  57. md_list.append(f'{docs_path}/{path}/README.md')
  58. else:
  59. md_list.append(f'{docs_path}/{path}/{md_name}.md')
  60. return list(set(md_list))
  61. if __name__ == '__main__':
  62. if os.path.exists(f'{docs_path}/{directory_file}'):
  63. md_file_list = []
  64. config_dict = json.load(open(f'{docs_path}/{directory_file}'))
  65. md_file_list += get_md_files(config_dict['cn'], 'zh_CN')
  66. md_file_list += get_md_files(config_dict['en'], 'en_US')
  67. for file_path, dir_list, file_list in os.walk(docs_path):
  68. for file_name in file_list:
  69. if file_name.split('.')[-1] != 'md':
  70. continue
  71. md_path = os.path.join(file_path, file_name)
  72. if md_path not in md_file_list:
  73. os.remove(md_path)
  74. for file in md_file_list:
  75. check_md_content(file)
  76. if not success:
  77. sys.exit('No pass!')
  78. else:
  79. print('Check completed!')