datasette install -r requirements.txt, closes #2033

pull/2034/head
Simon Willison 2023-03-06 14:27:30 -08:00
rodzic a53b893c46
commit 1ad92a1d87
4 zmienionych plików z 44 dodań i 6 usunięć

Wyświetl plik

@ -340,15 +340,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__")

Wyświetl plik

@ -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]]]

Wyświetl plik

@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wyświetl plik

@ -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()