|
| 1 | +# This file is part of arduino-cli. |
| 2 | + |
| 3 | +# Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | + |
| 5 | +# This software is released under the GNU General Public License version 3, |
| 6 | +# which covers the main part of arduino-cli. |
| 7 | +# The terms of this license can be found at: |
| 8 | +# https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | + |
| 10 | +# You can be released from the requirements of the above licenses by purchasing |
| 11 | +# a commercial license. Buying such a license is mandatory if you want to |
| 12 | +# modify or otherwise use the software for commercial activities involving the |
| 13 | +# Arduino software without disclosing the source code of your own applications. |
| 14 | +# To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import re |
| 18 | +import unittest |
| 19 | +import subprocess |
| 20 | + |
| 21 | +import semver |
| 22 | +from git import Repo |
| 23 | + |
| 24 | + |
| 25 | +class TestScript(unittest.TestCase): |
| 26 | + def test_get_docs_version(self): |
| 27 | + ver, alias = get_docs_version("master", []) |
| 28 | + self.assertEqual(ver, "dev") |
| 29 | + self.assertEqual(alias, "") |
| 30 | + |
| 31 | + release_names = ["1.4.x", "0.13.x"] |
| 32 | + ver, alias = get_docs_version("0.13.x", release_names) |
| 33 | + self.assertEqual(ver, "0.13") |
| 34 | + self.assertEqual(alias, "") |
| 35 | + ver, alias = get_docs_version("1.4.x", release_names) |
| 36 | + self.assertEqual(ver, "1.4") |
| 37 | + self.assertEqual(alias, "latest") |
| 38 | + |
| 39 | + ver, alias = get_docs_version("0.1.x", []) |
| 40 | + self.assertIsNone(ver) |
| 41 | + self.assertIsNone(alias) |
| 42 | + |
| 43 | + |
| 44 | +def get_docs_version(ref_name, release_branches): |
| 45 | + if ref_name == "master": |
| 46 | + return "dev", "" |
| 47 | + |
| 48 | + if ref_name in release_branches: |
| 49 | + # if version is latest, add an alias |
| 50 | + alias = "latest" if ref_name == release_branches[0] else "" |
| 51 | + # strip `.x` suffix from the branch name to get the version: 0.3.x -> 0.3 |
| 52 | + return ref_name[:-2], alias |
| 53 | + |
| 54 | + return None, None |
| 55 | + |
| 56 | + |
| 57 | +def get_rel_branch_names(blist): |
| 58 | + """Get the names of the release branches, sorted from newest to older. |
| 59 | +
|
| 60 | + Only process remote refs so we're sure to get all of them and clean up the |
| 61 | + name so that we have a list of strings like 0.6.x, 0.7.x, ... |
| 62 | + """ |
| 63 | + pattern = re.compile(r"origin/(\d+\.\d+\.x)") |
| 64 | + names = [] |
| 65 | + for b in blist: |
| 66 | + res = pattern.search(b.name) |
| 67 | + if res is not None: |
| 68 | + names.append(res.group(1)) |
| 69 | + |
| 70 | + # Since sorting is stable, first sort by major... |
| 71 | + names = sorted(names, key=lambda x: int(x.split(".")[0]), reverse=True) |
| 72 | + # ...then by minor |
| 73 | + return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True) |
| 74 | + |
| 75 | + |
| 76 | +def main(repo_dir): |
| 77 | + # Git remote must be set to publish docs |
| 78 | + remote = os.environ.get("REMOTE") |
| 79 | + if not remote: |
| 80 | + print("REMOTE env var must be set to publish, running dry mode") |
| 81 | + |
| 82 | + # Get current repo |
| 83 | + repo = Repo(repo_dir) |
| 84 | + |
| 85 | + # Get the list of release branch names |
| 86 | + rel_br_names = get_rel_branch_names(repo.refs) |
| 87 | + |
| 88 | + # Deduce docs version from current branch. Use the 'latest' alias if |
| 89 | + # version is the most recent |
| 90 | + docs_version, alias = get_docs_version(repo.active_branch.name, rel_br_names) |
| 91 | + if docs_version is None: |
| 92 | + print( |
| 93 | + f"Can't get version from current branch '{repo.active_branch}', skip docs generation" |
| 94 | + ) |
| 95 | + return 0 |
| 96 | + |
| 97 | + args = [ |
| 98 | + "task", |
| 99 | + "docs:publish", |
| 100 | + f"DOCS_REMOTE={remote}", |
| 101 | + f"DOCS_VERSION={docs_version}", |
| 102 | + f"DOCS_ALIAS={alias}", |
| 103 | + ] |
| 104 | + if remote: |
| 105 | + subprocess.run(args, shell=True, check=True, cwd=repo_dir) |
| 106 | + else: |
| 107 | + print(" ".join(args)) |
| 108 | + |
| 109 | + return 0 |
| 110 | + |
| 111 | + |
| 112 | +# Usage: |
| 113 | +# |
| 114 | +# To run the tests: |
| 115 | +# $python build.py test |
| 116 | +# |
| 117 | +# To run the script (must be run from within the repo tree): |
| 118 | +# $python build.py |
| 119 | +# |
| 120 | +if __name__ == "__main__": |
| 121 | + if len(sys.argv) > 1 and sys.argv[1] == "test": |
| 122 | + unittest.main(argv=[""], exit=False) |
| 123 | + sys.exit(0) |
| 124 | + |
| 125 | + here = os.path.dirname(os.path.realpath(__file__)) |
| 126 | + sys.exit(main(os.path.join(here, ".."))) |
0 commit comments