import codecs
import os
import shutil
import sys
import warnings

import setuptools
from setuptools import setup, Command
from setuptools.command.egg_info import egg_info


PACKAGE_NAME = 'oscrypto'
PACKAGE_VERSION = '0.20.0.dev1'
TEST_PACKAGE_NAME = '%s_tests' % PACKAGE_NAME


# setuptools 38.6.0 and newer know about long_description_content_type, but
# distutils still complains about it, so silence the warning
sv = setuptools.__version__
svi = tuple(int(o) if o.isdigit() else o for o in sv.split('.'))
if svi >= (38, 6):
    warnings.filterwarnings(
        'ignore',
        "Unknown distribution option: 'long_description_content_type'",
        module='distutils.dist'
    )


package_data = {
    TEST_PACKAGE_NAME: [
        'fixtures/*',
        'fixtures/*/*',
    ]
}
# This allows us to send the LICENSE when creating a sdist. Wheels
# automatically include the license, and don't need the docs. For these
# to be included, the command must be "python setup.py sdist".
if sys.argv[1:] == ['sdist'] or sorted(sys.argv[1:]) == ['-q', 'sdist']:
    package_data[TEST_PACKAGE_NAME].extend([
        'LICENSE',
        'readme.md',
    ])


tests_root = os.path.dirname(os.path.abspath(__file__))
package_root = os.path.abspath(os.path.join(tests_root, '..'))


# Ensures a copy of the LICENSE is included with the egg-info for
# install and bdist_egg commands
class EggInfoCommand(egg_info):
    def run(self):
        egg_info_path = os.path.join(
            tests_root,
            '%s.egg-info' % TEST_PACKAGE_NAME
        )
        if not os.path.exists(egg_info_path):
            os.mkdir(egg_info_path)
        shutil.copy2(
            os.path.join(package_root, 'LICENSE'),
            os.path.join(egg_info_path, 'LICENSE')
        )
        egg_info.run(self)


class CleanCommand(Command):
    user_options = [
        ('all', 'a', '(Compatibility with original clean command)'),
    ]

    def initialize_options(self):
        self.all = False

    def finalize_options(self):
        pass

    def run(self):
        sub_folders = ['build', 'temp', '%s.egg-info' % TEST_PACKAGE_NAME]
        if self.all:
            sub_folders.append('dist')
        for sub_folder in sub_folders:
            full_path = os.path.join(tests_root, sub_folder)
            if os.path.exists(full_path):
                shutil.rmtree(full_path)
        for root, dirs, files in os.walk(tests_root):
            for filename in files:
                if filename[-4:] == '.pyc':
                    os.unlink(os.path.join(root, filename))
            for dirname in list(dirs):
                if dirname == '__pycache__':
                    shutil.rmtree(os.path.join(root, dirname))


readme = ''
with codecs.open(os.path.join(tests_root, 'readme.md'), 'r', 'utf-8') as f:
    readme = f.read()


setup(
    name=TEST_PACKAGE_NAME,
    version=PACKAGE_VERSION,

    description=(
        'Test suite for oscrypto, separated due to file size'
    ),
    long_description=readme,
    long_description_content_type='text/markdown',

    url='https://github.com/wbond/oscrypto',

    author='wbond',
    author_email='will@wbond.net',

    license='MIT',

    classifiers=[
        'Development Status :: 4 - Beta',

        'Intended Audience :: Developers',

        'License :: OSI Approved :: MIT License',

        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: Implementation :: PyPy',

        'Topic :: Security :: Cryptography',
    ],

    keywords='crypto pki tls ssl x509 certificate encrypt decrypt sign verify rsa dsa ec dh',

    packages=[TEST_PACKAGE_NAME],
    package_dir={TEST_PACKAGE_NAME: '.'},
    package_data=package_data,

    install_requires=[
        '%s==%s' % (PACKAGE_NAME, PACKAGE_VERSION),
    ],

    cmdclass={
        'clean': CleanCommand,
        'egg_info': EggInfoCommand,
    }
)

@@ -270,16 +270,135 @@
 GE_NAME%0A
+TESTS_ROOT = os.path.dirname(os.path.abspath(__file__))%0APACKAGE_ROOT = os.path.abspath(os.path.join(TESTS_ROOT, '..'))%0A
 %0A%0A# setu
@@ -803,104 +803,475 @@
 )%0A%0A%0A
-package_data = %7B%0A    TEST_PACKAGE_NAME: %5B%0A        'fixtures/*',%0A        'fixtures/*/*',%0A    %5D
+# Older versions of distutils would take a glob pattern and return dirs%0A# and then would complain that it couldn't copy a dir like a file, so we%0A# have to build an explicit list of file names%0Adata_files = %5B%5D%0Afixtures_dir = os.path.join(TESTS_ROOT, 'fixtures')%0Afor root, dirs, files in os.walk(fixtures_dir):%0A    for filename in files:%0A        data_files.append(os.path.join(root, filename)%5Blen(TESTS_ROOT) + 1:%5D)%0Apackage_data = %7B%0A    TEST_PACKAGE_NAME: data_files
 %0A%7D%0A
+%0A
 # Th
@@ -1639,129 +1639,8 @@
 )%0A%0A%0A
-tests_root = os.path.dirname(os.path.abspath(__file__))%0Apackage_root = os.path.abspath(os.path.join(tests_root, '..'))%0A%0A%0A
 # En
@@ -1835,26 +1835,26 @@
         
-tests_root
+TESTS_ROOT
 ,%0A      
@@ -2036,20 +2036,20 @@
 oin(
-package_root
+PACKAGE_ROOT
 , 'L
@@ -2610,26 +2610,26 @@
 th.join(
-tests_root
+TESTS_ROOT
 , sub_fo
@@ -2758,26 +2758,26 @@
 os.walk(
-tests_root
+TESTS_ROOT
 ):%0A     
@@ -3105,18 +3105,18 @@
 oin(
-tests_root
+TESTS_ROOT
 , 'r

Improve compatibility with old versions of distutils