Repository navigation
[CI] Generate ApiHash during extended build #48628
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f298c0
Refactoring, and add apiHash generation to build-extended-artifacts.yml
tjprescott ade5519
Code review feedback.
tjprescott 835d99f
Test change for azure.template
tjprescott 45d4ec1
Revert azure-template changes (except pyproject.toml)
tjprescott 79031ea
Pass PI consistency...
tjprescott 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
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 was deleted.
Oops, something went wrong.
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,63 @@ | ||
| import argparse | ||
| import hashlib | ||
| import pathlib | ||
| import re | ||
| from typing import Dict, List | ||
|
|
||
|
|
||
| _METADATA_PATTERN = re.compile( | ||
| r"^# Package is parsed using apiview-stub-generator\(version:([^\)]+)\), Python version:\s*([^\s]+)\s*$" | ||
| ) | ||
|
|
||
|
|
||
| def extract_metadata(api_markdown_path: pathlib.Path) -> Dict[str, str]: | ||
| with api_markdown_path.open(encoding="utf-8-sig", newline="") as api_markdown_file: | ||
| file_text = api_markdown_file.read() | ||
| line_ending = "\r\n" if "\r\n" in file_text else "\n" | ||
| lines = re.split(r"\r?\n", file_text) | ||
|
|
||
| metadata: Dict[str, str] = {} | ||
| filtered: List[str] = [] | ||
| for line in lines: | ||
| match = _METADATA_PATTERN.match(line) | ||
| if match: | ||
| metadata["parserVersion"] = match.group(1) | ||
| metadata["pythonVersion"] = match.group(2) | ||
| else: | ||
| filtered.append(line) | ||
|
|
||
| if filtered and filtered[0].startswith("```"): | ||
| body = filtered[1:] | ||
| while body and not body[0].strip(): | ||
| body.pop(0) | ||
| filtered = [filtered[0], *body] | ||
| else: | ||
| while filtered and not filtered[0].strip(): | ||
| filtered.pop(0) | ||
|
|
||
| normalized_text = "\n".join(line.rstrip() for line in filtered) | ||
| metadata["apiMdSha256"] = hashlib.sha256(normalized_text.encode("utf-8")).hexdigest() | ||
|
|
||
| api_markdown_path.write_text(line_ending.join(filtered), encoding="utf-8", newline="") | ||
| metadata_path = api_markdown_path.parent / "api.metadata.yml" | ||
| metadata_text = line_ending.join(f"{key}: {metadata[key]}" for key in sorted(metadata)) + line_ending | ||
| metadata_path.write_text(metadata_text, encoding="utf-8", newline="") | ||
| print(f"Updated markdown: {api_markdown_path}") | ||
| print(f"Generated metadata: {metadata_path}") | ||
| return metadata | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Extract Python APIView metadata from API markdown") | ||
| parser.add_argument("--api-markdown-path") | ||
| parser.add_argument("--output-path", default=".") | ||
| args = parser.parse_args() | ||
|
|
||
| api_markdown_path = pathlib.Path(args.api_markdown_path) if args.api_markdown_path else pathlib.Path(args.output_path) / "api.md" | ||
| if not api_markdown_path.is_file(): | ||
| parser.error(f"API markdown file not found: {api_markdown_path}") | ||
| extract_metadata(api_markdown_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,55 @@ | ||
| import argparse | ||
| import json | ||
| import pathlib | ||
| import subprocess | ||
|
|
||
| import yaml | ||
|
|
||
| from extract_apiview_metadata import extract_metadata | ||
|
|
||
|
|
||
| def update_package_info(metadata_path: pathlib.Path, package_info_path: pathlib.Path) -> None: | ||
| metadata = yaml.safe_load(metadata_path.read_text(encoding="utf-8-sig")) | ||
| api_hash = metadata.get("apiMdSha256") if isinstance(metadata, dict) else None | ||
| if not isinstance(api_hash, str) or not api_hash: | ||
| raise ValueError(f"apiMdSha256 was not found in {metadata_path}") | ||
|
|
||
| package_info = json.loads(package_info_path.read_text(encoding="utf-8-sig")) | ||
| package_info["ApiHash"] = api_hash | ||
| package_info_path.write_text(json.dumps(package_info, indent=2) + "\n", encoding="utf-8") | ||
| print(f"Stored ApiHash in {package_info_path}") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Add API markdown hashes to PackageInfo files") | ||
| parser.add_argument("--artifact-staging-directory", required=True) | ||
| parser.add_argument("--repo-root", required=True) | ||
| args = parser.parse_args() | ||
|
|
||
| artifact_dir = pathlib.Path(args.artifact_staging_directory) | ||
| repo_root = pathlib.Path(args.repo_root) | ||
| package_info_dir = artifact_dir / "PackageInfo" | ||
| export_script = repo_root / "eng" / "common" / "scripts" / "Export-APIViewMarkdown.ps1" | ||
|
|
||
| for package_info_path in package_info_dir.glob("*.json"): | ||
| package_info = json.loads(package_info_path.read_text(encoding="utf-8-sig")) | ||
| package_name = package_info["Name"] | ||
| package_artifact_dir = artifact_dir / package_name | ||
| token_file = package_artifact_dir / f"{package_name}_python.json" | ||
| # API stub generation intentionally omits management-plane packages, so they have no token file. | ||
| # Revisit this behavior if management-plane packages are included in API stub generation. | ||
| if not token_file.is_file(): | ||
| print(f"API token file was not found for {package_name}; skipping ApiHash update") | ||
| continue | ||
|
|
||
| subprocess.run( | ||
| ["pwsh", str(export_script), "-TokenJsonPath", str(token_file), "-OutputPath", str(package_artifact_dir)], | ||
| check=True, | ||
| ) | ||
| extract_metadata(package_artifact_dir / "api.md") | ||
|
|
||
| update_package_info(package_artifact_dir / "api.metadata.yml", package_info_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
Oops, something went wrong.
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.