-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add apistub Check Without Tox
#44124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JennyPng
wants to merge
15
commits into
Azure:main
Choose a base branch
from
JennyPng:jennypng-apistub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
23d3986
initial
JennyPng 42c0be3
black
JennyPng 2651a22
invoke with executable
JennyPng fafe6fb
Merge branch 'main' into jennypng-apistub
JennyPng 65ccf0b
Merge branch 'main' into jennypng-apistub
JennyPng e6ee501
freeze
JennyPng ef2a35f
Merge branch 'main' into jennypng-apistub
JennyPng bdec5c7
clean and fix outpath
JennyPng 40f558e
Merge branch 'main' into jennypng-apistub
JennyPng 5099fa4
always use wheel
JennyPng 3e60fa9
minor
JennyPng 4e7cfa3
minor
JennyPng 5fa0314
version check
JennyPng 444effa
template update
JennyPng 7785e46
minor
JennyPng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import argparse | ||
| import os | ||
| import sys | ||
|
|
||
| from typing import Optional, List | ||
| from subprocess import CalledProcessError | ||
|
|
||
| from .Check import Check | ||
| from ci_tools.functions import install_into_venv, find_whl | ||
| from ci_tools.scenario.generation import create_package_and_install | ||
| from ci_tools.variables import discover_repo_root, set_envvar_defaults | ||
| from ci_tools.logging import logger | ||
| from ci_tools.parsing import ParsedSetup | ||
|
|
||
| REPO_ROOT = discover_repo_root() | ||
| MAX_PYTHON_VERSION = (3, 11) | ||
|
|
||
|
|
||
| def get_package_wheel_path(pkg_root: str, out_path: Optional[str]) -> tuple[str, Optional[str]]: | ||
| # parse setup.py to get package name and version | ||
| pkg_details = ParsedSetup.from_path(pkg_root) | ||
|
|
||
| # Check if wheel is already built and available for current package | ||
| prebuilt_dir = os.getenv("PREBUILT_WHEEL_DIR") | ||
| out_token_path = None | ||
| if prebuilt_dir: | ||
| found_whl = find_whl(prebuilt_dir, pkg_details.name, pkg_details.version) | ||
| pkg_path = os.path.join(prebuilt_dir, found_whl) if found_whl else None | ||
| if not pkg_path: | ||
| raise FileNotFoundError( | ||
| "No prebuilt wheel found for package {} version {} in directory {}".format( | ||
| pkg_details.name, pkg_details.version, prebuilt_dir | ||
| ) | ||
| ) | ||
| # If the package is a wheel and out_path is given, the token file output path should be the parent directory of the wheel | ||
| if out_path: | ||
| out_token_path = os.path.join(out_path, os.path.basename(os.path.dirname(pkg_path))) | ||
| return pkg_path, out_token_path | ||
|
|
||
| # Otherwise, use wheel created in staging directory, or fall back on source directory | ||
| pkg_path = find_whl(pkg_root, pkg_details.name, pkg_details.version) or pkg_root | ||
| out_token_path = out_path | ||
|
|
||
| return pkg_path, out_token_path | ||
|
|
||
|
|
||
| def get_cross_language_mapping_path(pkg_root): | ||
| mapping_path = os.path.join(pkg_root, "apiview-properties.json") | ||
| if os.path.exists(mapping_path): | ||
| return mapping_path | ||
| return None | ||
|
|
||
|
|
||
| class apistub(Check): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
| def register( | ||
| self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None | ||
| ) -> None: | ||
| """Register the apistub check. The apistub check generates an API stub of the target package.""" | ||
| parents = parent_parsers or [] | ||
| p = subparsers.add_parser( | ||
| "apistub", parents=parents, help="Run the apistub check to generate an API stub for a package" | ||
| ) | ||
| p.set_defaults(func=self.run) | ||
|
|
||
| def run(self, args: argparse.Namespace) -> int: | ||
| """Run the apistub check command.""" | ||
| logger.info("Running apistub check...") | ||
|
|
||
| if sys.version_info > MAX_PYTHON_VERSION: | ||
| logger.error( | ||
| f"Python version {sys.version_info.major}.{sys.version_info.minor} is not supported. Maximum supported version is {MAX_PYTHON_VERSION[0]}.{MAX_PYTHON_VERSION[1]}." | ||
| ) | ||
| return 1 | ||
|
|
||
| set_envvar_defaults() | ||
| targeted = self.get_targeted_directories(args) | ||
|
|
||
| results: List[int] = [] | ||
|
|
||
| for parsed in targeted: | ||
| package_dir = parsed.folder | ||
| package_name = parsed.name | ||
| executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir) | ||
| logger.info(f"Processing {package_name} for apistub check") | ||
|
|
||
JennyPng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # install dependencies | ||
| self.install_dev_reqs(executable, args, package_dir) | ||
|
|
||
| try: | ||
| install_into_venv( | ||
| executable, | ||
| [ | ||
| "-r", | ||
| os.path.join(REPO_ROOT, "eng", "apiview_reqs.txt"), | ||
| "--index-url=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/", | ||
| ], | ||
| package_dir, | ||
| ) | ||
| except CalledProcessError as e: | ||
| logger.error(f"Failed to install dependencies: {e}") | ||
| return e.returncode | ||
|
|
||
| create_package_and_install( | ||
| distribution_directory=staging_directory, | ||
| target_setup=package_dir, | ||
| skip_install=True, | ||
| cache_dir=None, | ||
| work_dir=staging_directory, | ||
| force_create=False, | ||
| package_type="wheel", | ||
| pre_download_disabled=False, | ||
| python_executable=executable, | ||
| ) | ||
|
|
||
| self.pip_freeze(executable) | ||
JennyPng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| pkg_path, out_token_path = get_package_wheel_path(package_dir, staging_directory) | ||
| cross_language_mapping_path = get_cross_language_mapping_path(package_dir) | ||
|
|
||
| cmds = ["-m", "apistub", "--pkg-path", pkg_path] | ||
|
|
||
| if out_token_path: | ||
| cmds.extend(["--out-path", out_token_path]) | ||
| if cross_language_mapping_path: | ||
| cmds.extend(["--mapping-path", cross_language_mapping_path]) | ||
|
|
||
| logger.info("Running apistub {}.".format(cmds)) | ||
|
|
||
| try: | ||
| self.run_venv_command(executable, cmds, cwd=package_dir, check=True, immediately_dump=True) | ||
| except CalledProcessError as e: | ||
| logger.error(f"{package_name} exited with error {e.returncode}") | ||
| results.append(e.returncode) | ||
|
|
||
| return max(results) if results else 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,3 +49,4 @@ pyright = true | |
| pylint = true | ||
| black = true | ||
| generate = false | ||
| apistub = false | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.