Debian Codegraph @

script/tools/changelog_update_metainfo.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 ###########################################################################
4 # fheroes2: https://github.com/ihhub/fheroes2 #
5 # Copyright (C) 2023 - 2024 #
6 # #
7 # This program is free software; you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation; either version 2 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program; if not, write to the #
19 # Free Software Foundation, Inc., #
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
21 ###########################################################################
22
23 # pylint: disable=missing-module-docstring
24
25 import argparse
26 import datetime
27 import re
28 import sys
29
30 from xml.sax.saxutils import escape
31
32 #
33 # Appstream file can be validated using the following command:
34 # appstream-util validate io.github.ihhub.Fheroes2.metainfo.xml
35 #
36
37
38 class CustomArgumentParser(
39 argparse.ArgumentParser
40 ): # pylint: disable=missing-class-docstring
41 def error(self, message):
42 self.print_usage(sys.stderr)
43 self.exit(
44 1,
45 "%(prog)s: error: %(message)s\n" % {"prog": self.prog, "message": message},
46 )
47
48
49 def parse_arguments(): # pylint: disable=missing-function-docstring
50 parser = CustomArgumentParser()
51
52 parser.add_argument("changelog_file", help="changelog.txt file")
53 parser.add_argument("metainfo_file", help="*.metainfo.xml file")
54
55 return parser.parse_args()
56
57
58 def main(): # pylint: disable=missing-function-docstring
59 args = parse_arguments()
60
61 with open(args.changelog_file, encoding="utf-8") as changelog_file:
62 changelog = changelog_file.read()
63
64 with open(args.metainfo_file, encoding="utf-8") as metainfo_file:
65 metainfo = metainfo_file.read()
66
67 tpl = """
68 <release date="{}" version="v{}">
69 <url>https://github.com/ihhub/fheroes2/releases/tag/{}</url>
70 <description>
71 <p>Changes in v{} ({}):</p>
72 <ul>
73 {}
74 </ul>
75 </description>
76 </release>\n""".lstrip(
77 "\r\n"
78 )
79 tmp = ""
80
81 regex = r"version ([\d.]+) \(([\w ]+)\)\n(.*?)\n{2}"
82 for match in re.findall(regex, changelog, re.MULTILINE | re.DOTALL):
83 tmp += tpl.format(
84 datetime.datetime.strptime(match[1], "%d %B %Y").strftime("%Y-%m-%d"),
85 match[0],
86 match[0],
87 match[0],
88 match[1],
89 re.sub(r"- (.*)", r" <li>\1</li>", escape(match[2])),
90 )
91
92 new_metainfo = re.sub(
93 r"<releases>(.*?)</releases>",
94 r"<releases>\n" + tmp + " </releases>",
95 metainfo,
96 flags=re.MULTILINE | re.DOTALL,
97 )
98
99 with open(args.metainfo_file, "w", encoding="utf-8") as metainfo_file:
100 metainfo_file.write(new_metainfo)
101
102
103 if __name__ == "__main__":
104 main()