Commits

Amar Šahinović committed a8c51ea

Initial commit

  • Participants

Comments (0)

Files changed (12)

+*.pyc
+*.egg-info
+include README.md
+django-aftermath
+================
+
+Execute commands after tests are completed.
+
+Usage
+=====
+
+In your settings file, add the following required options:
+
+`TEST_RUNNER = 'aftermath.AftermathTestRunner'`
+
+To specifiy a backend, set a dotted path to the backend class:
+
+`AFTERMATH_BACKEND = 'aftermath.backends.NotifySendBackend'`
+
+Currently, there are two backends, `NullBackend` which does nothing, and `NotifySendBackend` which uses notify-send to generate notifications. You must install notify-send yourself.
+
+On Gnome3, `NotifySendBackend` creates notifications like these:
+
+![notification](notification.png)
+
+To specifiy if backend is executed when all test succeed, use `AFTERMATH_RUN_ON_SUCCESS` setting. Default values is `True`.
+
+To specifiy if backend is executed when some tests fail, use `AFTERMATH_RUN_ON_FAIL` setting. Default values is `True`.
+
+Usually, you will need to specify the first two options. If you wish to implement your own backend, take a look at one of the exisitng backends. All you need is a simple class with `__init__` and `aftermath` functions.
+django-aftermath
+================
+
+Execute commands after tests are completed.
+
+Usage
+=====
+
+In your settings file, add the following required options:
+
+`TEST_RUNNER = 'aftermath.AftermathTestRunner'`
+
+To specifiy a backend, set a dotted path to the backend class:
+
+`AFTERMATH_BACKEND = 'aftermath.backends.NotifySendBackend'`
+
+Currently, there are two backends, `NullBackend` which does nothing, and `NotifySendBackend` which uses notify-send to generate notifications. You must install notify-send yourself.
+
+On Gnome3, `NotifySendBackend` creates notifications like these:
+
+![notification](notification.png)
+
+To specifiy if backend is executed when all test succeed, use `AFTERMATH_RUN_ON_SUCCESS` setting. Default values is `True`.
+
+To specifiy if backend is executed when some tests fail, use `AFTERMATH_RUN_ON_FAIL` setting. Default values is `True`.
+
+Usually, you will need to specify the first two options. If you wish to implement your own backend, take a look at one of the exisitng backends. All you need is a simple class with `__init__` and `aftermath` functions.

File aftermath/__init__.py

+try:
+    from .runner import AftermathTestRunner
+except ImportError:
+    pass

File aftermath/app_settings.py

+from django.conf import settings
+from django.utils import importlib
+
+
+# Taken from django-rest-framework
+def import_from_string(val):
+    """
+    Attempt to import a class from a string representation.
+    """
+    try:
+        parts = val.split('.')
+        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
+        module = importlib.import_module(module_path)
+        return getattr(module, class_name)
+    except ImportError as e:
+        msg = "Could not import '{0}'".format(val)
+        raise ImportError(msg)
+
+
+AFTERMATH_RUN_ON_FAIL = getattr(settings, 'AFTERMATH_RUN_ON_FAIL', True)
+AFTERMATH_RUN_ON_SUCCESS = getattr(settings, 'AFTERMATH_RUN_ON_SUCCESS', True)
+AFTERMATH_BACKEND = getattr(settings, 'AFTERMATH_BACKEND', 'aftermath.backends.NullBackend')
+AFTERMATH_BACKEND_CLASS = import_from_string(AFTERMATH_BACKEND)

File aftermath/backends/__init__.py

+try:
+    from .notify_send import NotifySendBackend
+    from .null import NullBackend
+except ImportError:
+    pass

File aftermath/backends/notify_send.py

+import os
+
+
+class NotifySendBackend(object):
+
+    def __init__(self, number_of_failed_tests, *args, **kwargs):
+        self._number_of_failed_tests = number_of_failed_tests
+        self._success_command = "notify-send --icon='gtk-apply' 'All tests finished successfully'"
+        self._fail_command = "notify-send --icon='dialog-error' 'Tests failed' '{0} tests failed.'".format(number_of_failed_tests)
+
+    def aftermath(self):
+        command = self._success_command
+        if self._number_of_failed_tests > 0:
+            command = self._fail_command
+        try:                                                                    
+            os.popen(command)                                                       
+        except Exception, e:                                                    
+            print e.message                                                     

File aftermath/backends/null.py

+
+
+class NullBackend(object):
+
+    def __init__(self, number_of_failed_tests, *args, **kwargs):
+        self._number_of_failed_tests = number_of_failed_tests
+
+    def aftermath(self):
+        pass

File aftermath/runner.py

+from django.core.exceptions import ImproperlyConfigured
+from .app_settings import (
+    AFTERMATH_RUN_ON_FAIL, AFTERMATH_RUN_ON_SUCCESS,
+    AFTERMATH_BACKEND_CLASS
+)
+
+# TODO: For django 1.6, use default test runner, and leave this check for 1.5
+try:
+    from discover_runner import DiscoverRunner
+except ImportError:
+    raise ImproperlyConfigured("Please make sure you have "
+                               "django-discover-runner installed.")
+
+# Looks nicer to instantiate normally capitalized class :)
+Backend = AFTERMATH_BACKEND_CLASS
+
+
+class AftermathTestRunner(DiscoverRunner):
+
+    def run_tests(self, test_labels, extra_tests=None, **kwargs):
+        result = super(AftermathTestRunner, self).run_tests(test_labels,
+                                                            extra_tests,
+                                                            **kwargs)
+        if result == 0 and AFTERMATH_RUN_ON_SUCCESS:
+            b = Backend(result)
+            b.aftermath()
+        elif result > 0 and AFTERMATH_RUN_ON_FAIL:
+            b = Backend(result)
+            b.aftermath()
+        return result

File notification.png

Added
New image
+#/usr/bin/env python
+# -*- coding: utf-8 -*-
+from setuptools import setup, find_packages
+import os
+
+ROOT_DIR = os.path.dirname(__file__)
+SOURCE_DIR = os.path.join(ROOT_DIR)
+
+setup(
+    name="django-aftermath",
+    version="0.1.0",
+    description="Execute commands after tests are completed.",
+    author="Amar Šahinović",
+    author_email="amar@sahinovic.com",
+    url="https://github.com/amarsahinovic/django-aftermath",
+    license='BSD License', 
+    packages=find_packages(),
+    include_package_data=True,
+    zip_safe=False,
+    classifiers=[
+                   'Environment :: Web Environment',
+                   'Framework :: Django',
+                   'Intended Audience :: Developers',
+                   'License :: OSI Approved :: BSD License',
+                   'Operating System :: OS Independent',
+                   'Programming Language :: Python',
+                   'Topic :: Utilities'],
+    install_requires=[],
+)