#!/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.

import sys, os, subprocess
from glob import glob

relnote_template = '''---
title: Release %s
short-description: Release notes for %s
...

# New features

'''


def add_to_sitemap(from_version, to_version):
    sitemapfile = '../sitemap.txt'
    sf = open(sitemapfile)
    lines = sf.readlines()
    sf.close()
    with open(sitemapfile, 'w') as sf:
        for line in lines:
            if 'Release-notes' in line and from_version in line:
                new_line = line.replace(from_version, to_version)
                sf.write(new_line)
            sf.write(line)

def generate(from_version, to_version):
    ofilename = 'Release-notes-for-%s.md' % to_version
    with open(ofilename, 'w') as ofile:
        ofile.write(relnote_template % (to_version, to_version))
        for snippetfile in glob('snippets/*.md'):
            snippet = open(snippetfile).read()
            ofile.write(snippet)
            if not snippet.endswith('\n'):
                ofile.write('\n')
            ofile.write('\n')
            subprocess.check_call(['git', 'rm', snippetfile])
    subprocess.check_call(['git', 'add', ofilename])
    add_to_sitemap(from_version, to_version)

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print(sys.argv[0], 'from_version to_version')
        sys.exit(1)
    from_version = sys.argv[1]
    to_version = sys.argv[2]
    os.chdir('markdown')
    generate(from_version, to_version)

@@ -611,34 +611,118 @@
 se.%0A
-%0Aimport sys, os, subproces
+'''%0A  Generates release notes for new releases of Meson build system%0A'''%0Aimport subprocess%0Aimport sys%0Aimport o
 s%0Afr
@@ -742,32 +742,32 @@
 t glob%0A%0A
-relnote_template
+RELNOTE_TEMPLATE
  = '''--
@@ -783,18 +783,18 @@
 Release 
-%25s
+%7B%7D
 %0Ashort-d
@@ -823,18 +823,18 @@
 tes for 
-%25s
+%7B%7D
 %0A...%0A%0A# 
@@ -895,24 +895,87 @@
 o_version):%0A
+    '''%0A       Adds release note entry to sitemap.txt.%0A    '''%0A
     sitemapf
@@ -1002,16 +1002,17 @@
 t'%0A    s
+_
 f = open
@@ -1038,16 +1038,17 @@
 ines = s
+_
 f.readli
@@ -1058,16 +1058,17 @@
 ()%0A    s
+_
 f.close(
@@ -1105,16 +1105,17 @@
 w') as s
+_
 f:%0A     
@@ -1276,32 +1276,33 @@
                s
+_
 f.write(new_line
@@ -1316,16 +1316,17 @@
        s
+_
 f.write(
@@ -1368,24 +1368,92 @@
 o_version):%0A
+    '''%0A       Generate notes for Meson build next release.%0A    '''%0A
     ofilenam
@@ -1479,17 +1479,22 @@
 for-
-%25s
+%7B%7D
 .md'
- %25 
+.format(
 to_v
@@ -1499,16 +1499,17 @@
 _version
+)
 %0A    wit
@@ -1565,27 +1565,31 @@
 ite(
-relnote_template %25 
+RELNOTE_TEMPLATE.format
 (to_
@@ -2140,28 +2140,28 @@
 (1)%0A    
-from_version
+FROM_VERSION
  = sys.a
@@ -2171,26 +2171,26 @@
 %5B1%5D%0A    
-to_version
+TO_VERSION
  = sys.a
@@ -2238,30 +2238,30 @@
 ate(
-from_version, to_version
+FROM_VERSION, TO_VERSION
 )%0A

Update Python2 syntax to Python3 syntax in genrelnotes.py