Skip to content

Commit

Permalink
Fix local plugin installation (#1129)
Browse files Browse the repository at this point in the history
* test: add test for lektor.packages.install_local_package

* fix(packages): fix local plugin installation

The most recent version of pip (23.1) removes support for the
`--install-option` parameter to `pip install`.  This breaks our
`install_local_package` function, which uses `--install-option`
because `pip install --target` didn't used to work with `pip install
--editable`.

As it turns out, recent versions of pip support the use of `--target`
with `--editable`.  So here we make use of that.

Note that this also relieves us of the need to parse our local
plugin requirements ourselves (thus, hopefully, fixing #865.)
  • Loading branch information
dairiki committed Apr 16, 2023
1 parent 5745acc commit 24c39ab
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ These are all the changes in Lektor since the first public release.

## 3.3.9 (unreleased)

### Bit-Rot

- Fix installation of local plugins (from the `packages/`
subdirectory). This was broken by release 23.1 of `pip` which
dropped support for the `--install-option` flag to `pip install`.
([#1129], [#1127])

### Bugs

- Implement better input validation for the date/time-formatting jinja
filters. Prior to this, passing a `jinja2.Undefined` value to the
`date`, `time`, or `datetime` filters would elicit an assertion
error. ([#1122], [#1121])

- Fix for spurious rebuilds. Recent versions of watchdog (>=2.3.0)
enabled tracking of IN_OPEN events. These fire when a file is opened
— even just for reading. Now we're pickier about only responding to
Expand All @@ -18,6 +26,8 @@ These are all the changes in Lektor since the first public release.
[#1117]: https://github.com/lektor/lektor/pull/1117
[#1121]: https://github.com/lektor/lektor/issues/1121
[#1122]: https://github.com/lektor/lektor/pull/1122
[#1127]: https://github.com/lektor/lektor/issues/1127
[#1129]: https://github.com/lektor/lektor/pull/1129

## 3.3.8 (2023-02-28)

Expand Down
37 changes: 2 additions & 35 deletions lektor/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import shutil
import site
import sys
import tempfile
from subprocess import PIPE

import click
Expand Down Expand Up @@ -104,53 +103,21 @@ def download_and_install_package(
def install_local_package(package_root, path):
"""This installs a local dependency of a package."""
# XXX: windows
env = dict(os.environ)
env["PYTHONPATH"] = package_root

# Step 1: generate egg info and link us into the target folder.
rv = portable_popen(
[
sys.executable,
"-m",
"pip",
"install",
"--target",
package_root,
"--editable",
path,
"--install-option=--install-dir=%s" % package_root,
"--no-deps",
],
env=env,
).wait()
if rv != 0:
raise RuntimeError("Failed to install local package")

# Step 2: generate the egg info into a temp folder to find the
# requirements.
tmp = tempfile.mkdtemp()
try:
rv = portable_popen(
[
sys.executable,
"setup.py",
"--quiet",
"egg_info",
"--quiet",
"--egg-base",
tmp,
],
cwd=path,
).wait()
dirs = os.listdir(tmp)
if rv != 0 or len(dirs) != 1:
raise RuntimeError("Failed to create egg info for local package.")
requires = os.path.join(tmp, dirs[0], "requires.txt")

# We have dependencies, install them!
if os.path.isfile(requires):
download_and_install_package(package_root, requirements_file=requires)
finally:
shutil.rmtree(tmp)


def get_package_info(path):
"""Returns the name of a package at a path."""
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ install_requires =
Jinja2>=3.0
markupsafe
mistune>=0.7.0,<2
pip
pip>=21.1
python-slugify
pytz
requests
setuptools
setuptools>=45.2
watchdog
Werkzeug<3

Expand Down
9 changes: 9 additions & 0 deletions tests/setup_py-dummy-plugin/lektor_dummy_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Dummy test plugin"""
from lektor.pluginsystem import Plugin


class DummyPlugin(Plugin):
"""Dummy test plugin."""

# pylint: disable=too-few-public-methods
name = "dummy"
13 changes: 13 additions & 0 deletions tests/setup_py-dummy-plugin/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from setuptools import setup

setup(
name="lektor-dummy-plugin",
description="setup.py test plugin",
version="0.1a42",
py_modules=["lektor_dummy_plugin"],
entry_points={
"lektor.plugins": [
"dummy-plugin = lektor_dummy_plugin:DummyPlugin",
]
},
)
18 changes: 18 additions & 0 deletions tests/test_packages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from pathlib import Path

import pytest

from lektor.packages import add_site
from lektor.packages import install_local_package
from lektor.pluginsystem import load_plugins


@pytest.mark.usefixtures("save_sys_path")
def test_install_local_package(tmp_path):
package_root = os.fspath(tmp_path / "package_root")
dummy_plugin_path = os.fspath(Path(__file__).parent / "setup_py-dummy-plugin")
install_local_package(package_root, dummy_plugin_path)
add_site(package_root)
plugins = load_plugins()
assert plugins["dummy-plugin"].name == "dummy"

0 comments on commit 24c39ab

Please sign in to comment.