Skip to content

Commit

Permalink
Merge pull request #2545 from SnoopJ/bugfix/crash-on-broken-symlink
Browse files Browse the repository at this point in the history
plugins: avoid crash on broken symlink
  • Loading branch information
dgw authored Nov 5, 2023
2 parents 3b91a76 + 3932025 commit 15a3b73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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, Union

# 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: Union[str, os.PathLike]) -> Iterable[tuple[str, str]]:
# 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)) == []

0 comments on commit 15a3b73

Please sign in to comment.