-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Encapsulating package managers #4918
Draft
foarsitter
wants to merge
1
commit into
cookiecutter:master
Choose a base branch
from
foarsitter:package_managers
base: master
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.
Draft
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 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 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 @@ | ||
from typing import Any | ||
|
||
from cookiecutter.environment import StrictEnvironment | ||
from jinja2.ext import Extension | ||
|
||
|
||
class PackageManager: | ||
managers: dict[str, "PackageManager"] = {} | ||
|
||
def __init_subclass__(cls, **kwargs): | ||
cls.managers[cls.__name__.lower()] = cls() | ||
|
||
def install(self, obj: str, context: dict[str, Any]) -> str: | ||
raise NotImplementedError("Subclasses must implement the install method") | ||
|
||
|
||
class PIP(PackageManager): | ||
"""PIP package manager""" | ||
|
||
def install(self, obj: str, context: dict[str, Any]) -> str: | ||
if obj == "development": | ||
return "pip install -r requirements/local.txt" | ||
|
||
return f"pip install {obj}" | ||
|
||
|
||
class Poetry(PackageManager): | ||
"""Poetry package manager""" | ||
|
||
def install(self, obj: str, context: dict[str, Any]) -> str: | ||
if obj == "development": | ||
return "poetry install --with dev" | ||
|
||
return f"poetry add {obj}" | ||
|
||
|
||
class UV(PackageManager): | ||
"""Poetry package manager""" | ||
|
||
|
||
class InstallExtension(Extension): | ||
"""Jinja2 extension to convert a Python object to JSON.""" | ||
|
||
def __init__(self, environment: StrictEnvironment): | ||
"""Initialize the extension with the given environment.""" | ||
super().__init__(environment) | ||
|
||
def install(obj, context): | ||
# TODO we need to retrieve the package manager from the context or in another way | ||
# manager_name = context.package_manager | ||
manager_name = "pip" | ||
package_manager = PackageManager.managers[manager_name] | ||
return package_manager.install(obj, context) | ||
|
||
environment.filters["install"] = install |
This file contains 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 |
---|---|---|
|
@@ -21,7 +21,7 @@ steps: | |
path: ${PRE_COMMIT_HOME} | ||
commands: | ||
- export PRE_COMMIT_HOME=$CI_PROJECT_DIR/.cache/pre-commit | ||
- pip install -q pre-commit | ||
- {{ "-q pre-commit"|install(cookiecutter)}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that's rendered out to either |
||
- pre-commit run --show-diff-on-failure --color=always --all-files | ||
|
||
- name: test | ||
|
@@ -38,7 +38,7 @@ steps: | |
{%- else %} | ||
image: python:3.11 | ||
commands: | ||
- pip install -r requirements/local.txt | ||
- {{ "development"|install(cookiecutter)}} | ||
- pytest | ||
{%- endif%} | ||
|
||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be out of scope apologies: this can be the exact same as pip install section except you would prepend pip with uv like uv pip install etc.
Regarding lock files, you should be able to run
uv pip compile requirements.txt -o requirements-lock.txt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So UV can extend PIP just by prepending the command with
uv
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not feature complete I think, but for the basics, like
pip install -r
which is the extent of 99% of projects, yes, prepending uv suffices.