from subprocess import check_call
import os
import shutil
import sys
import sysconfig
import unittest

cwd = os.path.dirname(__file__)
python_version = "python{}.{}".format(sys.version_info.major,
                                      sys.version_info.minor)
if sys.version_info.major == 3:
    so_version = ".{}m-{}".format(sys.implementation.cache_tag,
                                  sysconfig.get_config_var('MULTIARCH'))
else:
    so_version = ""


class TestDistutils(unittest.TestCase):

    def test_setup_build(self):
        check_call(['python', 'setup.py', 'build'],
                   cwd=os.path.join(cwd, 'test_distutils'))
        check_call(['python', 'setup.py', 'install', '--prefix=demo_install'],
                   cwd=os.path.join(cwd, 'test_distutils'))
        check_call(['python', '-c', 'import demo'],
                   cwd=os.path.join(cwd, 'test_distutils', 'demo_install',
                                    'lib', python_version, 'site-packages'))
        check_call(['python', 'setup.py', 'clean'],
                   cwd=os.path.join(cwd, 'test_distutils'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils', 'demo_install'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils', 'build'))

    def test_setup_sdist_install(self):
        check_call(['python', 'setup.py', 'sdist', "--dist-dir=sdist"],
                   cwd=os.path.join(cwd, 'test_distutils'))
        check_call(['tar', 'xzf', 'demo-1.0.tar.gz'],
                   cwd=os.path.join(cwd, 'test_distutils', 'sdist'))
        check_call(['python', 'setup.py', 'install', '--prefix=demo_install'],
                   cwd=os.path.join(cwd, 'test_distutils', 'sdist', 'demo-1.0'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils', 'sdist'))

    def test_setup_bdist_install(self):
        check_call(['python', 'setup.py', 'bdist', "--dist-dir=bdist"],
                   cwd=os.path.join(cwd, 'test_distutils'))
        dist_path = os.path.join(cwd, 'test_distutils', 'bdist')
        tgz = [f for f in os.listdir(dist_path) if f.endswith(".tar.gz")][0]
        check_call(['tar', 'xzf', tgz], cwd=dist_path)

        def find(name, path):
            for root, dirs, files in os.walk(path):
                if name in files:
                    return os.path.join(root, name)
        demo_so = find("demo{}.so".format(so_version), dist_path)
        self.assertIsNotNone(demo_so)
        shutil.rmtree(dist_path)

    def test_setup_build2(self):
        check_call(['python', 'setup.py', 'build'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged'))
        check_call(['python', 'setup.py', 'install', '--prefix=demo_install2'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged'))
        check_call(['python', '-c', 'import demo2.a'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged', 'demo_install2',
                                    'lib', python_version, 'site-packages'))
        check_call(['python', 'setup.py', 'clean'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils_packaged', 'demo_install2'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils_packaged', 'build'))


    def test_setup_sdist_install2(self):
        check_call(['python', 'setup.py', 'sdist', "--dist-dir=sdist2"],
                   cwd=os.path.join(cwd, 'test_distutils_packaged'))
        check_call(['tar', 'xzf', 'demo2-1.0.tar.gz'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged', 'sdist2'))
        check_call(['python', 'setup.py', 'install', '--prefix=demo_install2'],
                   cwd=os.path.join(cwd, 'test_distutils_packaged', 'sdist2', 'demo2-1.0'))
        shutil.rmtree(os.path.join(cwd, 'test_distutils_packaged', 'sdist2'))

    def test_setup_bdist_install2(self):
        check_call(['python', 'setup.py', 'bdist', "--dist-dir=bdist"],
                   cwd=os.path.join(cwd, 'test_distutils_packaged'))
        dist_path = os.path.join(cwd, 'test_distutils_packaged', 'bdist')
        tgz = [f for f in os.listdir(dist_path) if f.endswith(".tar.gz")][0]
        check_call(['tar', 'xzf', tgz], cwd=dist_path)

        def find(name, path):
            for root, dirs, files in os.walk(path):
                if name in files:
                    return os.path.join(root, name)
        demo_so = find("a{}.so".format(so_version), dist_path)
        self.assertIsNotNone(demo_so)
        shutil.rmtree(dist_path)

@@ -769,32 +769,33 @@
 istutils'))%0A
+%0A
         
 check_call(%5B
@@ -786,75 +786,15 @@
     
-check_call(%5B'python', '-c', 'import demo'%5D,%0A                   cwd=
+base = 
 os.p
@@ -840,16 +840,17 @@
 nstall',
+)
 %0A       
@@ -854,41 +854,224 @@
     
-                            'lib'
+libdir = os.path.join(base, 'lib')%0A        if not os.path.isdir(libdir):%0A            libdir = os.path.join(base, 'lib64')%0A        check_call(%5B'python', '-c', 'import demo'%5D,%0A                   cwd=os.path.join(libdir
 , py
@@ -2857,32 +2857,33 @@
 packaged'))%0A
+%0A
         
 check_call(%5B
@@ -2874,78 +2874,15 @@
     
-check_call(%5B'python', '-c', 'import demo2.a'%5D,%0A                   cwd=
+base = 
 os.p
@@ -2938,16 +2938,17 @@
 stall2',
+)
 %0A       
@@ -2952,41 +2952,227 @@
     
-                            'lib'
+libdir = os.path.join(base, 'lib')%0A        if not os.path.isdir(libdir):%0A            libdir = os.path.join(base, 'lib64')%0A        check_call(%5B'python', '-c', 'import demo2.a'%5D,%0A                   cwd=os.path.join(libdir
 , py

Make distutils test portable on Fedora