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

Show versions of registered plugins #30

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 34 additions & 8 deletions pyodide_cli/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from importlib.metadata import EntryPoint
from functools import cache
from importlib.metadata import Distribution, EntryPoint
from importlib.metadata import distribution as importlib_distribution
from importlib.metadata import entry_points

Expand All @@ -16,16 +17,29 @@


def version_callback(value: bool):
if value:
typer.echo(f"Pyodide CLI Version: {__version__}")
raise typer.Exit()
if not value:
return

typer.echo(f"pyodide CLI version: {__version__}")

eps = entry_points(group="pyodide.cli")
# filter out duplicate pkgs
pkgs = {_entrypoint_to_pkgname(ep): _entrypoint_to_version(ep) for ep in eps}
for pkg, version in pkgs.items():
typer.echo(f"{pkg} version: {version}")

raise typer.Exit()


@app.callback(no_args_is_help=True)
def callback(
ctx: typer.Context,
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show the version of the Pyodide CLI",
),
):
"""A command line interface for Pyodide.
Expand All @@ -36,14 +50,26 @@ def callback(
pass


def _entrypoint_to_pkgname(entrypoint: EntryPoint) -> str:
"""Find package name from entrypoint"""

@cache
def _entrypoint_to_distribution(entrypoint: EntryPoint) -> Distribution:
"""Find package distribution from entrypoint"""
top_level = entrypoint.value.split(".")[0]
dist = importlib_distribution(top_level)
return dist


def _entrypoint_to_pkgname(entrypoint: EntryPoint) -> str:
"""Find package name from entrypoint"""
dist = _entrypoint_to_distribution(entrypoint)
return dist.metadata["name"]


def _entrypoint_to_version(entrypoint: EntryPoint) -> str:
"""Find package version from entrypoint"""
dist = _entrypoint_to_distribution(entrypoint)
return dist.metadata["version"]


def _inject_origin(docstring: str, origin: str) -> str:
return f"{docstring}\n\n{origin}"

Expand Down
9 changes: 7 additions & 2 deletions pyodide_cli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ def plugins():

def test_cli_help():
output = check_output(["pyodide", "--help"]).decode("utf-8")
msg = "A command line interface for Pyodide."
assert msg in output
assert "A command line interface for Pyodide." in output


def test_cli_version(plugins):
output = check_output(["pyodide", "--version"]).decode("utf-8")
assert "pyodide CLI version:" in output
assert "plugin-test version: 1.0.0" in output


def test_click_click_object_defintion():
Expand Down
20 changes: 6 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[build-system]
requires = ["setuptools>=61.2", "setuptools_scm[toml]>=6.2"]

build-backend = "setuptools.build_meta"
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "pyodide-cli"
version = "0.0.1"
dynamic = ["version"]
authors = [{name = "Pyodide developers"}]
description = '"The command line interface for the Pyodide project"'
classifiers = [
Expand All @@ -31,19 +30,12 @@ Documentation = "https://pyodide.org/en/stable/"
[project.optional-dependencies]
test = ["pytest"]

[tool.hatch.version]
source = "vcs"

[project.scripts]
pyodide = "pyodide_cli.__main__:main"

[tool.setuptools]
package-dir = {"" = "."}
include-package-data = false

[tool.setuptools.packages]
find = {namespaces = false}

# Evable versioning via setuptools_scm
[tool.setuptools_scm]

[tool.pycln]
all = true

Expand Down