Check dependencies on setup in the nicer way. (#5989)

This commit is contained in:
Jorik Schellekens 2019-09-11 14:00:37 +01:00 committed by GitHub
parent 3505ffcda7
commit 6604b64fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 28 deletions

1
changelog.d/5989.misc Normal file
View File

@ -0,0 +1 @@
Clean up dependency checking at setup.

View File

@ -16,11 +16,9 @@
import attr import attr
from ._base import Config, ConfigError from synapse.python_dependencies import DependencyException, check_requirements
MISSING_SENTRY = """Missing sentry-sdk library. This is required to enable sentry from ._base import Config, ConfigError
integration.
"""
@attr.s @attr.s
@ -51,9 +49,9 @@ class MetricsConfig(Config):
self.sentry_enabled = "sentry" in config self.sentry_enabled = "sentry" in config
if self.sentry_enabled: if self.sentry_enabled:
try: try:
import sentry_sdk # noqa F401 check_requirements("sentry")
except ImportError: except DependencyException as e:
raise ConfigError(MISSING_SENTRY) raise ConfigError(e.message)
self.sentry_dsn = config["sentry"].get("dsn") self.sentry_dsn = config["sentry"].get("dsn")
if not self.sentry_dsn: if not self.sentry_dsn:

View File

@ -16,6 +16,7 @@
import os import os
from collections import namedtuple from collections import namedtuple
from synapse.python_dependencies import DependencyException, check_requirements
from synapse.util.module_loader import load_module from synapse.util.module_loader import load_module
from ._base import Config, ConfigError from ._base import Config, ConfigError
@ -34,17 +35,6 @@ THUMBNAIL_SIZE_YAML = """\
# method: %(method)s # method: %(method)s
""" """
MISSING_NETADDR = "Missing netaddr library. This is required for URL preview API."
MISSING_LXML = """Missing lxml library. This is required for URL preview API.
Install by running:
pip install lxml
Requires libxslt1-dev system package.
"""
ThumbnailRequirement = namedtuple( ThumbnailRequirement = namedtuple(
"ThumbnailRequirement", ["width", "height", "method", "media_type"] "ThumbnailRequirement", ["width", "height", "method", "media_type"]
) )
@ -171,16 +161,10 @@ class ContentRepositoryConfig(Config):
self.url_preview_enabled = config.get("url_preview_enabled", False) self.url_preview_enabled = config.get("url_preview_enabled", False)
if self.url_preview_enabled: if self.url_preview_enabled:
try: try:
import lxml check_requirements("url_preview")
lxml # To stop unused lint. except DependencyException as e:
except ImportError: raise ConfigError(e.message)
raise ConfigError(MISSING_LXML)
try:
from netaddr import IPSet
except ImportError:
raise ConfigError(MISSING_NETADDR)
if "url_preview_ip_range_blacklist" not in config: if "url_preview_ip_range_blacklist" not in config:
raise ConfigError( raise ConfigError(
@ -189,6 +173,9 @@ class ContentRepositoryConfig(Config):
"to work" "to work"
) )
# netaddr is a dependency for url_preview
from netaddr import IPSet
self.url_preview_ip_range_blacklist = IPSet( self.url_preview_ip_range_blacklist = IPSet(
config["url_preview_ip_range_blacklist"] config["url_preview_ip_range_blacklist"]
) )

View File

@ -147,6 +147,12 @@ def check_requirements(for_feature=None):
) )
except DistributionNotFound: except DistributionNotFound:
deps_needed.append(dependency) deps_needed.append(dependency)
if for_feature:
errors.append(
"Needed %s for the '%s' feature but it was not installed"
% (dependency, for_feature)
)
else:
errors.append("Needed %s but it was not installed" % (dependency,)) errors.append("Needed %s but it was not installed" % (dependency,))
if not for_feature: if not for_feature: