|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | + |
| 6 | +from typing import Optional, List |
| 7 | +from subprocess import CalledProcessError |
| 8 | + |
| 9 | +from ci_tools.functions import install_into_venv |
| 10 | +from ci_tools.variables import in_ci, discover_repo_root, set_envvar_defaults |
| 11 | +from ci_tools.environment_exclusions import is_check_enabled |
| 12 | +from ci_tools.logging import logger |
| 13 | + |
| 14 | +from .Check import Check |
| 15 | + |
| 16 | +BLACK_VERSION = "24.4.0" |
| 17 | +REPO_ROOT = discover_repo_root() |
| 18 | + |
| 19 | + |
| 20 | +class black(Check): |
| 21 | + def __init__(self) -> None: |
| 22 | + super().__init__() |
| 23 | + |
| 24 | + def register( |
| 25 | + self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None |
| 26 | + ) -> None: |
| 27 | + """Register the `black` check. The black check installs black and runs black against the target package.""" |
| 28 | + parents = parent_parsers or [] |
| 29 | + p = subparsers.add_parser("black", parents=parents, help="Run the code formatter black check") |
| 30 | + p.set_defaults(func=self.run) |
| 31 | + |
| 32 | + def run(self, args: argparse.Namespace) -> int: |
| 33 | + """Run the black check command.""" |
| 34 | + logger.info("Running black check...") |
| 35 | + |
| 36 | + set_envvar_defaults() |
| 37 | + |
| 38 | + targeted = self.get_targeted_directories(args) |
| 39 | + |
| 40 | + results: List[int] = [] |
| 41 | + |
| 42 | + for parsed in targeted: |
| 43 | + package_dir = parsed.folder |
| 44 | + package_name = parsed.name |
| 45 | + |
| 46 | + executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir) |
| 47 | + logger.info(f"Processing {package_name} for black check") |
| 48 | + |
| 49 | + # install black |
| 50 | + try: |
| 51 | + install_into_venv(executable, [f"black=={BLACK_VERSION}"]) |
| 52 | + except CalledProcessError as e: |
| 53 | + logger.error("Failed to install black:", e) |
| 54 | + return e.returncode |
| 55 | + |
| 56 | + logger.info(f"Running black against {package_name}") |
| 57 | + |
| 58 | + config_file_location = os.path.join(REPO_ROOT, "eng/black-pyproject.toml") |
| 59 | + |
| 60 | + if in_ci(): |
| 61 | + if not is_check_enabled(package_dir, "black"): |
| 62 | + logger.info(f"Package {package_name} opts-out of black check.") |
| 63 | + continue |
| 64 | + try: |
| 65 | + run_result = subprocess.run( |
| 66 | + [ |
| 67 | + executable, |
| 68 | + "-m", |
| 69 | + "black", |
| 70 | + f"--config={config_file_location}", |
| 71 | + package_dir, |
| 72 | + ], |
| 73 | + stdout=subprocess.PIPE, |
| 74 | + stderr=subprocess.PIPE, |
| 75 | + check=True, |
| 76 | + ) |
| 77 | + if run_result.stderr and "reformatted" in run_result.stderr.decode("utf-8"): |
| 78 | + if in_ci(): |
| 79 | + logger.info(f"The package {package_name} needs reformat. Run `black` locally to reformat.") |
| 80 | + results.append(1) |
| 81 | + else: |
| 82 | + logger.info(f"The package {package_name} was reformatted.") |
| 83 | + else: |
| 84 | + logger.info(f"The package {package_name} is properly formatted, no files changed.") |
| 85 | + |
| 86 | + except subprocess.CalledProcessError as e: |
| 87 | + logger.error(f"Unable to invoke black for {package_name}. Ran into exception {e}.") |
| 88 | + |
| 89 | + return max(results) if results else 0 |
0 commit comments