aboutsummaryrefslogtreecommitdiff
path: root/docs/genrelnotes.py
blob: 0d9f60da5cc38b7c368afbb33d1f1f74441658d7 (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
#!/usr/bin/env python3

# Copyright 2019 The Meson development team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
  Generates release notes for new releases of Meson build system
'''
import argparse
import subprocess
import re
import shutil
from pathlib import Path

RELNOTE_TEMPLATE = '''---
title: Release {}
short-description: Release notes for {}
...

# New features{}

'''


def add_to_sitemap(sitemap, output_sitemap):
    '''
       Adds release note entry to sitemap.txt.
    '''
    sitemapfile = Path(sitemap)
    with sitemapfile.open(encoding='utf-8') as s_f:
        lines = s_f.readlines()
    relnotes = None
    to_version = None
    output = Path(output_sitemap)
    output.parent.mkdir(exist_ok=True, parents=True)
    with output.open('w', encoding='utf-8') as s_f:
        for line in lines:
            if relnotes is None:
                m = re.match(r'[\s]*Release-notes-for-([0-9]+)\.([0-9]+)\.([0-9]+)\.md', line)
                if m:
                    from_version = f'{m[1]}.{m[2]}.{m[3]}'
                    to_version = f'{m[1]}.{int(m[2]) + 1}.{m[3]}'
                    new_line = line.replace(from_version, to_version)
                    relnotes = new_line.strip()
                    s_f.write(new_line)
            s_f.write(line)

    if sitemapfile == output:
        subprocess.check_call(['git', 'add', output])

    return relnotes, to_version

def generate(relnotes, to_version, source_dir, output_dir):
    '''
       Generate notes for Meson build next release.
    '''
    title_suffix = ' (in development)' if output_dir else ''
    title = f'{to_version}{title_suffix}'
    output = Path(output_dir, relnotes) if output_dir else Path('markdown', relnotes)
    output.parent.mkdir(exist_ok=True, parents=True)
    with output.open('w', encoding='utf-8') as ofile:
        ofile.write(RELNOTE_TEMPLATE.format(title, to_version, title_suffix))
        for snippetfile in sorted(Path(source_dir, 'markdown/snippets').glob('*.md')):
            snippet = snippetfile.read_text(encoding='utf-8')
            ofile.write(snippet)
            if not snippet.endswith('\n'):
                ofile.write('\n')
            ofile.write('\n')

    if not output_dir:
        subprocess.check_call(['git', 'rm', 'markdown/snippets/*.md'])
        subprocess.check_call(['git', 'add', output])

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate meson docs')
    parser.add_argument('--input-sitemap', default='sitemap.txt')
    parser.add_argument('--output-sitemap', default='sitemap.txt')
    parser.add_argument('--source-dir', default='.')
    parser.add_argument('--output-dir')

    args = parser.parse_args()

    if Path(args.source_dir, 'markdown/snippets').glob('*.md'):
        relnotes, to_version = add_to_sitemap(args.input_sitemap, args.output_sitemap)
        generate(relnotes, to_version, args.source_dir, args.output_dir)
    elif args.input_sitemap != args.output_sitemap:
        shutil.copyfile(args.input_sitemap, args.output_sitemap)