Skip to content

Commit

Permalink
Format the Python scripts using black and lint them using pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz committed Oct 14, 2024
1 parent ce0d465 commit f05d126
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 48 deletions.
56 changes: 39 additions & 17 deletions script/tools/changelog_update_metainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,51 @@
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
###########################################################################

# pylint: disable=missing-module-docstring

import argparse
import datetime
import re
import sys

from xml.sax.saxutils import escape

#
# Appstream file could be validated with 'appstream-util validate io.github.ihhub.Fheroes2.metainfo.xml'
# Appstream file can be validated using the following command:
# appstream-util validate io.github.ihhub.Fheroes2.metainfo.xml
#

class CustomArgumentParser(argparse.ArgumentParser):

class CustomArgumentParser(
argparse.ArgumentParser
): # pylint: disable=missing-class-docstring
def error(self, message):
self.print_usage(sys.stderr)
self.exit(1, "%(prog)s: error: %(message)s\n" %
{"prog": self.prog, "message": message})
self.exit(
1,
"%(prog)s: error: %(message)s\n" % {"prog": self.prog, "message": message},
)


def parse_arguments():
def parse_arguments(): # pylint: disable=missing-function-docstring
parser = CustomArgumentParser()

parser.add_argument("changelog_file", help="changelog.txt file")
parser.add_argument("metainfo_file", help="*.metainfo.xml file")

return parser.parse_args()

def main():

def main(): # pylint: disable=missing-function-docstring
args = parse_arguments()

changelog = open(args.changelog_file, encoding='utf-8').read()
metainfo = open(args.metainfo_file, encoding='utf-8').read()
with open(args.changelog_file, encoding="utf-8") as changelog_file:
changelog = changelog_file.read()

with open(args.metainfo_file, encoding="utf-8") as metainfo_file:
metainfo = metainfo_file.read()

