gen-version-specific-includes.py 7.4 KB

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