directory_check.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(.*?)\)', md_content)
  21. for url in url_list:
  22. if url[2].startswith(('http://', 'https://', '<')):
  23. continue
  24. ref_md_path = os.path.join(f'{"/".join(md_file.split("/")[:-1])}/', f'{url[2]}.md')
  25. if not os.path.exists(ref_md_path):
  26. print(f'In {md_file}:', end='')
  27. print(f'{url[2]}.md', f'not found or not in {directory_file}')
  28. success = False
  29. for image in image_list:
  30. if image[0].startswith('<!--'):
  31. continue
  32. if image[2].startswith(('http://', 'https://', '<')):
  33. continue
  34. image_path = os.path.join(f'{"/".join(md_file.split("/")[:-1])}/', image[2])
  35. if not os.path.exists(image_path):
  36. print(f'In {md_file}:', end='')
  37. print(image[2], 'does not exist')
  38. success = False
  39. def get_md_files(dir_config, path):
  40. global success
  41. md_list = []
  42. for i in dir_config:
  43. md_name = i.get('path')
  44. md_children = i.get('children')
  45. if md_name and md_children:
  46. print(f'{i.get("title")} has path and children')
  47. success = False
  48. if md_children:
  49. md_list += get_md_files(md_children, path)
  50. else:
  51. if md_name.startswith(('http://', 'https://')):
  52. continue
  53. elif md_name == './':
  54. md_list.append(f'{docs_path}/{path}/README.md')
  55. else:
  56. md_list.append(f'{docs_path}/{path}/{md_name}.md')
  57. return list(set(md_list))
  58. if __name__ == '__main__':
  59. if os.path.exists(f'{docs_path}/{directory_file}'):
  60. md_file_list = []
  61. config_dict = json.load(open(f'{docs_path}/{directory_file}'))
  62. md_file_list += get_md_files(config_dict['cn'], 'zh_CN')
  63. md_file_list += get_md_files(config_dict['en'], 'en_US')
  64. for file_path, dir_list, file_list in os.walk(docs_path):
  65. for file_name in file_list:
  66. if file_name.split('.')[-1] != 'md':
  67. continue
  68. md_path = os.path.join(file_path, file_name)
  69. if md_path not in md_file_list:
  70. os.remove(md_path)
  71. for file in md_file_list:
  72. check_md_content(file)
  73. if not success:
  74. sys.exit('No pass!')
  75. else:
  76. print('Check completed!')