bin/post_version.py: Update the release calendar as well
[mesa.git] / bin / post_version.py
1 #!/usr/bin/env python3
2 # Copyright © 2019 Intel Corporation
3
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21
22 """Update the main page, release notes, and calendar."""
23
24 import argparse
25 import calendar
26 import datetime
27 import pathlib
28 from lxml import (
29 etree,
30 html,
31 )
32
33
34 def calculate_previous_version(version: str, is_point: bool) -> str:
35 """Calculate the previous version to compare to.
36
37 In the case of -rc to final that verison is the previous .0 release,
38 (19.3.0 in the case of 20.0.0, for example). for point releases that is
39 the last point release. This value will be the same as the input value
40 for a poiont release, but different for a major release.
41 """
42 if '-' in version:
43 version = version.split('-')[0]
44 if is_point:
45 return version
46 base = version.split('.')
47 if base[1] == '0':
48 base[0] = str(int(base[0]) - 1)
49 base[1] = '3'
50 else:
51 base[1] = str(int(base[1]) - 1)
52 return '.'.join(base)
53
54
55 def is_point_release(version: str) -> bool:
56 return not version.endswith('.0')
57
58
59 def update_index(is_point: bool, version: str, previous_version: str) -> None:
60 p = pathlib.Path(__file__).parent.parent / 'docs' / 'index.html'
61 with p.open('rt') as f:
62 tree = html.parse(f)
63
64 news = tree.xpath('.//h1')[0]
65
66 date = datetime.date.today()
67 month = calendar.month_name[date.month]
68 header = etree.Element('h2')
69 header.text = f"{month} {date.day}, {date.year}"
70
71 body = etree.Element('p')
72 a = etree.SubElement(
73 body, 'a', attrib={'href': f'relnotes/{previous_version}.html'})
74 a.text = f"Mesa {previous_version}"
75 if is_point:
76 a.tail = " is released. This is a bug fix release."
77 else:
78 a.tail = (" is released. This is a new development release. "
79 "See the release notes for mor information about this release.")
80
81 root = news.getparent()
82 index = root.index(news) + 1
83 root.insert(index, body)
84 root.insert(index, header)
85
86 tree.write(p.as_posix(), method='html')
87
88
89 def update_release_notes(previous_version: str) -> None:
90 p = pathlib.Path(__file__).parent.parent / 'docs' / 'relnotes.html'
91 with p.open('rt') as f:
92 tree = html.parse(f)
93
94 li = etree.Element('li')
95 a = etree.SubElement(li, 'a', href=f'relnotes/{previous_version}.html')
96 a.text = f'{previous_version} release notes'
97
98 ul = tree.xpath('.//ul')[0]
99 ul.insert(0, li)
100
101 tree.write(p.as_posix(), method='html')
102
103
104 def update_calendar(previous_version: str) -> None:
105 p = pathlib.Path(__file__).parent.parent / 'docs' / 'release-calendar.html'
106 with p.open('rt') as f:
107 tree = html.parse(f)
108
109 base_version = previous_version[:-2]
110
111 old = None
112 new = None
113
114 for tr in tree.xpath('.//tr'):
115 if old is not None:
116 new = tr
117 break
118
119 for td in tr.xpath('./td'):
120 if td.text == base_version:
121 old = tr
122 break
123
124 assert old is not None
125 assert new is not None
126 old.getparent().remove(old)
127
128 # rowspan is 1 based in html, but 0 based in lxml
129 rowspan = int(td.get("rowspan")) - 1
130 if rowspan:
131 td.set("rowspan", str(rowspan))
132 new.insert(0, td)
133
134 tree.write(p.as_posix(), method='html')
135
136
137 def main() -> None:
138 parser = argparse.ArgumentParser()
139 parser.add_argument('version', help="The released version.")
140 args = parser.parse_args()
141
142 is_point = is_point_release(args.version)
143 previous_version = calculate_previous_version(args.version, is_point)
144
145 update_index(is_point, args.version, previous_version)
146 update_release_notes(previous_version)
147 update_calendar(previous_version)
148
149
150 if __name__ == "__main__":
151 main()