gen-version-specific-includes.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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": {
  16. "template": """
  17. To obtain a local copy: open terminal, navigate to the directory you want to put ESP-IDF, and clone the repository using ``git clone`` command::
  18. cd ~/esp
  19. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  20. ESP-IDF will be downloaded into ``~/esp/esp-idf``.
  21. .. note::
  22. %(extra_note)s
  23. .. note::
  24. %(zipfile_note)s
  25. """,
  26. "master": 'This command will clone the master branch, which has the latest development ("bleeding edge") '
  27. 'version of ESP-IDF. It is fully functional and updated on weekly basis with the most recent features and bugfixes.',
  28. "branch": 'The ``git clone`` option ``-b %(clone_arg)s`` tells git to clone the %(ver_type)s in the ESP-IDF repository '
  29. 'corresponding to this version of the documentation.',
  30. "zipfile": {
  31. "stable": 'As a fallback, it is also possible to download a zip file of this stable release from the `Releases page`_. '
  32. 'Do not download the "Source code" zip file(s) generated automatically by GitHub, they do not work with ESP-IDF.',
  33. "unstable": 'GitHub\'s "Download zip file" feature does not work with ESP-IDF, a ``git clone`` is required. As a fallback, '
  34. '`Stable version`_ can be installed without Git.'
  35. }, # zipfile
  36. }, # git-clone
  37. "version-note": {
  38. "master": """
  39. .. note::
  40. This is documentation for the master branch (latest version) of ESP-IDF. This version is under continual development.
  41. `Stable version`_ documentation is available, as well as other :doc:`/versions`.
  42. """,
  43. "stable": """
  44. .. note::
  45. This is documentation for stable version %s of ESP-IDF. Other :doc:`/versions` are also available.
  46. """,
  47. "branch": """
  48. .. note::
  49. This is documentation for %s ``%s`` of ESP-IDF. Other :doc:`/versions` are also available.
  50. """
  51. }, # version-note
  52. }, # en
  53. "zh_CN": {
  54. "git-clone": {
  55. "template": """
  56. 获取本地副本:打开终端,切换到你要存放 ESP-IDF 的工作目录,使用 ``git clone`` 命令克隆远程仓库::
  57. cd ~/esp
  58. git clone %(clone_args)s--recursive https://github.com/espressif/esp-idf.git
  59. ESP-IDF 将会被下载到 ``~/esp/esp-idf`` 目录下。
  60. .. note::
  61. %(extra_note)s
  62. .. note::
  63. %(zipfile_note)s
  64. """,
  65. "master": '此命令将克隆 master 分支,该分支保存着 ESP-IDF 的最新版本,它功能齐全,每周都会更新一些新功能并修正一些错误。',
  66. "branch": '``git clone`` 命令的 ``-b %(clone_arg)s`` 选项告诉 git 从 ESP-IDF 仓库中克隆与此版本的文档对应的分支。',
  67. "zipfile": {
  68. "stable": '作为备份,还可以从 `Releases page`_ 下载此稳定版本的 zip 文件。不要下载由 GitHub 自动生成的"源代码"的 zip 文件,它们不适用于 ESP-IDF。',
  69. "unstable": 'GitHub 中"下载 zip 文档"的功能不适用于 ESP-IDF,所以需要使用 ``git clone`` 命令。作为备份,可以在没有安装 Git 的环境中下载 '
  70. '`Stable version`_ 的 zip 归档文件。'
  71. }, # zipfile
  72. }, # git-clone
  73. "version-note": {
  74. "master": """
  75. .. note::
  76. 这是ESP-IDF master 分支(最新版本)的文档,该版本在持续开发中。还有 `Stable version`_ 的文档,以及其他版本的文档 :doc:`/versions` 供参考。
  77. This is documentation for the master branch (latest version) of ESP-IDF. This version is under continual development. `Stable version`_ documentation is '
  78. 'available, as well as other :doc:`/versions`.
  79. """,
  80. "stable": """
  81. .. note::
  82. 这是ESP-IDF 稳定版本 %s 的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  83. """,
  84. "branch": """
  85. .. note::
  86. 这是ESP-IDF %s ``%s`` 版本的文档,还有其他版本的文档 :doc:`/versions` 供参考。
  87. """
  88. }, # version-note
  89. } # zh_CN
  90. }
  91. def main():
  92. if len(sys.argv) != 3:
  93. print("Usage: gen-git-clone.py <language> <output file path>")
  94. sys.exit(1)
  95. language = sys.argv[1]
  96. out_dir = sys.argv[2]
  97. if not os.path.exists(out_dir):
  98. print("Creating directory %s" % out_dir)
  99. os.mkdir(out_dir)
  100. template = TEMPLATES[language]
  101. version, ver_type, is_stable = get_version()
  102. write_git_clone_inc(template["git-clone"], out_dir, version, ver_type, is_stable)
  103. write_version_note(template["version-note"], out_dir, version, ver_type, is_stable)
  104. print("Done")
  105. def write_git_clone_inc(template, out_dir, version, ver_type, is_stable):
  106. zipfile = template["zipfile"]
  107. if version == "master":
  108. args = {
  109. "clone_args": "",
  110. "extra_note": template["master"],
  111. "zipfile_note": zipfile["unstable"]
  112. }
  113. else:
  114. args = {
  115. "clone_args": "-b %s " % version,
  116. "extra_note": template["branch"] % {"clone_arg": version, "ver_type": ver_type},
  117. "zipfile_note": zipfile["stable"] if is_stable else zipfile["unstable"]
  118. }
  119. out_file = os.path.join(out_dir, "git-clone.inc")
  120. with open(out_file, "w", encoding='utf-8') as f:
  121. f.write(template["template"] % args)
  122. print("%s written" % out_file)
  123. def write_version_note(template, out_dir, version, ver_type, is_stable):
  124. if version == "master":
  125. content = template["master"]
  126. elif ver_type == "tag" and is_stable:
  127. content = template["stable"] % version
  128. else:
  129. content = template["branch"] % (ver_type, version)
  130. out_file = os.path.join(out_dir, "version-note.inc")
  131. with open(out_file, "w", encoding='utf-8') as f:
  132. f.write(content)
  133. print("%s written" % out_file)
  134. def get_version():
  135. """
  136. Returns a tuple of (name of branch/tag, type branch/tag, is_stable)
  137. """
  138. # Trust what RTD says our version is, if it is set
  139. version = os.environ.get("READTHEDOCS_VERSION", None)
  140. if version == "latest":
  141. return ("master", "branch", False)
  142. # Otherwise, use git to look for a tag
  143. try:
  144. tag = subprocess.check_output(["git", "describe", "--tags", "--exact-match"]).strip()
  145. is_stable = re.match(r"v[0-9\.]+$", tag) is not None
  146. return (tag, "tag", is_stable)
  147. except subprocess.CalledProcessError:
  148. pass
  149. # No tag, look for a branch
  150. refs = subprocess.check_output(["git", "for-each-ref", "--points-at", "HEAD", "--format", "%(refname)"])
  151. print("refs:\n%s" % refs)
  152. refs = refs.split(b"\n")
  153. # Note: this looks for branches in 'origin' because GitLab CI doesn't check out a local branch
  154. branches = [r.replace(b"refs/remotes/origin/",b"").strip() for r in refs if r.startswith(b"refs/remotes/origin/")]
  155. if len(branches) == 0:
  156. # last resort, return the commit (may happen on Gitlab CI sometimes, unclear why)
  157. return (subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip(), "commit", False)
  158. if "master" in branches:
  159. return ("master", "branch", False)
  160. else:
  161. return (branches[0], "branch", False) # take whatever the first branch is
  162. if __name__ == "__main__":
  163. main()