summaryrefslogtreecommitdiffstats
path: root/bin/post_version.py
blob: 9afb37b18a3162bd1f842c5908d4f8e6fc15ce69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# Copyright © 2019 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Update the main page, release notes, and calendar."""

import calendar
import datetime
import pathlib
from lxml import (
    etree,
    html,
)


def calculate_previous_version(version: str, is_point: bool) -> str:
    """Calculate the previous version to compare to.

    In the case of -rc to final that verison is the previous .0 release,
    (19.3.0 in the case of 20.0.0, for example). for point releases that is
    the last point release. This value will be the same as the input value
    for a poiont release, but different for a major release.
    """
    if '-' in version:
        version = version.split('-')[0]
    if is_point:
        return version
    base = version.split('.')
    if base[1] == '0':
        base[0] = str(int(base[0]) - 1)
        base[1] = '3'
    else:
        base[1] = str(int(base[1]) - 1)
    return '.'.join(base)


def get_version() -> str:
    v = pathlib.Path(__file__).parent.parent / 'VERSION'
    with v.open('rt') as f:
        raw_version = f.read().strip()
    return raw_version.split('-')[0]


def is_point_release() -> bool:
    v = pathlib.Path(__file__).parent.parent / 'VERSION'
    with v.open('rt') as f:
        raw_version = f.read().strip()
    return '-rc' not in raw_version


def update_index(is_point: bool, version: str, previous_version: str) -> None:
    p = pathlib.Path(__file__).parent.parent / 'docs' / 'index.html'
    with p.open('rt') as f:
        tree = html.parse(f)

    news = tree.xpath('.//h1')[0]

    date = datetime.date.today()
    month = calendar.month_name[date.month]
    header = etree.Element('h2')
    header.text=f"{month} {date.day}, {date.year}"

    body = etree.Element('p')
    a = etree.SubElement(body, 'a', attrib={'href': f'relnotes/{previous_version}'})
    a.text = f"Mesa {previous_version}"
    if is_point:
        a.tail = " is released. This is a bug fix release."
    else:
        a.tail = (" is released. This is a new development release. "
                  "See the release notes for mor information about this release.")

    root = news.getparent()
    index = root.index(news) + 1
    root.insert(index, body)
    root.insert(index, header)

    tree.write(p.as_posix(), method='html')


def update_release_notes(previous_version: str) -> None:
    p = pathlib.Path(__file__).parent.parent / 'docs' / 'relnotes.html'
    with p.open('rt') as f:
        tree = html.parse(f)

    li = etree.Element('li')
    a = etree.SubElement(li, 'a', href=f'relnotes/{previous_version}')
    a.text = f'{previous_version} release notes'

    ul = tree.xpath('.//ul')[0]
    ul.insert(0, li)

    tree.write(p.as_posix(), method='html')


def main() -> None:
    is_point = is_point_release()
    version = get_version()
    previous_version = calculate_previous_version(version, is_point)

    update_index(is_point, version, previous_version)
    update_release_notes(previous_version)


if __name__ == "__main__":
    main()