1#!/usr/bin/env python3
2#
3# Add version information to poky.yaml based upon current git branch/tags
4# Also generate the list of available manuals (releases.rst file)
5#
6# Copyright Linux Foundation
7# Author: Richard Purdie <richard.purdie@linuxfoundation.org>
8# Author: Quentin Schulz <foss@0leil.net>
9#
10# SPDX-License-Identifier: MIT
11#
12
13
14import subprocess
15import collections
16import sys
17import os
18import itertools
19import re
20
21ourversion = None
22if len(sys.argv) == 2:
23    ourversion = sys.argv[1]
24
25ourversion = None
26if len(sys.argv) == 2:
27    ourversion = sys.argv[1]
28
29activereleases = ["mickledore", "kirkstone", "dunfell"]
30devbranch = "nanbield"
31ltsseries = ["kirkstone", "dunfell"]
32
33# used by run-docs-builds to get the default page
34if ourversion == "getlatest":
35    print(activereleases[0])
36    sys.exit(0)
37
38release_series = collections.OrderedDict()
39release_series["nanbield"] = "4.3"
40release_series["mickledore"] = "4.2"
41release_series["langdale"] = "4.1"
42release_series["kirkstone"] = "4.0"
43release_series["honister"] = "3.4"
44release_series["hardknott"] = "3.3"
45release_series["gatesgarth"] = "3.2"
46release_series["dunfell"] = "3.1"
47release_series["zeus"] = "3.0"
48release_series["warrior"] = "2.7"
49release_series["thud"] = "2.6"
50release_series["sumo"] = "2.5"
51release_series["rocko"] = "2.4"
52release_series["pyro"] = "2.3"
53release_series["morty"] = "2.2"
54release_series["krogoth"] = "2.1"
55release_series["jethro"] = "2.0"
56release_series["jethro-pre"] = "1.9"
57release_series["fido"] = "1.8"
58release_series["dizzy"] = "1.7"
59release_series["daisy"] = "1.6"
60release_series["dora"] = "1.5"
61release_series["dylan"] = "1.4"
62release_series["danny"] = "1.3"
63release_series["denzil"] = "1.2"
64release_series["edison"] = "1.1"
65release_series["bernard"] = "1.0"
66release_series["laverne"] = "0.9"
67
68
69bitbake_mapping = {
70    "nanbield" : "2.6",
71    "mickledore" : "2.4",
72    "langdale" : "2.2",
73    "kirkstone" : "2.0",
74    "honister" : "1.52",
75    "hardknott" : "1.50",
76    "gatesgarth" : "1.48",
77    "dunfell" : "1.46",
78}
79
80# 3.4 onwards doesn't have poky version
81# Early 3.4 release docs do reference it though
82poky_mapping = {
83    "3.4" : "26.0",
84    "3.3" : "25.0",
85    "3.2" : "24.0",
86    "3.1" : "23.0",
87}
88
89ourseries = None
90ourbranch = None
91bitbakeversion = None
92docconfver = None
93
94# Test tags exist and inform the user to fetch if not
95try:
96    subprocess.run(["git", "show", "yocto-%s" % release_series[activereleases[0]]], capture_output=True, check=True)
97except subprocess.CalledProcessError:
98    sys.exit("Please run 'git fetch --tags' before building the documentation")
99
100# Try and figure out what we are
101tags = subprocess.run(["git", "tag", "--points-at", "HEAD"], capture_output=True, text=True).stdout
102for t in tags.split():
103    if t.startswith("yocto-"):
104        ourversion = t[6:]
105
106if ourversion:
107    # We're a tagged release
108    components = ourversion.split(".")
109    baseversion = components[0] + "." + components[1]
110    docconfver = ourversion
111    for i in release_series:
112        if release_series[i] == baseversion:
113            ourseries = i
114            ourbranch = i
115            if i in bitbake_mapping:
116                bitbakeversion = bitbake_mapping[i]
117else:
118    # We're floating on a branch
119    branch = subprocess.run(["git", "branch", "--show-current"], capture_output=True, text=True).stdout.strip()
120    ourbranch = branch
121    if branch != "master" and branch not in release_series:
122        # We're not on a known release branch so we have to guess. Compare the numbers of commits
123        # from each release branch and assume the smallest number of commits is the one we're based off
124        possible_branch = None
125        branch_count = 0
126        for b in itertools.chain(release_series.keys(), ["master"]):
127            result = subprocess.run(["git", "log", "--format=oneline",  "HEAD..origin/" + b], capture_output=True, text=True)
128            if result.returncode == 0:
129                count = result.stdout.count('\n')
130                if not possible_branch or count < branch_count:
131                    print("Branch %s has count %s" % (b, count))
132                    possible_branch = b
133                    branch_count = count
134        if possible_branch:
135            branch = possible_branch
136        else:
137            branch = "master"
138        print("Nearest release branch estimated to be %s" % branch)
139    if branch == "master":
140        ourseries = devbranch
141        docconfver = "dev"
142        bitbakeversion = "dev"
143    elif branch in release_series:
144        ourseries = branch
145        if branch in bitbake_mapping:
146            bitbakeversion = bitbake_mapping[branch]
147    else:
148        sys.exit("Unknown series for branch %s" % branch)
149
150    previoustags = subprocess.run(["git", "tag", "--merged", "HEAD"], capture_output=True, text=True).stdout
151    previoustags = [t[6:] for t in previoustags.split() if t.startswith("yocto-" + release_series[ourseries])]
152    futuretags = subprocess.run(["git", "tag", "--merged", ourbranch], capture_output=True, text=True).stdout
153    futuretags = [t[6:] for t in futuretags.split() if t.startswith("yocto-" + release_series[ourseries])]
154
155    # Append .999 against the last known version
156    if len(previoustags) != len(futuretags):
157        ourversion = previoustags[-1] + ".999"
158    else:
159        ourversion = release_series[ourseries] + ".999"
160    if not docconfver:
161        docconfver = ourversion
162
163series = [k for k in release_series]
164previousseries = series[series.index(ourseries)+1:] or [""]
165lastlts = [k for k in previousseries if k in ltsseries] or "dunfell"
166
167print("Version calculated to be %s" % ourversion)
168print("Release series calculated to be %s" % ourseries)
169
170replacements = {
171    "DISTRO" : ourversion,
172    "DISTRO_NAME_NO_CAP" : ourseries,
173    "DISTRO_NAME" : ourseries.capitalize(),
174    "DISTRO_NAME_NO_CAP_MINUS_ONE" : previousseries[0],
175    "DISTRO_NAME_NO_CAP_LTS" : lastlts[0],
176    "YOCTO_DOC_VERSION" : ourversion,
177    "DISTRO_REL_TAG" : "yocto-" + ourversion,
178    "DOCCONF_VERSION" : docconfver,
179    "BITBAKE_SERIES" : bitbakeversion,
180}
181
182if release_series[ourseries] in poky_mapping:
183    pokyversion = poky_mapping[release_series[ourseries]]
184    if ourversion != release_series[ourseries]:
185        pokyversion = pokyversion + "." + ourversion.rsplit(".", 1)[1]
186    else:
187        pokyversion = pokyversion + ".0"
188    replacements["POKYVERSION"] = pokyversion
189
190if os.path.exists("poky.yaml.in"):
191    with open("poky.yaml.in", "r") as r, open("poky.yaml", "w") as w:
192        lines = r.readlines()
193        for line in lines:
194            data = line.split(":")
195            k = data[0].strip()
196            if k in replacements:
197                w.write("%s : \"%s\"\n" % (k, replacements[k]))
198            else:
199                w.write(line)
200
201    print("poky.yaml generated from poky.yaml.in")
202
203
204# In the switcher list of versions we display:
205#  - latest dev
206#  - latest stable release
207#  - latest LTS
208#  - latest for each releases listed as active
209#  - latest doc version in current series
210#  - current doc version
211# (with duplicates removed)
212
213versions = []
214with open("sphinx-static/switchers.js.in", "r") as r, open("sphinx-static/switchers.js", "w") as w:
215    lines = r.readlines()
216    for line in lines:
217        if "ALL_RELEASES_PLACEHOLDER" in line:
218            w.write(str(list(release_series.keys())))
219            continue
220        if "VERSIONS_PLACEHOLDER" in line:
221            w.write("    'dev': { 'title': 'Unstable (dev)', 'obsolete': false,},\n")
222            for branch in activereleases + ([ourseries] if ourseries not in activereleases else []):
223                if branch == devbranch:
224                    continue
225                branch_versions = subprocess.run('git tag --list yocto-%s*' % (release_series[branch]), shell=True, capture_output=True, text=True).stdout.split()
226                branch_versions = sorted([v.replace("yocto-" +  release_series[branch] + ".", "").replace("yocto-" +  release_series[branch], "0") for v in branch_versions], key=int)
227                if not branch_versions:
228                    continue
229                version = release_series[branch]
230                if branch_versions[-1] != "0":
231                    version = version + "." + branch_versions[-1]
232                versions.append(version)
233                w.write("    '%s': {'title': '%s (%s)', 'obsolete': %s,},\n" % (version, branch.capitalize(), version, str(branch not in activereleases).lower()))
234            if ourversion not in versions and ourseries != devbranch:
235                w.write("    '%s': {'title': '%s (%s)', 'obsolete': %s,},\n" % (ourversion, ourseries.capitalize(), ourversion, str(ourseries not in activereleases).lower()))
236        else:
237            w.write(line)
238
239print("switchers.js generated from switchers.js.in")
240
241# generate releases.rst
242
243# list missing tags in yocto-docs
244missing_tags = [
245        'yocto-0.9',
246        'yocto-1.0', 'yocto-1.0.1',
247        'yocto-1.1', 'yocto-1.1.1',
248        'yocto-1.2',
249        'yocto-1.4.4', 'yocto-1.4.5',
250        'yocto-1.5', 'yocto-1.5.2', 'yocto-1.5.3', 'yocto-1.5.4',
251        'yocto-1.6', 'yocto-1.6.1', 'yocto-1.6.2',
252        'yocto-1.7', 'yocto-1.7.1',
253        'yocto-1.9',
254        'yocto-2.5.3',
255        'yocto-3.1', 'yocto-3.1.1', 'yocto-3.1.2', 'yocto-3.1.3',
256        ]
257
258semver = re.compile(r'yocto-(\d+)\.(\d+)(?:\.)?(\d*)')
259
260# git is able to properly order semver versions but not python
261# instead of adding a dependency on semver module, let's convert the version
262# into a decimal number, e.g. 11.23.1 will be 112301 and 1.5 will be 010500 so
263# it can be used as a key for the sorting algorithm.
264# This can be removed once all the old tags are re-created.
265def tag_to_semver_like(v):
266    v_semver = semver.search(v)
267    v_maj, v_min, v_patch = v_semver.groups('0')
268    return int("{:0>2}{:0>2}{:0>2}".format(v_maj, v_min, v_patch), 10)
269
270yocto_tags = subprocess.run(["git", "tag", "--list", "--sort=version:refname", "yocto-*"], capture_output=True, text=True).stdout
271yocto_tags = sorted(yocto_tags.split() + missing_tags, key=tag_to_semver_like)
272tags = [tag[6:] for tag in yocto_tags]
273
274with open('releases.rst', 'w') as f:
275    f.write('===========================\n')
276    f.write(' Supported Release Manuals\n')
277    f.write('===========================\n')
278    f.write('\n')
279
280    for activerelease in activereleases:
281        title = "Release Series %s (%s)" % (release_series[activerelease], activerelease)
282        f.write('*' * len(title) + '\n')
283        f.write(title + '\n')
284        f.write('*' * len(title) + '\n')
285        f.write('\n')
286
287        for tag in tags:
288            if tag == release_series[activerelease] or tag.startswith('%s.' % release_series[activerelease]):
289                f.write('- :yocto_docs:`%s Documentation </%s>`\n' % (tag, tag))
290        f.write('\n')
291
292    f.write('==========================\n')
293    f.write(' Outdated Release Manuals\n')
294    f.write('==========================\n')
295    f.write('\n')
296
297    for series in release_series:
298        if series == devbranch or series in activereleases:
299            continue
300
301        if series == "jethro-pre":
302            continue
303
304        title = "Release Series %s (%s)" % (release_series[series], series)
305        f.write('*' * len(title) + '\n')
306        f.write(title + '\n')
307        f.write('*' * len(title) + '\n')
308        f.write('\n')
309        if series == "jethro":
310            f.write('- :yocto_docs:`1.9 Documentation </1.9>`\n')
311        for tag in tags:
312            if tag == release_series[series] or tag.startswith('%s.' % release_series[series]):
313                f.write('- :yocto_docs:`%s Documentation </%s>`\n' % (tag, tag))
314        f.write('\n')
315