|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from typing import Optional, List |
| 6 | +from subprocess import CalledProcessError, check_call |
| 7 | + |
| 8 | +from .Check import Check |
| 9 | +from ci_tools.functions import pip_install |
| 10 | +from ci_tools.variables import discover_repo_root, in_ci, set_envvar_defaults, in_ci, set_envvar_defaults |
| 11 | +from ci_tools.environment_exclusions import is_check_enabled |
| 12 | +from ci_tools.logging import logger |
| 13 | + |
| 14 | +REPO_ROOT = discover_repo_root() |
| 15 | +PYLINT_VERSION = "3.2.7" |
| 16 | + |
| 17 | +class pylint(Check): |
| 18 | + def __init__(self) -> None: |
| 19 | + super().__init__() |
| 20 | + |
| 21 | + def register(self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None) -> None: |
| 22 | + """Register the pylint check. The pylint check installs pylint and runs pylint against the target package. |
| 23 | + """ |
| 24 | + parents = parent_parsers or [] |
| 25 | + p = subparsers.add_parser("pylint", parents=parents, help="Run the pylint check") |
| 26 | + p.set_defaults(func=self.run) |
| 27 | + |
| 28 | + p.add_argument( |
| 29 | + "--next", |
| 30 | + default=False, |
| 31 | + help="Next version of pylint is being tested.", |
| 32 | + required=False, |
| 33 | + ) |
| 34 | + |
| 35 | + def run(self, args: argparse.Namespace) -> int: |
| 36 | + """Run the pylint check command.""" |
| 37 | + logger.info("Running pylint check...") |
| 38 | + |
| 39 | + set_envvar_defaults() |
| 40 | + targeted = self.get_targeted_directories(args) |
| 41 | + |
| 42 | + results: List[int] = [] |
| 43 | + |
| 44 | + for parsed in targeted: |
| 45 | + package_dir = parsed.folder |
| 46 | + package_name = parsed.name |
| 47 | + executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir) |
| 48 | + logger.info(f"Processing {package_name} for pylint check") |
| 49 | + |
| 50 | + # install dependencies |
| 51 | + try: |
| 52 | + pip_install([ |
| 53 | + "azure-pylint-guidelines-checker==0.5.6", "--index-url=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/" |
| 54 | + ], True, executable, package_dir) |
| 55 | + except CalledProcessError as e: |
| 56 | + logger.error("Failed to install dependencies:", e) |
| 57 | + return e.returncode |
| 58 | + |
| 59 | + # install pylint |
| 60 | + try: |
| 61 | + if args.next: |
| 62 | + # use latest version of pylint |
| 63 | + pip_install(["pylint"], True, executable, package_dir) |
| 64 | + else: |
| 65 | + pip_install([f"pylint=={PYLINT_VERSION}"], True, executable, package_dir) |
| 66 | + except CalledProcessError as e: |
| 67 | + logger.error("Failed to install pylint:", e) |
| 68 | + return e.returncode |
| 69 | + |
| 70 | + top_level_module = parsed.namespace.split(".")[0] |
| 71 | + |
| 72 | + if in_ci(): |
| 73 | + if not is_check_enabled(package_dir, "pylint"): |
| 74 | + logger.info( |
| 75 | + f"Package {package_name} opts-out of pylint check." |
| 76 | + ) |
| 77 | + continue |
| 78 | + |
| 79 | + rcFileLocation = os.path.join(REPO_ROOT, "eng/pylintrc") if args.next else os.path.join(REPO_ROOT, "pylintrc") |
| 80 | + |
| 81 | + try: |
| 82 | + results.append(check_call( |
| 83 | + [ |
| 84 | + executable, |
| 85 | + "-m", |
| 86 | + "pylint", |
| 87 | + "--rcfile={}".format(rcFileLocation), |
| 88 | + "--output-format=parseable", |
| 89 | + os.path.join(package_dir, top_level_module), |
| 90 | + ] |
| 91 | + )) |
| 92 | + except CalledProcessError as e: |
| 93 | + logger.error( |
| 94 | + "{} exited with linting error {}. Please see this link for more information https://aka.ms/azsdk/python/pylint-guide".format(package_name, e.returncode) |
| 95 | + ) |
| 96 | + if args.next and in_ci(): |
| 97 | + from gh_tools.vnext_issue_creator import create_vnext_issue |
| 98 | + create_vnext_issue(package_dir, "pylint") |
| 99 | + |
| 100 | + results.append(e.returncode) |
| 101 | + |
| 102 | + if args.next and in_ci(): |
| 103 | + from gh_tools.vnext_issue_creator import close_vnext_issue |
| 104 | + close_vnext_issue(package_name, "pylint") |
| 105 | + |
| 106 | + return max(results) if results else 0 |
0 commit comments