From 7f6a02d7c025d9ec408edd8301342aa6422d9d02 Mon Sep 17 00:00:00 2001
From: Chris Marusich <cmmarusich@gmail.com>
Date: Mon, 16 Nov 2015 09:28:37 -0800
Subject: [PATCH] gnu: Add python-contextlib2

* gnu/packages/python.scm (python-contextlib2, python2-contextlib2):
  New variables.
---
 gnu/packages/patches/python-contextlib2.patch | 51 +++++++++++++++++++++++++++
 gnu/packages/python.scm                       | 24 +++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 gnu/packages/patches/python-contextlib2.patch

diff --git a/gnu/packages/patches/python-contextlib2.patch b/gnu/packages/patches/python-contextlib2.patch
new file mode 100644
index 0000000..343c121
--- /dev/null
+++ b/gnu/packages/patches/python-contextlib2.patch
@@ -0,0 +1,51 @@
+Make it so the test script included in the contextlib2 tarball will be run
+when setup.py is invoked with the "test" target. Succeed if and only if the
+tests pass.
+
+--- a/setup.py
++++ b/setup.py
+@@ -1,5 +1,36 @@
+ #!/usr/bin/env python
++import subprocess
++import sys
++
+ from distutils.core import setup
++from distutils.cmd import Command
++
++
++class TestRunner(Command):
++    description = "run the test script"
++    # distutils requires that user_options be set, but we don't actually need
++    # to create any special options for this command, so we use an empty list.
++    user_options = []
++    def initialize_options(self):
++        # All Command subclasses are required to implement this method,
++        # but we don't actually need to set up any options for this command.
++        pass
++
++    def finalize_options(self):
++        # Same as above.
++        pass
++
++    def run(self):
++        # The absolute path to the executable file for the currently
++        # running Python interpreter is in sys.executable. Let's just
++        # use that to invoke the tests like someone might from the
++        # shell. If the tests fail, this method call will raise an
++        # exception. The exception will not be caught, so it will cause
++        # the current Python interpreter to print a stack trace and exit
++        # with a non-zero exit code. The test output will go to
++        # stdout/stderr regardless of whether the tests succeed or fail.
++        subprocess.check_call([sys.executable, "./test_contextlib2.py", "-v"])
++
+ 
+ # Technically, unittest2 is a dependency to run the tests on 2.6 and 3.1
+ # This file ignores that, since I don't want to depend on distribute
+@@ -10,6 +41,7 @@ setup(
+     version=open('VERSION.txt').read().strip(),
+     py_modules=['contextlib2'],
+     license='PSF License',
++    cmdclass={'test': TestRunner},
+     description='Backports and enhancements for the contextlib module',
+     long_description=open('README.txt').read(),
+     author='Nick Coghlan',
diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index 4e54ef9..1b119fa 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -12,6 +12,7 @@
 ;;; Copyright © 2015 Eric Dvorsak <eric@dvorsak.fr>
 ;;; Copyright © 2015 Leo Famulari <leo@famulari.name>
 ;;; Copyright © 2015 Ben Woodcroft <donttrustben@gmail.com>
+;;; Copyright © 2015, Chris Marusich <cmmarusich@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5895,3 +5896,26 @@ automatically detect a wide range of file encodings.")
 
 (define-public python2-chardet
   (package-with-python2 python-chardet))
+
+(define-public python-contextlib2
+  (package
+    (name "python-contextlib2")
+    (version "0.4.0")
+    (source
+      (origin
+        (method url-fetch)
+        (uri (pypi-uri "contextlib2" version))
+        (sha256
+         (base32 "0cmp131dlh0d0zvw0aza1zd13glvngzk8lb4avks0hm7yxwdr9am"))
+        (patches (list (search-patch "python-contextlib2.patch")))))
+    (build-system python-build-system)
+    (home-page "http://contextlib2.readthedocs.org/")
+    (synopsis "Tools for decorators and context managers")
+    (description "This module is primarily a backport of the Python 3.2 contextlib to
+earlier Python versions.  Like contextlib, it provides utilities for common
+tasks involving decorators and context managers.  It also contains
+additional features that are not part of the standard library.")
+    (license psfl)))
+
+(define-public python2-contextlib2
+  (package-with-python2 python-contextlib2))
-- 
2.5.0

