-
Notifications
You must be signed in to change notification settings - Fork 35
feat: check PyPI registry when deps.dev fails to find a source repository #982
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2558b9f
feat: check PyPI registry when deps.dev fails to find a source reposi…
benmss c1750f8
chore: avoid circular dependency
benmss ddcd963
chore: add alternative find repo for latest purl version also
benmss 60d7a12
chore: add integration test
benmss 9c5535d
chore: update tests
benmss 058b94b
chore: pass build tool names to super class
benmss 73fa8e9
chore: reuse PyPI JSON asset
benmss 9a71ab5
chore: update tests
benmss 886a194
chore: add purl type to build tool in registry info
benmss 0842d8d
chore: add repository info for source code heuristic; minor fixes
benmss 3ed6e53
chore: fix test
benmss dc87f81
chore: minor fix
benmss 993996b
chore: add integration test for find-source command
benmss 6244da9
chore: address PR feedback
benmss c404735
chore: ensure package registry is found properly; ensure asset is not…
benmss dd1a61c
chore: revert find_valid_repository_url change
benmss 2209b7a
chore: minor fix
benmss 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 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 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,90 @@ | ||
| # Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. | ||
| # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
|
||
| """This module contains the logic for finding repositories of PyPI projects.""" | ||
| import logging | ||
|
|
||
| from packageurl import PackageURL | ||
|
|
||
| from macaron.repo_finder.repo_finder_enums import RepoFinderInfo | ||
| from macaron.repo_finder.repo_validator import find_valid_repository_url | ||
| from macaron.slsa_analyzer.package_registry import PACKAGE_REGISTRIES, PyPIRegistry | ||
| from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset | ||
| from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo | ||
|
|
||
| logger: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def find_repo( | ||
| purl: PackageURL, package_registries_info: list[PackageRegistryInfo] | None = None | ||
| ) -> tuple[str, RepoFinderInfo]: | ||
| """Retrieve the repository URL that matches the given PyPI PURL. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| purl : PackageURL | ||
| The parsed PURL to convert to the repository path. | ||
| package_registries_info: list[PackageRegistryInfo] | None | ||
| The list of package registry information if available. | ||
| If no package registries are loaded, this can be set to None. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple[str, RepoFinderOutcome] : | ||
| The repository URL for the passed package, if found, and the outcome to report. | ||
| """ | ||
| pypi_info = None | ||
| if package_registries_info: | ||
| # Find the package registry info object that contains the PyPI registry and has the pypi build tool. | ||
| pypi_info = next( | ||
| ( | ||
| info | ||
| for info in package_registries_info | ||
| if isinstance(info.package_registry, PyPIRegistry) and info.build_tool_name in {"poetry", "pip"} | ||
| ), | ||
| None, | ||
| ) | ||
|
|
||
| if not pypi_info or not isinstance(pypi_info.package_registry, PyPIRegistry): | ||
| pypi_registry = next((registry for registry in PACKAGE_REGISTRIES if isinstance(registry, PyPIRegistry)), None) | ||
| else: | ||
| pypi_registry = pypi_info.package_registry | ||
|
|
||
| if not pypi_registry: | ||
| logger.debug("PyPI package registry not available.") | ||
| return "", RepoFinderInfo.PYPI_NO_REGISTRY | ||
|
|
||
| pypi_asset = None | ||
| from_metadata = False | ||
| if pypi_info: | ||
| for existing_asset in pypi_info.metadata: | ||
| if not isinstance(existing_asset, PyPIPackageJsonAsset): | ||
| continue | ||
|
|
||
| if existing_asset.component_name == purl.name and existing_asset.component_version == purl.version: | ||
| pypi_asset = existing_asset | ||
| from_metadata = True | ||
| break | ||
|
|
||
| if not pypi_asset: | ||
| pypi_asset = PyPIPackageJsonAsset(purl.name, purl.version, False, pypi_registry, {}) | ||
|
|
||
| if not pypi_asset.package_json and not pypi_asset.download(dest=""): | ||
| return "", RepoFinderInfo.PYPI_HTTP_ERROR | ||
|
|
||
| if not from_metadata and pypi_info: | ||
| # Save the asset for later use. | ||
| pypi_info.metadata.append(pypi_asset) | ||
|
|
||
| url_dict = pypi_asset.get_project_links() | ||
| if not url_dict: | ||
| return "", RepoFinderInfo.PYPI_JSON_ERROR | ||
|
|
||
| # Look for the repository URL. | ||
| fixed_url = find_valid_repository_url(url_dict.values()) | ||
| if not fixed_url: | ||
| return "", RepoFinderInfo.PYPI_NO_URLS | ||
|
|
||
| logger.debug("Found repository URL from PyPI: %s", fixed_url) | ||
| pypi_asset.has_repository = True | ||
| return fixed_url, RepoFinderInfo.FOUND_FROM_PYPI | ||
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.