@register("console_scripts")
def console_scripts(path, options, ignore_output=False):
- import pkg_resources
try:
entrypoint_name = options["entrypoint"]
"Key %s.entrypoint is required for post write hook %r"
% (options["_hook_name"], options["_hook_name"])
) from ke
- iter_ = pkg_resources.iter_entry_points("console_scripts", entrypoint_name)
- impl = next(iter_)
+ for entry in compat.importlib_metadata_get("console_scripts"):
+ if entry.name == entrypoint_name:
+ impl = entry
+ break
+ else:
+ raise util.CommandError(
+ f"Could not find entrypoint console_scripts.{entrypoint_name}"
+ )
cwd = options.get("cwd", None)
cmdline_options_str = options.get("options", "")
cmdline_options_list = _parse_cmdline_options(cmdline_options_str, path)
kw = {}
if ignore_output:
kw["stdout"] = kw["stderr"] = subprocess.DEVNULL
+
subprocess.run(
[
sys.executable,
"-c",
- "import %s; %s()"
- % (impl.module_name, ".".join((impl.module_name,) + impl.attrs)),
+ "import %s; %s.%s()" % (impl.module, impl.module, impl.attr),
]
+ cmdline_options_list,
cwd=cwd,
- **kw
+ **kw,
)
is_posix = os.name == "posix"
py39 = sys.version_info >= (3, 9)
-
+py38 = sys.version_info >= (3, 8)
+py37 = sys.version_info >= (3, 7)
string_types = (str,)
binary_type = bytes
class EncodedIO(io.TextIOWrapper):
def close(self) -> None:
pass
+
+
+if py37:
+ from importlib import resources as importlib_resources
+else:
+ import importlib_resources # noqa
+
+if py38:
+ from importlib import metadata as importlib_metadata
+else:
+ import importlib_metadata # noqa
+
+
+def importlib_metadata_get(group):
+ ep = importlib_metadata.entry_points()
+ if hasattr(ep, "select"):
+ return ep.select(group=group)
+ else:
+ return ep.get(group, ())
+import atexit
+from contextlib import ExitStack
import importlib
import importlib.machinery
import importlib.util
from mako import exceptions
from mako.template import Template
+from . import compat
from .exc import CommandError
"""
if not os.path.isabs(fname) and ":" in fname:
- import pkg_resources
- fname = pkg_resources.resource_filename(*fname.split(":"))
+ tokens = fname.split(":")
+
+ # from https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename # noqa E501
+
+ file_manager = ExitStack()
+ atexit.register(file_manager.close)
+
+ ref = compat.importlib_resources.files(tokens[0])
+ for tok in tokens[1:]:
+ ref = ref / tok
+ fname = file_manager.enter_context(
+ compat.importlib_resources.as_file(ref)
+ )
return fname
--- /dev/null
+.. change::
+ :tags: bug, general
+ :tickets: 885
+
+ The dependency on ``pkg_resources`` which is part of ``setuptools`` has
+ been removed, so there is no longer any runtime dependency on
+ ``setuptools``. The functionality has been replaced with
+ ``importlib.metadata`` and ``importlib.resources`` which are both part of
+ Python std.lib, or via pypy dependency ``importlib-metadata`` for Python
+ version < 3.8 and ``importlib-resources`` for Python version < 3.7.
install_requires =
SQLAlchemy>=1.3.0
Mako
+ importlib-metadata;python_version<"3.8"
+ importlib-resources;python_version<"3.7"
[options.extras_require]
tz =
self, input_config, expected_additional_arguments_fn, cwd=None
):
self.cfg = _no_sql_testing_config(directives=input_config)
- impl = mock.Mock(attrs=("foo", "bar"), module_name="black_module")
- entrypoints = mock.Mock(return_value=iter([impl]))
+
+ class MocksCantName:
+ name = "black"
+ attr = "bar"
+ module = "black_module.foo"
+
+ importlib_metadata_get = mock.Mock(return_value=iter([MocksCantName]))
with mock.patch(
- "pkg_resources.iter_entry_points", entrypoints
+ "alembic.util.compat.importlib_metadata_get",
+ importlib_metadata_get,
), mock.patch(
"alembic.script.write_hooks.subprocess"
) as mock_subprocess:
rev = command.revision(self.cfg, message="x")
- eq_(entrypoints.mock_calls, [mock.call("console_scripts", "black")])
+ eq_(importlib_metadata_get.mock_calls, [mock.call("console_scripts")])
eq_(
mock_subprocess.mock_calls,
[
[
sys.executable,
"-c",
- "import black_module; black_module.foo.bar()",
+ "import black_module.foo; black_module.foo.bar()",
]
+ expected_additional_arguments_fn(rev.path),
cwd=cwd,