directory_check.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import sys
  3. import json
  4. import re
  5. docs_path = sys.argv[1]
  6. success = True
  7. def check_path(path_list, folder):
  8. global success
  9. for i in path_list:
  10. md_path = i.get('path')
  11. md_children = i.get('children')
  12. if md_path and md_children:
  13. print(f'{i.get("title")} has path and children')
  14. success = False
  15. if md_children:
  16. check_path(md_children, folder)
  17. else:
  18. if md_path.startswith(('http://', 'https://')) or md_path == './':
  19. continue
  20. file_path = f'{docs_path}/{folder}/{md_path}.md'
  21. if not os.path.exists(file_path):
  22. print(f'{folder}/{md_path}.md not exists')
  23. success = False
  24. continue
  25. md_content = open(file_path, 'r').read()
  26. image_list = re.findall('(.*?)!\[(.*?)\]\((.*?)\)', md_content)
  27. for image in image_list:
  28. if image[0].startswith('<!--'):
  29. continue
  30. if image[2].startswith(('http://', 'https://', '<')):
  31. continue
  32. image_path = os.path.join(f'{"/".join(file_path.split("/")[:-1])}/', image[2])
  33. if not os.path.exists(image_path):
  34. print(f'In {folder}/{md_path}.md:', end='')
  35. print(image[2], 'does not exist')
  36. success = False
  37. if __name__ == '__main__':
  38. file_list = []
  39. if os.path.exists(f'{docs_path}/directory.json'):
  40. file_list.append('directory.json')
  41. for file in file_list:
  42. with open(f'{docs_path}/{file}') as f:
  43. print(f'Check {file}...')
  44. config_dict = json.load(f)
  45. check_path(config_dict['cn'], 'zh_CN')
  46. check_path(config_dict['en'], 'en_US')
  47. if not success:
  48. sys.exit('No pass!')
  49. else:
  50. print('Check completed!')