-
Notifications
You must be signed in to change notification settings - Fork 11
feat(python): add uv package manager support (TC-3855) #428
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
9 commits
Select commit
Hold shift + click to select a range
b7611e0
feat(python): add PythonUvProvider and PythonProviderFactory for uv p…
a-oren f73f511
ci: install uv in PR workflow for PythonUvProvider tests
a-oren 1cf873e
Merge branch 'main' into TC-3855
a-oren e8e4756
fix(python): update manifest references in Python providers to use ma…
a-oren a9174b5
refactor(python): switch UV provider from pip list/show to uv export
a-oren 1cc2721
Merge branch 'main' into TC-3855
a-oren 5482e17
fix(python): walk up parent directories to find uv.lock for workspace…
a-oren 7ca7c02
fix(python): align UV provider with JS client behavior
a-oren 008eca0
Merge branch 'main' into TC-3855
a-oren 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
121 changes: 121 additions & 0 deletions
121
src/main/java/io/github/guacsec/trustifyda/providers/PythonProviderFactory.java
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,121 @@ | ||
| /* | ||
| * Copyright 2023-2025 Trustify Dependency Analytics Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.github.guacsec.trustifyda.providers; | ||
|
|
||
| import io.github.guacsec.trustifyda.tools.Operations; | ||
| import io.github.guacsec.trustifyda.utils.Environment; | ||
| import io.github.guacsec.trustifyda.utils.PyprojectTomlUtils; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Factory for creating the appropriate {@link PythonProvider} based on lock file presence. Follows | ||
| * the same pattern as {@link JavaScriptProviderFactory}. | ||
| */ | ||
| public final class PythonProviderFactory { | ||
|
|
||
| private static final Map<String, Function<Path, PythonProvider>> PYTHON_PROVIDERS = | ||
| Map.of(PythonUvProvider.LOCK_FILE, PythonUvProvider::new); | ||
|
|
||
| /** | ||
| * Creates a Python provider for {@code pyproject.toml} manifests by checking for known lock files | ||
| * in the manifest directory and parent directories (for workspace members). When {@code uv.lock} | ||
| * is present, returns a {@link PythonUvProvider}; otherwise falls back to {@link | ||
| * PythonPyprojectProvider} (pip-based). | ||
| * | ||
| * @param manifestPath the path to the pyproject.toml manifest | ||
| * @return the matching Python provider | ||
| */ | ||
| public static PythonProvider create(final Path manifestPath) { | ||
| var manifestDir = manifestPath.getParent(); | ||
|
|
||
| // Check manifest directory first (fast path) | ||
| for (var entry : PYTHON_PROVIDERS.entrySet()) { | ||
| if (Files.isRegularFile(manifestDir.resolve(entry.getKey()))) { | ||
| return entry.getValue().apply(manifestPath); | ||
| } | ||
| } | ||
|
|
||
| // Walk up parent directories to find lock file at workspace root | ||
| Path lockFileDir = findLockFileDirInParents(manifestDir); | ||
| if (lockFileDir != null) { | ||
| for (var entry : PYTHON_PROVIDERS.entrySet()) { | ||
| if (Files.isRegularFile(lockFileDir.resolve(entry.getKey()))) { | ||
| return entry.getValue().apply(manifestPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Unlike JavaScript, pip fallback is valid — no lock file required | ||
| return new PythonPyprojectProvider(manifestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Walks up from the given directory to find a parent containing a known Python lock file. | ||
| * Respects {@code TRUSTIFY_DA_WORKSPACE_DIR} override, stops at uv workspace root boundaries and | ||
| * the git root. | ||
| * | ||
| * @param startDir the directory to start searching from (typically the manifest directory) | ||
| * @return the directory containing the lock file, or {@code null} if not found | ||
| */ | ||
| static Path findLockFileDirInParents(Path startDir) { | ||
|
a-oren marked this conversation as resolved.
|
||
| // Environment override takes precedence | ||
| String workspaceDirOverride = Environment.get("TRUSTIFY_DA_WORKSPACE_DIR"); | ||
| if (workspaceDirOverride != null && !workspaceDirOverride.isBlank()) { | ||
| Path overrideDir = Path.of(workspaceDirOverride); | ||
| for (String lockFile : PYTHON_PROVIDERS.keySet()) { | ||
| if (Files.isRegularFile(overrideDir.resolve(lockFile))) { | ||
| return overrideDir; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // If startDir itself is a workspace root, don't walk up to avoid escaping into a parent | ||
| // workspace | ||
| if (PyprojectTomlUtils.isUvWorkspaceRoot(startDir)) { | ||
| return null; | ||
| } | ||
|
|
||
| String gitRoot = Operations.getGitRootDir(startDir.toString()).orElse(null); | ||
| Path boundary = gitRoot != null ? Path.of(gitRoot) : startDir.toAbsolutePath().getRoot(); | ||
|
|
||
| Path current = startDir.toAbsolutePath().normalize().getParent(); | ||
| while (current != null) { | ||
| for (String lockFile : PYTHON_PROVIDERS.keySet()) { | ||
| if (Files.isRegularFile(current.resolve(lockFile))) { | ||
| return current; | ||
| } | ||
| } | ||
|
|
||
| // Stop at uv workspace root boundary | ||
| if (PyprojectTomlUtils.isUvWorkspaceRoot(current)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (current.equals(boundary.toAbsolutePath().normalize())) { | ||
| break; | ||
| } | ||
|
|
||
| current = current.getParent(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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.
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.