Skip to content
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

feat: script shims for package managers #5

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
with:
fetch-depth: 0

- uses: actions/setup-python@v3
- uses: actions/setup-python@v5
with:
python-version: '3.11'

Expand All @@ -32,5 +32,4 @@ jobs:

- name: Upload to Pypi
run: |
pip install twine
twine upload --username __token__ --password ${{ secrets.PYPI_TOKEN }} dist/*
pipx run twine upload --username __token__ --password ${{ secrets.PYPI_TOKEN }} dist/*
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,32 @@ pa
```

If the package manager agent is pip, **OnePM will enforce an activated virtualenv, or a `.venv` under the current directory**.

## Shims for Package Managers

OnePM also provides shim for the package managers like [corepack](https://nodejs.org/api/corepack.html),
so you don't need to install package managers yourself.

OnePM reads the `package-manager` field under `[tool.onepm]` table in `pyproject.toml`, and install the required package manager with the correct version in an isolated environment.

```toml
[tool.onepm]
package-manager = "poetry"
```

Or you can restrict the version range:

```toml
[tool.onepm]
package-manager = "poetry>=1.1.0"
```

_For Python package management, OnePM is all you need._

## OnePM Management Commands

- `onepm install`: Install the package manager configured in project file
- `onepm use $SPEC`: Use the package manager given by the requirement spec
- `onepm update|up`: Update the package manager used in the project
- `onepm cleanup [$NAME]`: Clean up installations of specified package manager or all
- `onepm list|ls $NAME`: List all installed versions of the given package manager
152 changes: 149 additions & 3 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ authors = [
{name = "Frost Ming", email = "[email protected]"},
]
dependencies = [
"tomli>=2.0.1; python_version < \"3.11\"",
"packaging>=22.1",
"tomlkit>=0.12.3",
"unearth>=0.14.0",
]
requires-python = ">=3.10"
readme = "README.md"
Expand All @@ -31,19 +32,24 @@ pu = "onepm:pu"
pr = "onepm:pr"
pun = "onepm:pun"
pa = "onepm:pa"
pdm = "onepm:pdm"
poetry = "onepm:poetry"
pipenv = "onepm:pipenv"
onepm = "onepm.cli:main"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.pdm.version]
source = "scm"

[tool.pdm.dev-dependencies]
test = [
"pytest>=7.1.2",
"pytest-mock>=3.12.0",
]

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.ruff]
target-version = "py310"

Expand All @@ -59,3 +65,6 @@ extend-select = [

[tool.ruff.isort]
known-first-party = ["onepm"]

[tool.onepm]
package-manager = "pdm>=2.12.0"
48 changes: 8 additions & 40 deletions src/onepm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,16 @@
import sys
from typing import Callable, NoReturn

from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
from onepm.core import OneManager

from onepm.pm.base import PackageManager
from onepm.pm.pdm import PDM
from onepm.pm.pip import Pip
from onepm.pm.pipenv import Pipenv
from onepm.pm.poetry import Poetry

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


PACKAGE_MANAGERS: dict[str, type[PackageManager]] = {
p.name: p # type: ignore[type-abstract]
for p in [Pipenv, PDM, Poetry, Pip]
}


def determine_package_manager() -> type[PackageManager]:
try:
with open("pyproject.toml", "rb") as f:
pyproject = tomllib.load(f)
except FileNotFoundError:
pyproject = {}
package_manager: str | None = (
pyproject.get("tool", {}).get("onepm", {}).get("package-manager")
)
if package_manager:
requirement = Requirement(package_manager)
if (name := canonicalize_name(requirement.name)) in PACKAGE_MANAGERS:
return PACKAGE_MANAGERS[name]
for pm in PACKAGE_MANAGERS.values():
if pm.matches(pyproject):
return pm
return Pip


def make_shortcut(method_name: str) -> Callable[[list[str] | None], NoReturn]:
def make_shortcut(
method_name: str, specified: str | None = None
) -> Callable[[list[str] | None], NoReturn]:
def main(args: list[str] | None = None) -> NoReturn: # type: ignore[misc]
if args is None:
args = sys.argv[1:]
package_manager = determine_package_manager()()
package_manager = OneManager().get_package_manager(specified)
getattr(package_manager, method_name)(*args)

return main
Expand All @@ -58,3 +23,6 @@ def main(args: list[str] | None = None) -> NoReturn: # type: ignore[misc]
pun = make_shortcut("uninstall")
pr = make_shortcut("run")
pa = make_shortcut("execute")
pdm = make_shortcut("execute", "pdm")
poetry = make_shortcut("execute", "poetry")
pipenv = make_shortcut("execute", "pipenv")
Loading
Loading