tpl = '''
tpl = """
<release date="{}" version="v{}">
<url>https://github.com/ihhub/fheroes2/releases/tag/{}</url>
<description>
Expand All @@ -57,26 +73,32 @@ def main():
{}
</ul>
</description>
</release>\n'''.lstrip("\r\n")
tmp = ''
</release>\n""".lstrip(
"\r\n"
)
tmp = ""

regex = r"version ([\d.]+) \(([\w ]+)\)\n(.*?)[\n]{2}"
regex = r"version ([\d.]+) \(([\w ]+)\)\n(.*?)\n{2}"
for match in re.findall(regex, changelog, re.MULTILINE | re.DOTALL):
tmp += tpl.format(
datetime.datetime.strptime(match[1], '%d %B %Y').strftime('%Y-%m-%d'),
datetime.datetime.strptime(match[1], "%d %B %Y").strftime("%Y-%m-%d"),
match[0],
match[0],
match[0],
match[1],
re.sub(r'- (.*)', r' <li>\1</li>', escape(match[2]))
re.sub(r"- (.*)", r" <li>\1</li>", escape(match[2])),
)

new_metainfo = re.sub(r'<releases>(.*?)</releases>', r'<releases>\n' + tmp + ' </releases>',
new_metainfo = re.sub(
r"<releases>(.*?)</releases>",
r"<releases>\n" + tmp + " </releases>",
metainfo,
flags=re.MULTILINE | re.DOTALL
flags=re.MULTILINE | re.DOTALL,
)

open(args.metainfo_file, 'w', encoding='utf-8').write(new_metainfo)
with open(args.metainfo_file, "w", encoding="utf-8") as metainfo_file:
metainfo_file.write(new_metainfo)


if __name__ == "__main__":
main()
77 changes: 46 additions & 31 deletions script/tools/check_copyright_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
###########################################################################

# pylint: disable=missing-module-docstring

import argparse
import datetime
import os
Expand All @@ -30,59 +32,68 @@
CURRENT_YEAR = datetime.datetime.now().date().year


class CustomArgumentParser(argparse.ArgumentParser):
class CustomArgumentParser(
argparse.ArgumentParser
): # pylint: disable=missing-class-docstring
def error(self, message):
self.print_usage(sys.stderr)
self.exit(1, "%(prog)s: error: %(message)s\n" %
{"prog": self.prog, "message": message})
self.exit(
1,
"%(prog)s: error: %(message)s\n" % {"prog": self.prog, "message": message},
)


def parse_arguments():
def parse_arguments(): # pylint: disable=missing-function-docstring
parser = CustomArgumentParser()

parser.add_argument(
"--handle-shebang",
action="store_true",
help="handle the shebang at the beginning of the source file(s)"
)
parser.add_argument(
"full_header_file",
help="full header file"
)
parser.add_argument(
"header_template_file",
help="header template file"
help="handle the shebang at the beginning of the source file(s)",
)
parser.add_argument("full_header_file", help="full header file")
parser.add_argument("header_template_file", help="header template file")
parser.add_argument(
"source_file",
help="source files (at least one, can be multiple)",
nargs="+"
"source_file", help="source files (at least one, can be multiple)", nargs="+"
)

return parser.parse_args()


def main():
# if something is wrong with the commandline arguments,
# the script exits here with return code 1:
def main(): # pylint: disable=missing-function-docstring,too-many-locals,too-many-branches
args = parse_arguments()

with open(args.full_header_file, "r", encoding="latin_1") as full_header_file:
copyright_hdr_full = full_header_file.read().strip().replace("{YR}", f"{CURRENT_YEAR}")
with open(args.header_template_file, "r", encoding="latin_1") as header_template_file:
copyright_hdr_full = (
full_header_file.read().strip().replace("{YR}", f"{CURRENT_YEAR}")
)

with open(
args.header_template_file, "r", encoding="latin_1"
) as header_template_file:
copyright_hdr_tmpl = header_template_file.read().strip()

copyright_hdr_re_tmpl = copyright_hdr_tmpl

for ch_to_replace in "*()":
copyright_hdr_re_tmpl = copyright_hdr_re_tmpl.replace(ch_to_replace, "\\" + ch_to_replace)
copyright_hdr_re_tmpl = copyright_hdr_re_tmpl.replace(
ch_to_replace, "\\" + ch_to_replace
)

copyright_hdr_year_re = re.compile("^" + copyright_hdr_re_tmpl.replace("{Y1} - {Y2}",
"([0-9]{4}) "))
copyright_hdr_yrng_re = re.compile("^" + copyright_hdr_re_tmpl.replace("{Y1}", "([0-9]{4})")
.replace("{Y2}", "([0-9]{4})"))
copyright_hdr_year_re = re.compile(
"^" + copyright_hdr_re_tmpl.replace("{Y1} - {Y2}", "([0-9]{4}) ")
)
copyright_hdr_yrng_re = re.compile(
"^"
+ copyright_hdr_re_tmpl.replace("{Y1}", "([0-9]{4})").replace(
"{Y2}", "([0-9]{4})"
)
)

for file_name in args.source_file:
if not os.path.exists(file_name):
continue

with open(file_name, "r", encoding="latin_1") as src_file:
src = src_file.read()

Expand All @@ -108,12 +119,14 @@ def main():

if not src_is_ok:
if year_re_match:
hdr = copyright_hdr_tmpl.replace("{Y1}", f"{year_re_match.group(1)}") \
.replace("{Y2}", f"{CURRENT_YEAR}")
hdr = copyright_hdr_tmpl.replace(
"{Y1}", f"{year_re_match.group(1)}"
).replace("{Y2}", f"{CURRENT_YEAR}")
src = copyright_hdr_year_re.sub(hdr, src)
elif yrng_re_match:
hdr = copyright_hdr_tmpl.replace("{Y1}", f"{yrng_re_match.group(1)}") \
.replace("{Y2}", f"{CURRENT_YEAR}")
hdr = copyright_hdr_tmpl.replace(
"{Y1}", f"{yrng_re_match.group(1)}"
).replace("{Y2}", f"{CURRENT_YEAR}")
src = copyright_hdr_yrng_re.sub(hdr, src)
else:
src = copyright_hdr_full + "\n\n" + src.lstrip("\n")
Expand All @@ -124,7 +137,9 @@ def main():
with open(file_name + ".tmp", "x", encoding="latin_1") as tmp_file:
tmp_file.write(src)

with subprocess.Popen(["diff", "-u", file_name, file_name + ".tmp"]) as diff_proc:
with subprocess.Popen(
["diff", "-u", file_name, file_name + ".tmp"]
) as diff_proc:
diff_proc.wait()

os.remove(file_name + ".tmp")
Expand Down

0 comments on commit f05d126

Please sign in to comment.