|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from typing import Optional, List |
| 7 | +from subprocess import CalledProcessError, check_call |
| 8 | + |
| 9 | +from .Check import Check |
| 10 | +from ci_tools.functions import install_into_venv |
| 11 | +from ci_tools.variables import in_ci, set_envvar_defaults |
| 12 | +from ci_tools.variables import discover_repo_root |
| 13 | +from ci_tools.environment_exclusions import is_check_enabled, is_typing_ignored |
| 14 | +from ci_tools.scenario.generation import create_package_and_install |
| 15 | + |
| 16 | +from ci_tools.logging import logger |
| 17 | + |
| 18 | +PYRIGHT_VERSION = "1.1.391" |
| 19 | +REPO_ROOT = discover_repo_root() |
| 20 | + |
| 21 | +def get_pyright_config_path(package_dir: str, staging_dir: str) -> str: |
| 22 | + if os.path.exists(os.path.join(package_dir, "pyrightconfig.json")): |
| 23 | + config_path = os.path.join(package_dir, "pyrightconfig.json") |
| 24 | + else: |
| 25 | + config_path = os.path.join(REPO_ROOT, "pyrightconfig.json") |
| 26 | + |
| 27 | + # read the config and adjust relative paths |
| 28 | + with open(config_path, "r") as f: |
| 29 | + config_text = f.read() |
| 30 | + config_text = config_text.replace("\"**", "\"../../../../**") |
| 31 | + config = json.loads(config_text) |
| 32 | + |
| 33 | + # add or update the execution environment |
| 34 | + if config.get("executionEnvironments"): |
| 35 | + config["executionEnvironments"].append({"root": package_dir}) |
| 36 | + else: |
| 37 | + config.update({"executionEnvironments": [{"root": package_dir}]}) |
| 38 | + |
| 39 | + pyright_config_path = os.path.join(staging_dir, "pyrightconfig.json") |
| 40 | + |
| 41 | + with open(pyright_config_path, "w+") as f: |
| 42 | + f.write(json.dumps(config, indent=4)) |
| 43 | + return pyright_config_path |
| 44 | + |
| 45 | + |
| 46 | +class pyright(Check): |
| 47 | + def __init__(self) -> None: |
| 48 | + super().__init__() |
| 49 | + |
| 50 | + def register(self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None) -> None: |
| 51 | + """Register the pyright check. The pyright check installs pyright and runs pyright against the target package. |
| 52 | + """ |
| 53 | + parents = parent_parsers or [] |
| 54 | + p = subparsers.add_parser("pyright", parents=parents, help="Run the pyright check") |
| 55 | + p.set_defaults(func=self.run) |
| 56 | + |
| 57 | + p.add_argument( |
| 58 | + "--next", |
| 59 | + default=False, |
| 60 | + help="Next version of pyright is being tested.", |
| 61 | + required=False, |
| 62 | + ) |
| 63 | + |
| 64 | + def run(self, args: argparse.Namespace) -> int: |
| 65 | + """Run the pyright check command.""" |
| 66 | + logger.info("Running pyright check...") |
| 67 | + |
| 68 | + set_envvar_defaults() |
| 69 | + targeted = self.get_targeted_directories(args) |
| 70 | + |
| 71 | + results: List[int] = [] |
| 72 | + |
| 73 | + for parsed in targeted: |
| 74 | + package_dir = parsed.folder |
| 75 | + package_name = parsed.name |
| 76 | + |
| 77 | + executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir) |
| 78 | + logger.info(f"Processing {package_name} for pyright check") |
| 79 | + |
| 80 | + try: |
| 81 | + if args.next: |
| 82 | + # use latest version of pyright |
| 83 | + install_into_venv(executable, ["pyright"], package_dir) |
| 84 | + else: |
| 85 | + install_into_venv(executable, [f"pyright=={PYRIGHT_VERSION}"], package_dir) |
| 86 | + except CalledProcessError as e: |
| 87 | + logger.error("Failed to install pyright:", e) |
| 88 | + return e.returncode |
| 89 | + |
| 90 | + create_package_and_install( |
| 91 | + distribution_directory=staging_directory, |
| 92 | + target_setup=package_dir, |
| 93 | + skip_install=False, |
| 94 | + cache_dir=None, |
| 95 | + work_dir=staging_directory, |
| 96 | + force_create=False, |
| 97 | + package_type="sdist", |
| 98 | + pre_download_disabled=False, |
| 99 | + python_executable=executable, |
| 100 | + ) |
| 101 | + |
| 102 | + self.install_dev_reqs(executable, args, package_dir) |
| 103 | + |
| 104 | + top_level_module = parsed.namespace.split(".")[0] |
| 105 | + paths = [ |
| 106 | + os.path.join(package_dir, top_level_module), |
| 107 | + ] |
| 108 | + |
| 109 | + if not args.next and in_ci() and not is_check_enabled(package_dir, "pyright"): |
| 110 | + logger.info( |
| 111 | + f"Package {package_name} opts-out of pyright check. See https://aka.ms/python/typing-guide for information." |
| 112 | + ) |
| 113 | + continue |
| 114 | + else: |
| 115 | + # check if samples dir exists, if not, skip sample code check |
| 116 | + samples = os.path.exists(os.path.join(package_dir, "samples")) |
| 117 | + generated_samples = os.path.exists(os.path.join(package_dir, "generated_samples")) |
| 118 | + if not samples and not generated_samples: |
| 119 | + logger.info( |
| 120 | + f"Package {package_name} does not have a samples directory." |
| 121 | + ) |
| 122 | + else: |
| 123 | + paths.append(os.path.join(package_dir, "samples" if samples else "generated_samples")) |
| 124 | + |
| 125 | + config_path = get_pyright_config_path(package_dir, staging_directory) |
| 126 | + |
| 127 | + commands = [ |
| 128 | + executable, |
| 129 | + "-m", |
| 130 | + "pyright", |
| 131 | + "--project", |
| 132 | + config_path, |
| 133 | + ] |
| 134 | + commands.extend(paths) |
| 135 | + |
| 136 | + try: |
| 137 | + check_call(commands) |
| 138 | + except CalledProcessError as error: |
| 139 | + if args.next and in_ci() and is_check_enabled(args.target_package, "pyright") and not is_typing_ignored(package_name): |
| 140 | + from gh_tools.vnext_issue_creator import create_vnext_issue |
| 141 | + create_vnext_issue(package_dir, "pyright") |
| 142 | + |
| 143 | + print("See https://aka.ms/python/typing-guide for information.\n\n") |
| 144 | + results.append(1) |
| 145 | + |
| 146 | + if args.next and in_ci() and not is_typing_ignored(package_name): |
| 147 | + from gh_tools.vnext_issue_creator import close_vnext_issue |
| 148 | + close_vnext_issue(package_name, "pyright") |
| 149 | + |
| 150 | + return max(results) if results else 0 |
0 commit comments