gen-version-specific-includes.py 7.3 KB

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