From 910079e4a7900cb60fbfff9aa4ae03a6b1838e6d Mon Sep 17 00:00:00 2001 From: Erik Skultety Date: Thu, 8 Jun 2023 15:32:36 +0200 Subject: [PATCH] setup: styling: Use context managers more Some of the operations, namely file operations and spawning processes can utilize the power of context managers. Use them more, use them together. Signed-off-by: Erik Skultety --- setup.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 1569d95..a892d26 100755 --- a/setup.py +++ b/setup.py @@ -217,9 +217,9 @@ class my_sdist(sdist): f2.close() def gen_changelog(self): - f2 = open("ChangeLog", "w") cmd = "git log '--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n'".split(" ") - with subprocess.Popen(cmd, + with open("ChangeLog", "w") as f_out, \ + subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True) as p: @@ -227,13 +227,12 @@ class my_sdist(sdist): m = re.match(r"([a-f0-9]+):(\d+)\s(.*)", line) if m: t = time.gmtime(int(m.group(2))) - f2.write("%04d-%02d-%02d %s\n" % (t.tm_year, t.tm_mon, t.tm_mday, m.group(3))) + fmt = "{: 04d}-{: 02d}-{: 02d} {}\n" + f_out.write(fmt.format(t.tm_year, t.tm_mon, t.tm_mday, m.group(3))) else: if re.match(r"Signed-off-by", line): continue - f2.write(" " + line.strip() + "\n") - - f2.close() + f_out.write(" " + line.strip() + "\n") def run(self): Path("build").mkdir(exist_ok=True)