From 7c4bbf8cb31d856a0fe547faf8edf165cd48ce74 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 7 Oct 2022 08:47:31 -0400 Subject: [PATCH] chore: minor cleanups from reverb (#1293) Signed-off-by: Henry Schreiner Signed-off-by: Henry Schreiner --- cibuildwheel/__main__.py | 4 ++-- cibuildwheel/linux.py | 2 +- cibuildwheel/macos.py | 7 +++--- cibuildwheel/options.py | 28 +++++++++++------------ cibuildwheel/projectfiles.py | 23 ++++++++----------- cibuildwheel/resources/install_certifi.py | 5 ++-- cibuildwheel/util.py | 4 ++-- cibuildwheel/windows.py | 2 +- 8 files changed, 34 insertions(+), 41 deletions(-) diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index 1a9f296f..48ba61af 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -162,7 +162,7 @@ def main() -> None: # cleanup manually shutil.rmtree(temp_dir, ignore_errors=sys.platform.startswith("win")) if temp_dir.exists(): - log.warning(f"Can't delete temporary folder '{str(temp_dir)}'") + log.warning(f"Can't delete temporary folder '{temp_dir}'") def build_in_directory(args: CommandLineArguments) -> None: @@ -300,7 +300,7 @@ def build_in_directory(args: CommandLineArguments) -> None: # cleanup manually shutil.rmtree(tmp_path, ignore_errors=sys.platform.startswith("win")) if tmp_path.exists(): - log.warning(f"Can't delete temporary folder '{str(tmp_path)}'") + log.warning(f"Can't delete temporary folder '{tmp_path}'") def print_preamble(platform: str, options: Options, identifiers: list[str]) -> None: diff --git a/cibuildwheel/linux.py b/cibuildwheel/linux.py index a64c829e..c9134e7e 100644 --- a/cibuildwheel/linux.py +++ b/cibuildwheel/linux.py @@ -448,4 +448,4 @@ def troubleshoot(options: Options, error: Exception) -> None: print(" Files detected:") print("\n".join(f" {f}" for f in so_files)) - print("") + print() diff --git a/cibuildwheel/macos.py b/cibuildwheel/macos.py index 95bbe268..895689ed 100644 --- a/cibuildwheel/macos.py +++ b/cibuildwheel/macos.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import functools import os import platform @@ -283,7 +284,7 @@ def build(options: Options, tmp_path: Path) -> None: options.globals.build_selector, options.globals.architectures ) - if len(python_configurations) == 0: + if not python_configurations: return try: @@ -574,10 +575,8 @@ def build(options: Options, tmp_path: Path) -> None: # we're all done here; move it to output (overwrite existing) if compatible_wheel is None: - try: + with contextlib.suppress(FileNotFoundError): (build_options.output_dir / repaired_wheel.name).unlink() - except FileNotFoundError: - pass shutil.move(str(repaired_wheel), build_options.output_dir) built_wheels.append(build_options.output_dir / repaired_wheel.name) diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index cb681fa3..fa313800 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -1,14 +1,14 @@ from __future__ import annotations +import configparser +import contextlib +import dataclasses import difflib import functools import os import shlex import sys import traceback -from configparser import ConfigParser -from contextlib import contextmanager -from dataclasses import asdict, dataclass from pathlib import Path from typing import Any, Callable, Dict, Generator, Iterator, List, Mapping, Union, cast @@ -40,7 +40,7 @@ from .util import ( ) -@dataclass +@dataclasses.dataclass class CommandLineArguments: platform: Literal["auto", "linux", "macos", "windows"] | None archs: str | None @@ -53,7 +53,7 @@ class CommandLineArguments: prerelease_pythons: bool -@dataclass(frozen=True) +@dataclasses.dataclass(frozen=True) class GlobalOptions: package_dir: Path output_dir: Path @@ -63,7 +63,7 @@ class GlobalOptions: container_engine: ContainerEngine -@dataclass(frozen=True) +@dataclasses.dataclass(frozen=True) class BuildOptions: globals: GlobalOptions environment: ParsedEnvironment @@ -105,7 +105,7 @@ class BuildOptions: Setting = Union[Dict[str, str], List[str], str, int] -@dataclass(frozen=True) +@dataclasses.dataclass(frozen=True) class Override: select_pattern: str options: dict[str, Setting] @@ -277,7 +277,7 @@ class OptionsReader: o for o in self.overrides if selector_matches(o.select_pattern, self.current_identifier) ] - @contextmanager + @contextlib.contextmanager def identifier(self, identifier: str | None) -> Generator[None, None, None]: self.current_identifier = identifier try: @@ -496,10 +496,8 @@ class Options: # Pass through environment variables if self.platform == "linux": for env_var_name in environment_pass: - try: + with contextlib.suppress(KeyError): environment.add(env_var_name, os.environ[env_var_name]) - except KeyError: - pass if dependency_versions == "pinned": dependency_constraints: None | ( @@ -574,7 +572,7 @@ class Options: ) def check_for_invalid_configuration(self, identifiers: list[str]) -> None: - if self.platform in ["macos", "windows"]: + if self.platform in {"macos", "windows"}: before_all_values = {self.build_options(i).before_all for i in identifiers} if len(before_all_values) > 1: @@ -599,7 +597,7 @@ class Options: def summary(self, identifiers: list[str]) -> str: lines = [ f"{option_name}: {option_value!r}" - for option_name, option_value in sorted(asdict(self.globals).items()) + for option_name, option_value in sorted(dataclasses.asdict(self.globals).items()) ] build_option_defaults = self.build_options(identifier=None) @@ -607,7 +605,7 @@ class Options: identifier: self.build_options(identifier) for identifier in identifiers } - for option_name, default_value in sorted(asdict(build_option_defaults).items()): + for option_name, default_value in sorted(dataclasses.asdict(build_option_defaults).items()): if option_name == "globals": continue @@ -642,7 +640,7 @@ def _get_pinned_container_images() -> Mapping[str, Mapping[str, str]]: """ pinned_images_file = resources_dir / "pinned_docker_images.cfg" - all_pinned_images = ConfigParser() + all_pinned_images = configparser.ConfigParser() all_pinned_images.read(pinned_images_file) return all_pinned_images diff --git a/cibuildwheel/projectfiles.py b/cibuildwheel/projectfiles.py index b5c3cbdf..46780cd1 100644 --- a/cibuildwheel/projectfiles.py +++ b/cibuildwheel/projectfiles.py @@ -1,8 +1,9 @@ from __future__ import annotations import ast +import configparser +import contextlib import sys -from configparser import ConfigParser from pathlib import Path from typing import Any @@ -59,25 +60,21 @@ def get_requires_python_str(package_dir: Path) -> str | None: """Return the python requires string from the most canonical source available, or None""" # Read in from pyproject.toml:project.requires-python - try: + with contextlib.suppress(FileNotFoundError): with (package_dir / "pyproject.toml").open("rb") as f1: info = tomllib.load(f1) - return str(info["project"]["requires-python"]) - except (FileNotFoundError, KeyError, IndexError, TypeError): - pass + with contextlib.suppress(KeyError, IndexError, TypeError): + return str(info["project"]["requires-python"]) # Read in from setup.cfg:options.python_requires - try: - config = ConfigParser() + config = configparser.ConfigParser() + with contextlib.suppress(FileNotFoundError): config.read(package_dir / "setup.cfg") - return str(config["options"]["python_requires"]) - except (FileNotFoundError, KeyError, IndexError, TypeError): - pass + with contextlib.suppress(KeyError, IndexError, TypeError): + return str(config["options"]["python_requires"]) - try: + with contextlib.suppress(FileNotFoundError): with (package_dir / "setup.py").open(encoding="utf8") as f2: return setup_py_python_requires(f2.read()) - except FileNotFoundError: - pass return None diff --git a/cibuildwheel/resources/install_certifi.py b/cibuildwheel/resources/install_certifi.py index 89bf82f0..0d261d51 100644 --- a/cibuildwheel/resources/install_certifi.py +++ b/cibuildwheel/resources/install_certifi.py @@ -6,6 +6,7 @@ # for the ssl module. Uses the certificates provided by the certifi package: # https://pypi.org/project/certifi/ +import contextlib import os import os.path import ssl @@ -39,10 +40,8 @@ def main(): relpath_to_certifi_cafile = os.path.relpath(certifi.where()) print(" -- removing any existing file or link") - try: + with contextlib.suppress(FileNotFoundError): os.remove(openssl_cafile) - except FileNotFoundError: - pass print(" -- creating symlink to certifi certificate bundle") os.symlink(relpath_to_certifi_cafile, openssl_cafile) diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index 01db05ec..29ccda97 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -461,7 +461,7 @@ def print_new_wheels(msg: str, output_dir: Path) -> Generator[None, None, None]: for wheel in final_contents - existing_contents ] - if len(new_contents) == 0: + if not new_contents: return max_name_len = max(len(f.name) for f in new_contents) @@ -526,7 +526,7 @@ def _parse_constraints_for_virtualenv( with constraint_path.open(encoding="utf-8") as constraint_file: for line in constraint_file: line = line.strip() - if len(line) == 0: + if not line: continue if line.startswith("#"): continue diff --git a/cibuildwheel/windows.py b/cibuildwheel/windows.py index 9e7dfff5..3ca6a24b 100644 --- a/cibuildwheel/windows.py +++ b/cibuildwheel/windows.py @@ -242,7 +242,7 @@ def build(options: Options, tmp_path: Path) -> None: options.globals.build_selector, options.globals.architectures ) - if len(python_configurations) == 0: + if not python_configurations: return try: