collect_projects.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. # Copyright (c) OpenMMLab. All rights reserved.
  3. import os
  4. import os.path as osp
  5. import re
  6. from glob import glob
  7. def _get_project_docs():
  8. """Get all project document files.
  9. Returns:
  10. list[str]: file paths
  11. """
  12. project_root = osp.join('..', '..', 'projects')
  13. pattern = osp.sep.join(['*'] * 2) + '.md'
  14. docs = glob(osp.join(project_root, pattern))
  15. docs = [
  16. doc for doc in docs
  17. if 'example_project' not in doc and '_CN' not in doc
  18. ]
  19. return docs
  20. def _parse_project_doc_path(fn):
  21. """Get project name and banner from a project reference file.
  22. Returns:
  23. tuple:
  24. - project_name (str)
  25. - project_banner (str)
  26. """
  27. project_banner, project_name = None, None
  28. with open(fn, 'r', encoding='utf-8') as f:
  29. for line in f.readlines():
  30. if re.match('^( )*<img', line) and not project_banner:
  31. project_banner = line
  32. if line.startswith('# ') and not project_name:
  33. project_name = line
  34. if project_name and project_banner:
  35. break
  36. if project_name is None or project_banner is None:
  37. raise ValueError(f'Invalid paper reference file {fn}')
  38. project_name = re.sub(r'^\# ', '', project_name).strip()
  39. project_banner = project_banner.strip()
  40. return project_name, project_banner
  41. def _get_project_intro_doc():
  42. project_intro_doc = []
  43. with open(
  44. osp.join('..', '..', 'projects', 'README.md'), 'r',
  45. encoding='utf-8') as f:
  46. for line in f.readlines():
  47. if line.startswith('# Welcome'):
  48. continue
  49. if './faq.md' in line:
  50. line = line.replace('./faq.md', '#faq')
  51. if 'example_project' in line:
  52. line = line.replace(
  53. './', 'https://github.com/open-mmlab/mmpose/'
  54. 'tree/dev-1.x/projects/')
  55. project_intro_doc.append(line)
  56. if line.startswith('## Project List'):
  57. break
  58. return project_intro_doc
  59. def _get_faq_doc():
  60. faq_doc = []
  61. with open(
  62. osp.join('..', '..', 'projects', 'faq.md'), 'r',
  63. encoding='utf-8') as f:
  64. for line in f.readlines():
  65. if '#' in line:
  66. line = re.sub(r'^(\#+)', r'\g<1>#', line)
  67. faq_doc.append(line)
  68. return faq_doc
  69. def main():
  70. # Build output folders
  71. os.makedirs('projects', exist_ok=True)
  72. # Collect all document contents
  73. project_doc_list = _get_project_docs()
  74. project_lines = []
  75. for path in project_doc_list:
  76. name, banner = _parse_project_doc_path(path)
  77. _path = path.split(osp.sep)
  78. _rel_path = _path[_path.index('projects'):-1]
  79. url = 'https://github.com/open-mmlab/mmpose/blob/dev-1.x/' + '/'.join(
  80. _rel_path)
  81. _name = name.split(':', 1)
  82. name, description = _name[0], '' if len(
  83. _name) < 2 else f': {_name[-1]}'
  84. project_lines += [
  85. f'- **{name}**{description} [\\[github\\]]({url})', '',
  86. '<div align="center">', ' ' + banner, '</div>', '<br/>', ''
  87. ]
  88. project_intro_doc = _get_project_intro_doc()
  89. faq_doc = _get_faq_doc()
  90. with open(
  91. osp.join('projects', 'community_projects.md'), 'w',
  92. encoding='utf-8') as f:
  93. f.write('# Projects of MMPose from Community Contributors\n')
  94. f.write(''.join(project_intro_doc))
  95. f.write('\n'.join(project_lines))
  96. f.write(''.join(faq_doc))
  97. if __name__ == '__main__':
  98. print('collect project documents')
  99. main()