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

plugins: avoid crash on broken symlink #2545

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions sopel/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import collections
import importlib
import itertools
import logging
import os
from typing import TYPE_CHECKING

# TODO: use stdlib importlib.metadata when possible, after dropping py3.9.
# Stdlib does not support `entry_points(group='filter')` until py3.10, but
Expand All @@ -41,13 +43,22 @@

from . import exceptions, handlers, rules # noqa

if TYPE_CHECKING:
from collections.abc import Iterable

def _list_plugin_filenames(directory):

LOGGER = logging.getLogger(__name__)


def _list_plugin_filenames(directory: str | os.PathLike) -> Iterable[tuple[str, str]]:
SnoopJ marked this conversation as resolved.
Show resolved Hide resolved
# list plugin filenames from a directory
# yield 2-value tuples: (name, absolute path)
base = os.path.abspath(directory)
for filename in os.listdir(base):
abspath = os.path.join(base, filename)
abspath = os.path.realpath(os.path.join(base, filename))
if not os.path.exists(abspath):
LOGGER.warning("Plugin path does not exist, skipping: %r", abspath)
continue

if os.path.isdir(abspath):
if os.path.isfile(os.path.join(abspath, '__init__.py')):
Expand Down
9 changes: 9 additions & 0 deletions test/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,12 @@ def test_plugin_load_entry_point(tmpdir):
assert hasattr(test_mod, 'example_url')
assert hasattr(test_mod, 'shutdown')
assert hasattr(test_mod, 'ignored')


def test_plugin_skip_broken_links(tmp_path):
plugins_path = tmp_path
plugin = plugins_path.joinpath("amazing_plugin.py")
link_target = plugins_path.joinpath("nonexistent_file.py")
plugin.symlink_to(link_target)

assert list(plugins._list_plugin_filenames(plugins_path)) == []