|
16 | 16 | ''' |
17 | 17 | Generates release notes for new releases of Meson build system |
18 | 18 | ''' |
| 19 | +import argparse |
19 | 20 | import subprocess |
20 | | -import sys |
21 | | -import os |
22 | | -from glob import glob |
| 21 | +import re |
| 22 | +import shutil |
| 23 | +from pathlib import Path |
23 | 24 |
|
24 | 25 | RELNOTE_TEMPLATE = '''--- |
25 | 26 | title: Release {} |
|
31 | 32 | ''' |
32 | 33 |
|
33 | 34 |
|
34 | | -def add_to_sitemap(from_version, to_version): |
| 35 | +def add_to_sitemap(sitemap, output_sitemap): |
35 | 36 | ''' |
36 | 37 | Adds release note entry to sitemap.txt. |
37 | 38 | ''' |
38 | | - sitemapfile = '../sitemap.txt' |
39 | | - s_f = open(sitemapfile, encoding='utf-8') |
40 | | - lines = s_f.readlines() |
41 | | - s_f.close() |
42 | | - with open(sitemapfile, 'w', encoding='utf-8') as s_f: |
| 39 | + sitemapfile = Path(sitemap) |
| 40 | + with sitemapfile.open(encoding='utf-8') as s_f: |
| 41 | + lines = s_f.readlines() |
| 42 | + relnotes = None |
| 43 | + to_version = None |
| 44 | + output = Path(output_sitemap) |
| 45 | + output.parent.mkdir(exist_ok=True, parents=True) |
| 46 | + with output.open('w', encoding='utf-8') as s_f: |
43 | 47 | for line in lines: |
44 | | - if 'Release-notes' in line and from_version in line: |
45 | | - new_line = line.replace(from_version, to_version) |
46 | | - s_f.write(new_line) |
| 48 | + if relnotes is None: |
| 49 | + m = re.match(r'[\s]*Release-notes-for-([0-9]+)\.([0-9]+)\.([0-9]+)\.md', line) |
| 50 | + if m: |
| 51 | + from_version = f'{m[1]}.{m[2]}.{m[3]}' |
| 52 | + to_version = f'{m[1]}.{int(m[2]) + 1}.{m[3]}' |
| 53 | + new_line = line.replace(from_version, to_version) |
| 54 | + relnotes = new_line.strip() |
| 55 | + s_f.write(new_line) |
47 | 56 | s_f.write(line) |
48 | 57 |
|
49 | | -def generate(from_version, to_version): |
| 58 | + if sitemapfile == output: |
| 59 | + subprocess.check_call(['git', 'add', output]) |
| 60 | + |
| 61 | + return relnotes, to_version |
| 62 | + |
| 63 | +def generate(relnotes, to_version, source_dir, output_dir): |
50 | 64 | ''' |
51 | 65 | Generate notes for Meson build next release. |
52 | 66 | ''' |
53 | | - ofilename = f'Release-notes-for-{to_version}.md' |
54 | | - with open(ofilename, 'w', encoding='utf-8') as ofile: |
55 | | - ofile.write(RELNOTE_TEMPLATE.format(to_version, to_version)) |
56 | | - for snippetfile in glob('snippets/*.md'): |
57 | | - snippet = open(snippetfile, encoding='utf-8').read() |
| 67 | + release = f'{to_version} (in development)' if output_dir else to_version |
| 68 | + output = Path(output_dir, relnotes) if output_dir else Path('markdown', relnotes) |
| 69 | + output.parent.mkdir(exist_ok=True, parents=True) |
| 70 | + with output.open('w', encoding='utf-8') as ofile: |
| 71 | + ofile.write(RELNOTE_TEMPLATE.format(release, to_version)) |
| 72 | + for snippetfile in Path(source_dir, 'markdown/snippets').glob('*.md'): |
| 73 | + snippet = snippetfile.read_text(encoding='utf-8') |
58 | 74 | ofile.write(snippet) |
59 | 75 | if not snippet.endswith('\n'): |
60 | 76 | ofile.write('\n') |
61 | 77 | ofile.write('\n') |
62 | | - subprocess.check_call(['git', 'rm', snippetfile]) |
63 | | - subprocess.check_call(['git', 'add', ofilename]) |
64 | | - add_to_sitemap(from_version, to_version) |
| 78 | + |
| 79 | + if not output_dir: |
| 80 | + subprocess.check_call(['git', 'rm', 'markdown/snippets/*.md']) |
| 81 | + subprocess.check_call(['git', 'add', output]) |
65 | 82 |
|
66 | 83 | if __name__ == '__main__': |
67 | | - if len(sys.argv) != 3: |
68 | | - print(sys.argv[0], 'from_version to_version') |
69 | | - sys.exit(1) |
70 | | - FROM_VERSION = sys.argv[1] |
71 | | - TO_VERSION = sys.argv[2] |
72 | | - os.chdir('markdown') |
73 | | - generate(FROM_VERSION, TO_VERSION) |
| 84 | + parser = argparse.ArgumentParser(description='Generate meson docs') |
| 85 | + parser.add_argument('--input-sitemap', default='sitemap.txt') |
| 86 | + parser.add_argument('--output-sitemap', default='sitemap.txt') |
| 87 | + parser.add_argument('--source-dir', default='.') |
| 88 | + parser.add_argument('--output-dir') |
| 89 | + |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + if Path(args.source_dir, 'markdown/snippets').glob('*.md'): |
| 93 | + relnotes, to_version = add_to_sitemap(args.input_sitemap, args.output_sitemap) |
| 94 | + generate(relnotes, to_version, args.source_dir, args.output_dir) |
| 95 | + elif args.input_sitemap != args.output_sitemap: |
| 96 | + shutil.copyfile(args.input_sitemap, args.output_sitemap) |
0 commit comments