-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: make DependencyManagers objects of base class #377
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
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 |
|---|---|---|
| @@ -1,15 +1 @@ | ||
| from twyn.dependency_managers.managers.npm_dependency_manager import NpmDependencyManager | ||
| from twyn.dependency_managers.managers.pypi_dependency_manager import PypiDependencyManager | ||
| from twyn.dependency_managers.utils import ( | ||
| PACKAGE_ECOSYSTEMS, | ||
| get_dependency_manager_from_file, | ||
| get_dependency_manager_from_name, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "NpmDependencyManager", | ||
| "PypiDependencyManager", | ||
| "get_dependency_manager_from_file", | ||
| "get_dependency_manager_from_name", | ||
| "PACKAGE_ECOSYSTEMS", | ||
| ] | ||
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,73 @@ | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
| from twyn.dependency_managers.exceptions import NoMatchingDependencyManagerError | ||
| from twyn.dependency_parser.parsers.constants import ( | ||
| PACKAGE_LOCK_JSON, | ||
| POETRY_LOCK, | ||
| REQUIREMENTS_TXT, | ||
| UV_LOCK, | ||
| YARN_LOCK, | ||
| ) | ||
| from twyn.trusted_packages.references.base import AbstractPackageReference | ||
| from twyn.trusted_packages.references.top_npm_reference import TopNpmReference | ||
| from twyn.trusted_packages.references.top_pypi_reference import TopPyPiReference | ||
|
|
||
|
|
||
| @dataclass | ||
| class DependencyManager: | ||
| """Base class for all `DependencyManagers`. | ||
|
|
||
| It acts as a repository, linking programming languages with trusted packages sources and dependency file names. | ||
| """ | ||
|
|
||
| name: str | ||
| """Name identifier for the package ecosystem.""" | ||
|
|
||
| trusted_packages_source: type[AbstractPackageReference] | ||
| """Reference class for trusted packages source.""" | ||
|
|
||
| dependency_files: set[str] | ||
| """Set of supported dependency file names.""" | ||
|
|
||
| def matches_dependency_file(self, dependency_file: str) -> bool: | ||
| """Check if this manager can handle the given dependency file.""" | ||
| return Path(dependency_file).name in self.dependency_files | ||
|
|
||
| def get_alternative_source(self, sources: dict[str, str]) -> str | None: | ||
| """Get alternative source URL for this ecosystem from sources dict.""" | ||
| return sources.get(self.name) | ||
|
|
||
|
|
||
| npm_dependency_manager = DependencyManager( | ||
| name="npm", | ||
| trusted_packages_source=TopNpmReference, | ||
| dependency_files={PACKAGE_LOCK_JSON, YARN_LOCK}, | ||
| ) | ||
| pypi_dependency_manager = DependencyManager( | ||
| name="pypi", | ||
| trusted_packages_source=TopPyPiReference, | ||
| dependency_files={UV_LOCK, POETRY_LOCK, REQUIREMENTS_TXT}, | ||
| ) | ||
|
|
||
| DEPENDENCY_MANAGERS: list[DependencyManager] = [pypi_dependency_manager, npm_dependency_manager] | ||
| """List of available dependency manager classes.""" | ||
|
|
||
| PACKAGE_ECOSYSTEMS = {x.name for x in DEPENDENCY_MANAGERS} | ||
| """Set of package ecosystem names from available dependency managers.""" | ||
|
|
||
|
|
||
| def get_dependency_manager_from_file(dependency_file: str) -> DependencyManager: | ||
| """Get dependency manager that can handle the given file.""" | ||
| for manager in DEPENDENCY_MANAGERS: | ||
| if manager.matches_dependency_file(dependency_file): | ||
| return manager | ||
| raise NoMatchingDependencyManagerError | ||
|
|
||
|
|
||
| def get_dependency_manager_from_name(name: str) -> DependencyManager: | ||
| """Get dependency manager by ecosystem name.""" | ||
| for manager in DEPENDENCY_MANAGERS: | ||
| if manager.name == name: | ||
| return manager | ||
| raise NoMatchingDependencyManagerError |
Empty file.
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/twyn/dependency_managers/managers/npm_dependency_manager.py
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/twyn/dependency_managers/managers/pypi_dependency_manager.py
This file was deleted.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -1,24 +1,18 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| from twyn.dependency_managers.managers.base import BaseDependencyManager | ||
| from twyn.dependency_managers.managers import DependencyManager | ||
|
|
||
|
|
||
| class DummyManager(BaseDependencyManager): | ||
| name = "pypi" | ||
| trusted_packages_source = Mock() | ||
| dependency_files = {"requirements.txt", "poetry.lock"} | ||
| manager = DependencyManager( | ||
| name="pypi", trusted_packages_source=Mock(), dependency_files={"requirements.txt", "poetry.lock"} | ||
| ) | ||
|
|
||
|
|
||
| class TestDependencyManager: | ||
| def test_matches_dependency_file(self) -> None: | ||
| assert DummyManager.matches_dependency_file("requirements.txt") | ||
| assert DummyManager.matches_dependency_file("/some/path/poetry.lock") | ||
| assert not DummyManager.matches_dependency_file("setup.py") | ||
|
|
||
| def test_matches_language_name(self) -> None: | ||
| assert DummyManager.matches_ecosystem_name("pypi") | ||
| assert not DummyManager.matches_ecosystem_name("npm") | ||
| assert manager.matches_dependency_file("requirements.txt") | ||
| assert manager.matches_dependency_file("/some/path/poetry.lock") | ||
| assert not manager.matches_dependency_file("setup.py") | ||
|
|
||
| def test_get_alternative_source_none(self) -> None: | ||
| sources = {"npm": "npmjs.com"} | ||
| assert DummyManager.get_alternative_source(sources) is None | ||
| assert manager.get_alternative_source(sources) is None |
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.