# -*- coding: utf-8 -*-
import os.path
import pip
from pip.exceptions import InstallationError
from pip.status_codes import SUCCESS


def check_install(config_data):
    """
    Here we do some **really** basic environment sanity checks.

    Basically we test for the more delicate and failing-prone dependencies:
     * database driver
     * Pillow image format support

    Many other errors will go undetected
    """
    errors = []
    size = 128, 128

    # PIL tests
    try:
        from PIL import Image

        try:
            im = Image.open(os.path.join(os.path.dirname(__file__), "../share/test_image.png"))
            im.thumbnail(size)
        except IOError:  # pragma: no cover
            errors.append("Pillow is not compiled with PNG support, see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html.")
        try:
            im = Image.open(os.path.join(os.path.dirname(__file__), "../share/test_image.jpg"))
            im.thumbnail(size)
        except IOError:  # pragma: no cover
            errors.append("Pillow is not compiled with JPEG support, see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html")
    except ImportError:  # pragma: no cover
        errors.append("Pillow is not installed check for installation errors and see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html")

    # PostgreSQL test
    if config_data.db_driver == 'psycopg2' and not config_data.no_db_driver:  # pragma: no cover
        try:
            import psycopg2  # NOQA
        except ImportError:
            errors.append("PostgreSQL driver is not installed, but you configured a PostgreSQL database, please check your installation and see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html")

    # MySQL test
    if config_data.db_driver == 'MySQL-python' and not config_data.no_db_driver:  # pragma: no cover
        try:
            import MySQLdb  # NOQA
        except ImportError:
            errors.append("MySQL driver is not installed, but you configured a MySQL database, please check your installation and see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html")
    if errors:  # pragma: no cover
        raise EnvironmentError("\n".join(errors))


def requirements(requirements, is_file=False):
    if is_file:  # pragma: no cover
        args = ['install', '-q', '-r', requirements]
    else:
        args = ['install', '-q']
        args.extend(requirements.split())
    exit_status = pip.main(args)
    if exit_status != SUCCESS:
        raise InstallationError("Error while installing requirements. Check pip log file for error details.")
    return True


def cleanup(requirements):  # pragma: no cover
    args = ['uninstall', '-q', '-y']
    args.extend(requirements.split())
    exit_status = pip.main(args)
    return True

@@ -36,101 +36,8 @@
 ath%0A
-import pip%0Afrom pip.exceptions import InstallationError%0Afrom pip.status_codes import SUCCESS%0A
 %0A%0Ade
@@ -2488,16 +2488,122 @@
 False):%0A
+    import pip%0A    from pip.exceptions import InstallationError%0A    from pip.status_codes import SUCCESS%0A%0A
     if i
@@ -2622,32 +2622,32 @@
 ragma: no cover%0A
-
         args = %5B
@@ -2738,32 +2738,63 @@
     args.extend(
+%5B'%25s' %25 package for package in 
 requirements.spl
@@ -2789,32 +2789,33 @@
 irements.split()
+%5D
 )%0A    exit_statu
@@ -2992,16 +2992,16 @@
  True%0A%0A%0A
-
 def clea
@@ -3035,24 +3035,40 @@
 a: no cover%0A
+    import pip%0A%0A
     args = %5B

Move pip imports