bin/post_version.py: white space fixes
[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(body, 'a', attrib={'href': f'relnotes/{previous_version}'})
73 a.text = f"Mesa {previous_version}"
74 if is_point:
75 a.tail = " is released. This is a bug fix release."
76 else:
77 a.tail = (" is released. This is a new development release. "
78 "See the release notes for mor information about this release.")
79
80 root = news.getparent()
81 index = root.index(news) + 1
82 root.insert(index, body)
83 root.insert(index, header)
84
85 tree.write(p.as_posix(), method='html')
86
87
88 def update_release_notes(previous_version: str) -> None:
89 p = pathlib.Path(__file__).parent.parent / 'docs' / 'relnotes.html'
90 with p.open('rt') as f:
91 tree = html.parse(f)
92
93 li = etree.Element('li')
94 a = etree.SubElement(li, 'a', href=f'relnotes/{previous_version}')
95 a.text = f'{previous_version} release notes'
96
97 ul = tree.xpath('.//ul')[0]
98 ul.insert(0, li)
99
100 tree.write(p.as_posix(), method='html')
101
102
103 def main() -> None:
104 parser = argparse.ArgumentParser()
105 parser.add_argument('version', help="The released version.")
106 args = parser.parse_args()
107
108 is_point = is_point_release(args.version)
109 previous_version = calculate_previous_version(args.version, is_point)
110
111 update_index(is_point, args.version, previous_version)
112 update_release_notes(previous_version)
113
114
115 if __name__ == "__main__":
116 main()