From 7a13306c95b1bba657e2a5f48fa4aaab6050c1b1 Mon Sep 17 00:00:00 2001 From: "James D. Mitchell" Date: Wed, 7 Feb 2024 09:05:29 +0000 Subject: [PATCH] Apply pylint changes to dev/release/*.py --- dev/releases/make_archives.py | 33 +++++-- dev/releases/make_github_release.py | 4 +- dev/releases/release_notes.py | 138 ++++++++++++++-------------- dev/releases/update_website.py | 24 ++--- dev/releases/utils.py | 27 +++--- dev/releases/utils_github.py | 26 +++--- 6 files changed, 135 insertions(+), 117 deletions(-) diff --git a/dev/releases/make_archives.py b/dev/releases/make_archives.py index 4d3065403a..7a8252e319 100755 --- a/dev/releases/make_archives.py +++ b/dev/releases/make_archives.py @@ -17,6 +17,7 @@ import grp import gzip import json +import os import pwd import re import shutil @@ -24,7 +25,19 @@ import sys import tarfile -from utils import * +from utils import ( + download_with_sha256, + error, + get_makefile_var, + notice, + patchfile, + run_with_log, + safe_git_fetch_tags, + verify_command_available, + verify_git_clean, + verify_git_repo, + working_directory, +) # Insist on Python >= 3.6 for f-strings and other goodies if sys.version_info < (3, 6): @@ -56,11 +69,11 @@ error("make sure GAP has been compiled via './configure && make'") notice(f"Detected GAP version {gapversion}") -if re.fullmatch(r"[1-9]+\.[0-9]+\.[0-9]+", gapversion) != None: - notice(f"--- THIS LOOKS LIKE A RELEASE ---") +if re.fullmatch(r"[1-9]+\.[0-9]+\.[0-9]+", gapversion) is not None: + notice("--- THIS LOOKS LIKE A RELEASE ---") pkg_tag = f"v{gapversion}" else: - notice(f"--- THIS LOOKS LIKE A NIGHTLY BUILD ---") + notice("--- THIS LOOKS LIKE A NIGHTLY BUILD ---") pkg_tag = "latest" @@ -195,14 +208,14 @@ subprocess.run(["./configure"], check=True) subprocess.run(["make"], check=True) - notice(f"Constructing help-links JSON file") + notice("Constructing help-links JSON file") json_output = subprocess.run( [ "./gap", "-r", "--quiet", "--quitonbreak", - f"dev/releases/HelpLinks-to-JSON.g", + "dev/releases/HelpLinks-to-JSON.g", ], check=True, capture_output=True, @@ -257,12 +270,12 @@ def make_and_record_archive( else: error(f"unknown compression type {compression} (not gztar or zip)") - filename = f"{name}{ext}" - notice(f"Creating {filename}") + fname = f"{name}{ext}" + notice(f"Creating {fname}") owner = pwd.getpwuid(0).pw_name group = grp.getgrgid(0).gr_name shutil.make_archive(name, compression, root_dir, base_dir, owner=owner, group=group) - manifest_list.append(filename) + manifest_list.append(fname) # Create the remaining archives @@ -280,7 +293,7 @@ def make_and_record_archive( # If you create additional archives, make sure to add them to manifest_list! manifest_filename = "MANIFEST" notice(f"Creating the manifest, with name {manifest_filename}") - with open(manifest_filename, "w") as manifest: + with open(manifest_filename, "w", encoding="utf-8") as manifest: for filename in manifest_list: manifest.write(f"{filename}\n") diff --git a/dev/releases/make_github_release.py b/dev/releases/make_github_release.py index 9d937f692f..8de5f4b8a1 100755 --- a/dev/releases/make_github_release.py +++ b/dev/releases/make_github_release.py @@ -58,7 +58,7 @@ with utils.working_directory(PATH_TO_RELEASE): manifest_filename = "MANIFEST" - with open(manifest_filename, "r") as manifest_file: + with open(manifest_filename, "r", encoding="utf-8") as manifest_file: manifest = manifest_file.read().splitlines() notice(f"Contents of {manifest_filename}:") @@ -67,7 +67,7 @@ # Now check that TAG_NAME and the created archives belong together main_archive_name = "gap-" + VERSION + ".tar.gz" - if not main_archive_name in manifest: + if main_archive_name not in manifest: error(f"Expected to find {main_archive_name} in MANIFEST, but did not!") # Upload all assets to release diff --git a/dev/releases/release_notes.py b/dev/releases/release_notes.py index 0cc1c2823f..40625ee915 100755 --- a/dev/releases/release_notes.py +++ b/dev/releases/release_notes.py @@ -247,86 +247,86 @@ def changes_overview( # Could also introduce some consistency checks here for wrong combinations of labels filename = "releasenotes_" + new_version + ".md" notice("Writing release notes into file " + filename) - relnotes_file = open(filename, "w") - prs_with_use_title = [pr for pr in prs if has_label(pr, "release notes: use title")] - - # Write out all PRs with 'use title' - relnotes_file.write( - f""" + with open(filename, "w", encoding="utf-8") as relnotes_file: + prs_with_use_title = [ + pr for pr in prs if has_label(pr, "release notes: use title") + ] + # Write out all PRs with 'use title' + relnotes_file.write( + f""" ## GAP {new_version} (TODO insert date here, ) -The following gives an overview of the changes compared to the previous -release. This list is not complete, many more internal or minor changes -were made, but we tried to only list those changes which we think might -affect some users directly. - -""" - ) - - for priorityobject in prioritylist: - matches = [pr for pr in prs_with_use_title if has_label(pr, priorityobject[0])] - print("PRs with label '" + priorityobject[0] + "': ", len(matches)) - if len(matches) == 0: - continue - relnotes_file.write("### " + priorityobject[1] + "\n\n") - for pr in matches: - relnotes_file.write(pr_to_md(pr)) - prs_with_use_title.remove(pr) - relnotes_file.write("\n") + The following gives an overview of the changes compared to the previous + release. This list is not complete, many more internal or minor changes + were made, but we tried to only list those changes which we think might + affect some users directly. - # The remaining PRs have no "kind" or "topic" label from the priority list - # (may have other "kind" or "topic" label outside the priority list). - # Check their list in the release notes, and adjust labels if appropriate. - if len(prs_with_use_title) > 0: - relnotes_file.write("### Other changes\n\n") - for pr in prs_with_use_title: - relnotes_file.write(pr_to_md(pr)) - relnotes_file.write("\n") - - package_updates(relnotes_file, new_version) + """ + ) - relnotes_file.close() + for priorityobject in prioritylist: + matches = [ + pr for pr in prs_with_use_title if has_label(pr, priorityobject[0]) + ] + print("PRs with label '" + priorityobject[0] + "': ", len(matches)) + if len(matches) == 0: + continue + relnotes_file.write("### " + priorityobject[1] + "\n\n") + for pr in matches: + relnotes_file.write(pr_to_md(pr)) + prs_with_use_title.remove(pr) + relnotes_file.write("\n") + + # The remaining PRs have no "kind" or "topic" label from the priority list + # (may have other "kind" or "topic" label outside the priority list). + # Check their list in the release notes, and adjust labels if appropriate. + if len(prs_with_use_title) > 0: + relnotes_file.write("### Other changes\n\n") + for pr in prs_with_use_title: + relnotes_file.write(pr_to_md(pr)) + relnotes_file.write("\n") + + package_updates(relnotes_file, new_version) notice("Release notes were written into file " + filename) + with open( + "unsorted_PRs_" + new_version + ".md", "w", encoding="utf-8" + ) as unsorted_file: + # Report PRs that have to be updated before inclusion into release notes. + unsorted_file.write("### " + "release notes: to be added" + "\n\n") + unsorted_file.write( + "If there are any PRs listed below, check their title and labels.\n" + ) + unsorted_file.write( + 'When done, change their label to "release notes: use title".\n\n' + ) - unsorted_file = open("unsorted_PRs_" + new_version + ".md", "w") - - # Report PRs that have to be updated before inclusion into release notes. - unsorted_file.write("### " + "release notes: to be added" + "\n\n") - unsorted_file.write( - "If there are any PRs listed below, check their title and labels.\n" - ) - unsorted_file.write( - 'When done, change their label to "release notes: use title".\n\n' - ) - - for pr in prs: - if has_label(pr, "release notes: to be added"): - unsorted_file.write(pr_to_md(pr)) + for pr in prs: + if has_label(pr, "release notes: to be added"): + unsorted_file.write(pr_to_md(pr)) - prs = [pr for pr in prs if not has_label(pr, "release notes: to be added")] + prs = [pr for pr in prs if not has_label(pr, "release notes: to be added")] - unsorted_file.write("\n") + unsorted_file.write("\n") - # Report PRs that have neither "to be added" nor "added" or "use title" label - unsorted_file.write("### Uncategorized PR" + "\n\n") - unsorted_file.write( - "If there are any PRs listed below, either apply the same steps\n" - ) - unsorted_file.write( - 'as above, or change their label to "release notes: not needed".\n\n' - ) + # Report PRs that have neither "to be added" nor "added" or "use title" label + unsorted_file.write("### Uncategorized PR" + "\n\n") + unsorted_file.write( + "If there are any PRs listed below, either apply the same steps\n" + ) + unsorted_file.write( + 'as above, or change their label to "release notes: not needed".\n\n' + ) - for pr in prs: - # we need to use both old "release notes: added" label and - # the newly introduced in "release notes: use title" label - # since both label may appear in GAP 4.12.0 changes overview - if not ( - has_label(pr, "release notes: added") - or has_label(pr, "release notes: use title") - ): - unsorted_file.write(pr_to_md(pr)) - unsorted_file.close() + for pr in prs: + # we need to use both old "release notes: added" label and + # the newly introduced in "release notes: use title" label + # since both label may appear in GAP 4.12.0 changes overview + if not ( + has_label(pr, "release notes: added") + or has_label(pr, "release notes: use title") + ): + unsorted_file.write(pr_to_md(pr)) def main(new_version: str) -> None: diff --git a/dev/releases/update_website.py b/dev/releases/update_website.py index 3c38bf668f..558b7bab13 100755 --- a/dev/releases/update_website.py +++ b/dev/releases/update_website.py @@ -80,21 +80,21 @@ def download_asset_by_name(asset_name: str, writedir: str) -> None: utils.download_with_sha256(url, asset_name) -def extract_tarball(tarball: str) -> None: - notice(f"Extracting {tarball} . . .") - with tarfile.open(tarball) as tar: +def extract_tarball(tball: str) -> None: + notice(f"Extracting {tball} . . .") + with tarfile.open(tball) as tar: try: tar.extractall() except: - error(f"Failed to extract {tarball}!") + error(f"Failed to extract {tball}!") def get_date_from_configure_ac(gaproot: str) -> str: - with open(f"{gaproot}/configure.ac", "r") as configure_ac: + with open(f"{gaproot}/configure.ac", "r", encoding="utf-8") as configure_ac: filedata = configure_ac.read() try: # Expect date in YYYY-MM-DD format release_date = re.search( - "\[gap_releaseday\], \[(\d{4}-\d{2}-\d{2})\]", filedata + r"\[gap_releaseday\], \[(\d{4}-\d{2}-\d{2})\]", filedata ).group(1) release_date = datetime.datetime.strptime(release_date, "%Y-%m-%d") return release_date.strftime("%d %B %Y") @@ -109,7 +109,7 @@ def download_and_extract_json_gz_asset(asset_name: str, dest: str) -> None: download_asset_by_name(asset_name, tmpdir) with utils.working_directory(tmpdir): with gzip.open(asset_name, "rt", encoding="utf-8") as file_in: - with open(dest, "w") as file_out: + with open(dest, "w", encoding="utf-8") as file_out: shutil.copyfileobj(file_in, file_out) @@ -155,12 +155,12 @@ def download_and_extract_json_gz_asset(asset_name: str, dest: str) -> None: known_release = os.path.isfile(f"_Releases/{version}.html") newest_release = releases.index(release) == 0 if known_release: - notice(f"I have seen this release before") + notice("I have seen this release before") elif newest_release: - notice(f"This is a new release to me, and it has the biggest version number") + notice("This is a new release to me, and it has the biggest version number") else: notice( - f"This is a new release to me, but I know about releases with bigger version numbers" + "This is a new release to me, but I know about releases with bigger version numbers" ) # For all releases, record the assets (in case they were deleted/updated/added) @@ -237,5 +237,7 @@ def download_and_extract_json_gz_asset(asset_name: str, dest: str) -> None: with open(f"{pwd}/_data/package-infos/{version_safe}.json", "rb") as infile: data = json.loads(infile.read()) for pkg in data: - with open(f"{pwd}/_Packages/{pkg}.html", "w+") as pkg_file: + with open( + f"{pwd}/_Packages/{pkg}.html", "w+", encoding="utf-8" + ) as pkg_file: pkg_file.write(f"---\ntitle: {data[pkg]['PackageName']}\n---\n") diff --git a/dev/releases/utils.py b/dev/releases/utils.py index 9516157c87..8814a36367 100644 --- a/dev/releases/utils.py +++ b/dev/releases/utils.py @@ -34,7 +34,7 @@ def error(msg: str) -> NoReturn: def verify_command_available(cmd: str) -> None: - if shutil.which(cmd) == None: + if shutil.which(cmd) is None: error(f"the '{cmd}' command was not found, please install it") # TODO: do the analog of this in ReleaseTools bash script: # command -v curl >/dev/null 2>&1 || @@ -43,7 +43,7 @@ def verify_command_available(cmd: str) -> None: def verify_git_repo() -> None: res = subprocess.run( - ["git", "--git-dir=.git", "rev-parse"], stderr=subprocess.DEVNULL + ["git", "--git-dir=.git", "rev-parse"], stderr=subprocess.DEVNULL, check=False ) if res.returncode != 0: error("current directory is not a git root directory") @@ -51,7 +51,7 @@ def verify_git_repo() -> None: # check for uncommitted changes def is_git_clean() -> bool: - res = subprocess.run(["git", "update-index", "--refresh"]) + res = subprocess.run(["git", "update-index", "--refresh"], check=False) if res.returncode == 0: res = subprocess.run(["git", "diff-index", "--quiet", "HEAD", "--"]) return res.returncode == 0 @@ -97,34 +97,34 @@ def sha256file(path: str) -> str: # read a file into memory, apply some transformations, and write it back def patchfile(path: str, pattern: str, repl: str) -> None: # Read in the file - with open(path, "r") as file: + with open(path, "r", encoding="utf-8") as file: filedata = file.read() # Replace the target string filedata = re.sub(pattern, repl, filedata) # Write the file out again - with open(path, "w") as file: + with open(path, "w", encoding="utf-8") as file: file.write(filedata) # download file at the given URL to path `dst` def download(url: str, dst: str) -> None: notice(f"Downlading {url} to {dst}") - res = subprocess.run(["curl", "-L", "-C", "-", "-o", dst, url]) + res = subprocess.run(["curl", "-L", "-C", "-", "-o", dst, url], check=False) if res.returncode != 0: error("failed downloading " + url) def file_matches_checksumfile(filename: str) -> bool: - with open(filename + ".sha256", "r") as f: + with open(filename + ".sha256", "r", encoding="utf-8") as f: expected_checksum = f.read().strip() return expected_checksum == sha256file(filename) def verify_via_checksumfile(filename: str) -> None: actual_checksum = sha256file(filename) - with open(filename + ".sha256", "r") as f: + with open(filename + ".sha256", "r", encoding="utf-8") as f: expected_checksum = f.read().strip() if expected_checksum != actual_checksum: error( @@ -148,7 +148,7 @@ def download_with_sha256(url: str, dst: str) -> None: def run_with_log(args: List[str], name: str, msg: Optional[str] = None) -> None: if not msg: msg = name - with open("../" + name + ".log", "w") as fp: + with open("../" + name + ".log", "w", encoding="utf-8") as fp: try: subprocess.run(args, check=True, stdout=fp, stderr=fp) except subprocess.CalledProcessError: @@ -156,7 +156,7 @@ def run_with_log(args: List[str], name: str, msg: Optional[str] = None) -> None: def is_possible_gap_release_tag(tag: str) -> bool: - return re.fullmatch(r"v[1-9]+\.[0-9]+\.[0-9]+(-.+)?", tag) != None + return re.fullmatch(r"v[1-9]+\.[0-9]+\.[0-9]+(-.+)?", tag) is not None def verify_is_possible_gap_release_tag(tag: str) -> None: @@ -166,7 +166,7 @@ def verify_is_possible_gap_release_tag(tag: str) -> None: def is_existing_tag(tag: str) -> bool: res = subprocess.run( - ["git", "show-ref", "--quiet", "--verify", "refs/tags/" + tag], + ["git", "show-ref", "--quiet", "--verify", "refs/tags/" + tag], check=False ) return res.returncode == 0 @@ -183,7 +183,10 @@ def safe_git_fetch_tags() -> None: # https://stackoverflow.com/questions/40479712/how-can-i-tell-if-a-given-git-tag-is-annotated-or-lightweight#40499437 def is_annotated_git_tag(tag: str) -> bool: res = subprocess.run( - ["git", "for-each-ref", "refs/tags/" + tag], capture_output=True, text=True + ["git", "for-each-ref", "refs/tags/" + tag], + capture_output=True, + text=True, + check=False, ) return res.returncode == 0 and res.stdout.split()[1] == "tag" diff --git a/dev/releases/utils_github.py b/dev/releases/utils_github.py index 8d7887fc55..5d0d162862 100644 --- a/dev/releases/utils_github.py +++ b/dev/releases/utils_github.py @@ -7,13 +7,8 @@ ## ## SPDX-License-Identifier: GPL-2.0-or-later ## -import contextlib -import hashlib import os -import re -import shutil import subprocess -import sys import github from utils import error, notice, sha256file, verify_via_checksumfile @@ -30,25 +25,30 @@ # GITHUB_TOKEN. def initialize_github(token=None) -> None: global GITHUB_INSTANCE, CURRENT_REPO - if GITHUB_INSTANCE != None or CURRENT_REPO != None: + if GITHUB_INSTANCE is not None or CURRENT_REPO is not None: error( "Global variables GITHUB_INSTANCE and CURRENT_REPO" + " are already initialized." ) - if token == None and "GITHUB_TOKEN" in os.environ: + if token is None and "GITHUB_TOKEN" in os.environ: token = os.environ["GITHUB_TOKEN"] - if token == None: + if token is None: temp = subprocess.run( - ["git", "config", "--get", "github.token"], text=True, capture_output=True + ["git", "config", "--get", "github.token"], + text=True, + capture_output=True, + check=False, ) if temp.returncode == 0: token = temp.stdout.strip() - if token == None and os.path.isfile( + if token is None and os.path.isfile( os.path.expanduser("~") + "/.github_shell_token" ): - with open(os.path.expanduser("~") + "/.github_shell_token", "r") as token_file: + with open( + os.path.expanduser("~") + "/.github_shell_token", "r", encoding="utf-8" + ) as token_file: token = token_file.read().strip() - if token == None: + if token is None: error("Error: no access token found or provided") g = github.Github(token) GITHUB_INSTANCE = g @@ -80,7 +80,7 @@ def upload_asset_with_checksum(release, filename: str) -> None: verify_via_checksumfile(filename) else: notice("Writing new checksum file") - with open(checksum_filename, "w") as checksumfile: + with open(checksum_filename, "w", encoding="utf-8") as checksumfile: checksumfile.write(sha256file(filename)) for file in [filename, checksum_filename]: