Skip to content

Commit

Permalink
datasette install -r requirements.txt, closes #2033
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 22, 2023
1 parent 8297032 commit 84b726c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
14 changes: 12 additions & 2 deletions datasette/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,25 @@ def package(


@cli.command()
@click.argument("packages", nargs=-1, required=True)
@click.argument("packages", nargs=-1)
@click.option(
"-U", "--upgrade", is_flag=True, help="Upgrade packages to latest version"
)
def install(packages, upgrade):
@click.option(
"-r",
"--requirement",
type=click.Path(exists=True),
help="Install from requirements file",
)
def install(packages, upgrade, requirement):
"""Install plugins and packages from PyPI into the same environment as Datasette"""
if not packages and not requirement:
raise click.UsageError("Please specify at least one package to install")
args = ["pip", "install"]
if upgrade:
args += ["--upgrade"]
if requirement:
args += ["-r", requirement]
args += list(packages)
sys.argv = args
run_module("pip", run_name="__main__")
Expand Down
7 changes: 4 additions & 3 deletions docs/cli-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,14 @@ Would install the `datasette-cluster-map <https://datasette.io/plugins/datasette
::

Usage: datasette install [OPTIONS] PACKAGES...
Usage: datasette install [OPTIONS] [PACKAGES]...

Install plugins and packages from PyPI into the same environment as Datasette

Options:
-U, --upgrade Upgrade packages to latest version
--help Show this message and exit.
-U, --upgrade Upgrade packages to latest version
-r, --requirement PATH Install from requirements file
--help Show this message and exit.


.. [[[end]]]
Expand Down
11 changes: 10 additions & 1 deletion docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ This command can also be used to upgrade Datasette itself to the latest released

datasette install -U datasette

These commands are thin wrappers around ``pip install`` and ``pip uninstall``, which ensure they run ``pip`` in the same virtual environment as Datasette itself.
You can install multiple plugins at once by listing them as lines in a ``requirements.txt`` file like this::

datasette-vega
datasette-cluster-map

Then pass that file to ``datasette install -r``::

datasette install -r requirements.txt

The ``install`` and ``uninstall`` commands are thin wrappers around ``pip install`` and ``pip uninstall``, which ensure that they run ``pip`` in the same virtual environment as Datasette itself.

One-off plugins using --plugins-dir
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ def test_install_upgrade(run_module, flag):
assert sys.argv == ["pip", "install", "--upgrade", "datasette"]


@mock.patch("datasette.cli.run_module")
def test_install_requirements(run_module):
runner = CliRunner()
with runner.isolated_filesystem():
with open("requirements.txt", "w") as fp:
fp.write("datasette-mock-plugin\ndatasette-plugin-2")
runner.invoke(cli, ["install", "-r", "requirements.txt"])
run_module.assert_called_once_with("pip", run_name="__main__")
assert sys.argv == ["pip", "install", "-r", "requirements.txt"]


def test_install_error_if_no_packages():
runner = CliRunner()
result = runner.invoke(cli, ["install"])
assert result.exit_code == 2
assert "Error: Please specify at least one package to install" in result.output


@mock.patch("datasette.cli.run_module")
def test_uninstall(run_module):
runner = CliRunner()
Expand Down

0 comments on commit 84b726c

Please sign in to comment.