gen-version-specific-includes.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Python script to generate ReSTructured Text .inc snippets
  5. # with version-based content for this IDF version
  6. from __future__ import print_function
  7. from __future__ import unicode_literals
  8. from io import open
  9. import subprocess
  10. import os
  11. import sys
  12. import re
  13. TEMPLATES = {
  14. "en": {
  15. "git-clone-bash": """
  16. .. code-block:: bash
  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. cd ~/esp
  64. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  65. """,
  66. "git-clone-windows": """
  67. .. code-block:: batch
  68. mkdir %%userprofile%%\\esp
  69. cd %%userprofile%%\\esp
  70. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  71. """,
  72. "git-clone-notes": {
  73. "template": """
  74. .. note::
  75. %(extra_note)s
  76. .. note::
  77. %(zipfile_note)s
  78. """,
  79. "master": '此命令将克隆 master 分支,该分支保存着 ESP-IDF 的最新版本,它功能齐全,每周都会更新一些新功能并修正一些错误。',
  80. "branch": '``git clone`` 命令的 ``-b %(clone_arg)s`` 选项告诉 git 从 ESP-IDF 仓库中克隆与此版本的文档对应的分支。',
  81. "zipfile": {
  82. "stable": '作为备份,还可以从 `Releases page`_ 下载此稳定版本的 zip 文件。不要下载由 GitHub 自动生成的"源代码"的 zip 文件,它们不适用于 ESP-IDF。',
  83. "unstable": 'GitHub 中"下载 zip 文档"的功能不适用于 ESP-IDF,所以需要使用 ``git clone`` 命令。作为备份,可以在没有安装 Git 的环境中下载 '
  84. '`Stable version`_ 的 zip 归档文件。'
  85. }, # zipfile
  86. }, # git-clone
  87. "version-note": {
  88. "master": """
  89. .. note::
  90. 这是ESP-IDF master 分支(最新版本)的文档,该版本在持续开发中。还有 `Stable version`_ 的文档,以及其他版本的文档 :doc:`/versions` 供参考。
  91. This is documentation for the master branch (latest version) of ESP-IDF. This version is under continual development. `Stable version`_ documentation is '
  92. 'available, as well as other :doc:`/versions`.
  93. """,
  94. "stable": """
  95. .. note::
  96. 这是ESP-IDF 稳定版本 %s 的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  97. """,
  98. "branch": """
  99. .. note::
  100. 这是ESP-IDF %s ``%s`` 版本的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  101. """
  102. }, # version-note
  103. } # zh_CN
  104. }
  105. def main():
  106. if len(sys.argv) != 3:
  107. print("Usage: gen-git-clone.py <language> <output file path>")
  108. sys.exit(1)
  109. language = sys.argv[1]
  110. out_dir = sys.argv[2]
  111. if not os.path.exists(out_dir):
  112. print("Creating directory %s" % out_dir)
  113. os.mkdir(out_dir)
  114. template = TEMPLATES[language]
  115. version, ver_type, is_stable = get_version()
  116. write_git_clone_inc_files(template, out_dir, version, ver_type, is_stable)
  117. write_version_note(template["version-note"], out_dir, version, ver_type, is_stable)
  118. print("Done")
  119. def write_git_clone_inc_files(templates, out_dir, version, ver_type, is_stable):
  120. def out_file(basename):
  121. p = os.path.join(out_dir, "%s.inc" % basename)
  122. print("Writing %s..." % p)
  123. return p
  124. if version == "master":
  125. clone_args = ""
  126. else:
  127. clone_args = "-b %s " % version
  128. with open(out_file("git-clone-bash"), "w", encoding="utf-8") as f:
  129. f.write(templates["git-clone-bash"] % locals())
  130. with open(out_file("git-clone-windows"), "w", encoding="utf-8") as f:
  131. f.write(templates["git-clone-windows"] % locals())
  132. with open(out_file("git-clone-notes"), "w", encoding="utf-8") as f:
  133. template = templates["git-clone-notes"]
  134. zipfile = template["zipfile"]
  135. if version == "master":
  136. extra_note = template["master"]
  137. zipfile_note = zipfile["unstable"]
  138. else:
  139. extra_note = template["branch"] % {"clone_arg": version, "ver_type": ver_type}
  140. zipfile_note = zipfile["stable"] if is_stable else zipfile["unstable"]
  141. f.write(template["template"] % locals())
  142. print("Wrote git-clone-xxx.inc files")
  143. def write_version_note(template, out_dir, version, ver_type, is_stable):
  144. if version == "master":
  145. content = template["master"]
  146. elif ver_type == "tag" and is_stable:
  147. content = template["stable"] % version
  148. else:
  149. content = template["branch"] % (ver_type, version)
  150. out_file = os.path.join(out_dir, "version-note.inc")
  151. with open(out_file, "w", encoding='utf-8') as f:
  152. f.write(content)
  153. print("%s written" % out_file)
  154. def get_version():
  155. """
  156. Returns a tuple of (name of branch/tag/commit-id, type branch/tag/commit, is_stable)
  157. """
  158. # Use git to look for a tag
  159. try:
  160. tag = subprocess.check_output(["git", "describe", "--exact-match"]).strip().decode('utf-8')
  161. is_stable = re.match(r"v[0-9\.]+$", tag) is not None
  162. return (tag, "tag", is_stable)
  163. except subprocess.CalledProcessError:
  164. pass
  165. # No tag, look at branch name from CI, this will give the correct branch name even if the ref for the branch we
  166. # merge into has moved forward before the pipeline runs
  167. branch = os.environ.get("CI_COMMIT_REF_NAME", None)
  168. if branch is not None:
  169. return (branch, "branch", False)
  170. # Try to find the branch name even if docs are built locally
  171. branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip().decode('utf-8')
  172. if branch != "HEAD":
  173. return (branch, "branch", False)
  174. # As a last resort we return commit SHA-1, should never happen in CI/docs that should be published
  175. return (subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip().decode('utf-8'), "commit", False)
  176. if __name__ == "__main__":
  177. main()