From ceeacdbfc33c2f1eb9d219d8bd539ef5e6c4c877 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 5 Apr 2017 12:55:18 +0200 Subject: [PATCH] Add explicit checks for asgi library versions Refs daphne/#105 --- channels/apps.py | 10 +++------- channels/package_checks.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 channels/package_checks.py diff --git a/channels/apps.py b/channels/apps.py index f2d7874..2ee1b0f 100644 --- a/channels/apps.py +++ b/channels/apps.py @@ -2,6 +2,7 @@ from django.apps import AppConfig from django.core.exceptions import ImproperlyConfigured from .binding.base import BindingMetaclass +from .package_checks import check_all class ChannelsConfig(AppConfig): @@ -10,13 +11,8 @@ class ChannelsConfig(AppConfig): verbose_name = "Channels" def ready(self): - # Check you're not running 1.10 or above - try: - from django import channels # NOQA isort:skip - except ImportError: - pass - else: - raise ImproperlyConfigured("You have Django 1.10 or above; use the builtin django.channels!") + # Check versions + check_all() # Do django monkeypatches from .hacks import monkeypatch_django monkeypatch_django() diff --git a/channels/package_checks.py b/channels/package_checks.py new file mode 100644 index 0000000..4509c81 --- /dev/null +++ b/channels/package_checks.py @@ -0,0 +1,30 @@ +import importlib +from distutils.version import StrictVersion + + +required_versions = { + "asgi_redis": "1.2.0", + "asgi_ipc": "1.3.0", +} + + +def check_all(): + """ + Checks versions of all the possible packages you have installed so that + we can easily warn people about incompatible versions. + + This is needed as there are some packages (e.g. asgi_redis) that we cannot + declare dependencies on as they are not _required_. People usually remember + to upgrade their Channels package so this is where we check. + """ + for package, version in required_versions.items(): + try: + module = importlib.import_module(package) + except ImportError: + return + else: + if StrictVersion(version) > StrictVersion(module.__version__): + raise RuntimeError("Your version of %s is too old - it must be at least %s" % ( + package, + version, + ))