gen_version_specific_includes.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Sphinx extension to generate ReSTructured Text .inc snippets
  5. # with version-based content for this IDF version
  6. from __future__ import print_function, unicode_literals
  7. import os
  8. import re
  9. import subprocess
  10. from io import open
  11. from .util import copy_if_modified
  12. TEMPLATES = {
  13. 'en': {
  14. 'git-clone-bash': """
  15. .. code-block:: bash
  16. mkdir -p ~/esp
  17. cd ~/esp
  18. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  19. """,
  20. 'git-clone-windows': """
  21. .. code-block:: batch
  22. mkdir %%userprofile%%\\esp
  23. cd %%userprofile%%\\esp
  24. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  25. """,
  26. 'git-clone-notes': {
  27. 'template': """
  28. .. note::
  29. %(extra_note)s
  30. .. note::
  31. %(zipfile_note)s
  32. """,
  33. 'master': 'This command will clone the master branch, which has the latest development ("bleeding edge") '
  34. 'version of ESP-IDF. It is fully functional and updated on weekly basis with the most recent features and bugfixes.',
  35. 'branch': 'The ``git clone`` option ``-b %(clone_arg)s`` tells git to clone the %(ver_type)s in the ESP-IDF repository ``git clone`` '
  36. 'corresponding to this version of the documentation.',
  37. 'zipfile': {
  38. 'stable': 'As a fallback, it is also possible to download a zip file of this stable release from the `Releases page`_. '
  39. 'Do not download the "Source code" zip file(s) generated automatically by GitHub, they do not work with ESP-IDF.',
  40. 'unstable': 'GitHub\'s "Download zip file" feature does not work with ESP-IDF, a ``git clone`` is required. As a fallback, '
  41. '`Stable version`_ can be installed without Git.'
  42. }, # zipfile
  43. }, # git-clone-notes
  44. 'version-note': {
  45. 'master': """
  46. .. note::
  47. This is documentation for the master branch (latest version) of ESP-IDF. This version is under continual development.
  48. `Stable version`_ documentation is available, as well as other :doc:`/versions`.
  49. """,
  50. 'stable': """
  51. .. note::
  52. This is documentation for stable version %s of ESP-IDF. Other :doc:`/versions` are also available.
  53. """,
  54. 'branch': """
  55. .. note::
  56. This is documentation for %s ``%s`` of ESP-IDF. Other :doc:`/versions` are also available.
  57. """
  58. }, # version-note
  59. }, # en
  60. 'zh_CN': {
  61. 'git-clone-bash': """
  62. .. code-block:: bash
  63. mkdir -p ~/esp
  64. cd ~/esp
  65. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  66. """,
  67. 'git-clone-windows': """
  68. .. code-block:: batch
  69. mkdir %%userprofile%%\\esp
  70. cd %%userprofile%%\\esp
  71. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  72. """,
  73. 'git-clone-notes': {
  74. 'template': """
  75. .. note::
  76. %(extra_note)s
  77. .. note::
  78. %(zipfile_note)s
  79. """,
  80. 'master': '此命令将克隆 master 分支,该分支保存着 ESP-IDF 的最新版本,它功能齐全,每周都会更新一些新功能并修正一些错误。',
  81. 'branch': '``git clone`` 命令的 ``-b %(clone_arg)s`` 选项告诉 git 从 ESP-IDF 仓库中克隆与此版本的文档对应的分支。',
  82. 'zipfile': {
  83. 'stable': '作为备份,还可以从 `Releases page`_ 下载此稳定版本的 zip 文件。不要下载由 GitHub 自动生成的"源代码"的 zip 文件,它们不适用于 ESP-IDF。',
  84. 'unstable': 'GitHub 中"下载 zip 文档"的功能不适用于 ESP-IDF,所以需要使用 ``git clone`` 命令。作为备份,可以在没有安装 Git 的环境中下载 '
  85. '`Stable version`_ 的 zip 归档文件。'
  86. }, # zipfile
  87. }, # git-clone
  88. 'version-note': {
  89. 'master': """
  90. .. note::
  91. 这是ESP-IDF master 分支(最新版本)的文档,该版本在持续开发中。还有 `Stable version`_ 的文档,以及其他版本的文档 :doc:`/versions` 供参考。
  92. """,
  93. 'stable': """
  94. .. note::
  95. 这是ESP-IDF 稳定版本 %s 的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  96. """,
  97. 'branch': """
  98. .. note::
  99. 这是ESP-IDF %s ``%s`` 版本的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  100. """
  101. }, # version-note
  102. } # zh_CN
  103. }
  104. def setup(app):
  105. # doesn't need to be this event specifically, but this is roughly the right time
  106. app.connect('idf-info', generate_version_specific_includes)
  107. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}
  108. def generate_version_specific_includes(app, project_description):
  109. language = app.config.language
  110. tmp_out_dir = os.path.join(app.config.build_dir, 'version_inc')
  111. if not os.path.exists(tmp_out_dir):
  112. print('Creating directory %s' % tmp_out_dir)
  113. os.mkdir(tmp_out_dir)
  114. template = TEMPLATES[language]
  115. version, ver_type, is_stable = get_version()
  116. write_git_clone_inc_files(template, tmp_out_dir, version, ver_type, is_stable)
  117. write_version_note(template['version-note'], tmp_out_dir, version, ver_type, is_stable)
  118. copy_if_modified(tmp_out_dir, os.path.join(app.config.build_dir, 'inc'))
  119. print('Done')
  120. def write_git_clone_inc_files(templates, out_dir, version, ver_type, is_stable):
  121. def out_file(basename):
  122. p = os.path.join(out_dir, '%s.inc' % basename)
  123. print('Writing %s...' % p)
  124. return p
  125. if version == 'master':
  126. clone_args = ''
  127. else:
  128. clone_args = '-b %s ' % version
  129. with open(out_file('git-clone-bash'), 'w', encoding='utf-8') as f:
  130. f.write(templates['git-clone-bash'] % locals())
  131. with open(out_file('git-clone-windows'), 'w', encoding='utf-8') as f:
  132. f.write(templates['git-clone-windows'] % locals())
  133. with open(out_file('git-clone-notes'), 'w', encoding='utf-8') as f:
  134. template = templates['git-clone-notes']
  135. zipfile = template['zipfile']
  136. if version == 'master':
  137. extra_note = template['master']
  138. zipfile_note = zipfile['unstable']
  139. else:
  140. extra_note = template['branch'] % {'clone_arg': version, 'ver_type': ver_type}
  141. zipfile_note = zipfile['stable'] if is_stable else zipfile['unstable']
  142. f.write(template['template'] % locals())
  143. print('Wrote git-clone-xxx.inc files')
  144. def write_version_note(template, out_dir, version, ver_type, is_stable):
  145. if version == 'master':
  146. content = template['master']
  147. elif ver_type == 'tag' and is_stable:
  148. content = template['stable'] % version
  149. else:
  150. content = template['branch'] % (ver_type, version)
  151. out_file = os.path.join(out_dir, 'version-note.inc')
  152. with open(out_file, 'w', encoding='utf-8') as f:
  153. f.write(content)
  154. print('%s written' % out_file)
  155. def get_version():
  156. """
  157. Returns a tuple of (name of branch/tag/commit-id, type branch/tag/commit, is_stable)
  158. """
  159. # Use git to look for a tag
  160. try:
  161. tag = subprocess.check_output(['git', 'describe', '--exact-match']).strip().decode('utf-8')
  162. is_stable = re.match(r'v[0-9\.]+$', tag) is not None
  163. return (tag, 'tag', is_stable)
  164. except subprocess.CalledProcessError:
  165. pass
  166. # No tag, look at branch name from CI, this will give the correct branch name even if the ref for the branch we
  167. # merge into has moved forward before the pipeline runs
  168. branch = os.environ.get('CI_COMMIT_REF_NAME', None)
  169. if branch is not None:
  170. return (branch, 'branch', False)
  171. # Try to find the branch name even if docs are built locally
  172. branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode('utf-8')
  173. if branch != 'HEAD':
  174. return (branch, 'branch', False)
  175. # As a last resort we return commit SHA-1, should never happen in CI/docs that should be published
  176. return (subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('utf-8'), 'commit', False)