# Path: bento/core/pkg_objects.py
# class DataFiles(object):
#     @classmethod
#     def from_parse_dict(cls, d):
#         return cls(**d)
# 
#     def __init__(self, name, files=None, target_dir=None, source_dir=None):
#         self.name = name
# 
#         if files is not None:
#             self.files = files
#         else:
#             self.files = []
# 
#         if target_dir is not None:
#             self.target_dir = target_dir
#         else:
#             self.target_dir = "$sitedir"
# 
#         if source_dir is not None:
#             self.source_dir = source_dir
#         else:
#             self.source_dir = "."
# 
#     def __repr__(self):
#         return "DataSection(files=%r, target_dir=%r, source_dir=%r)" % \
#                (self.files, self.target_dir, self.source_dir)
# 
#     def __eq__(self, other):
#         return self.__dict__ == other.__dict__
# 
# Path: bento/testing/sub_test_case.py
# class SubprocessTestCase(unittest.TestCase):
#     """Simple unittest.TestCase subclass to execute every test in a different
#     subprocess.
#     
#     Class-level tearDown and setUp are executed in the same subprocess as their
#     corresponding test.
#     """
#     def run(self, result=None):
#         is_sub = os.environ.get("SUBTESTPIPE", "0")
#         if is_sub == "0":
#             module_name = self.__module__
#             class_name = self.__class__.__name__
#             function_name = self._testMethodName
#             result.startTest(self)
#             try:
#                 out, err, st = _execute_test(module_name, class_name, function_name)
#                 out = out.decode()
#                 err = err.decode()
#                 sys.stdout.write(out)
#                 if st == 0:
#                     result.addSuccess(self)
#                 elif st == 1:
#                     result.errors.append([self, err])
#                     sys.stderr.write("ERROR\n")
#                 elif st == 2:
#                     result.failures.append([self, err])
#                     sys.stderr.write("FAILED\n")
#                 else:
#                     raise ValueError("Unexpected subprocess test failure")
#             finally:
#                 result.stopTest(self)
#         else:
#             return super(SubprocessTestCase, self).run(result)


import os
import sys
import tempfile
import shutil
import os.path as op
from bento.compat.api.moves \
    import \
        unittest
from bento.core.package \
    import \
        PackageDescription
from bento.core.pkg_objects \
    import \
        DataFiles
from bento.testing.sub_test_case \
    import \
        SubprocessTestCase
from bento.core.node \
    import \
        create_first_node
from bento.convert.core \
    import \
        monkey_patch, analyse_setup_py, build_pkg, _convert_numpy_data_files, prune_extra_files

        top.make_node("setup.py").write("""\
import setuptools
from distutils.core import setup

setup(name="foo", include_package_data=True, packages=["foo"])
""")

        monkey_patch(top, "setuptools", "setup.py")
        dist, package_objects = analyse_setup_py("setup.py", ["-n", "-q"])
        pkg, options = build_pkg(dist, package_objects, top)

        self.assertEqual(pkg.data_files, {"foo_data": DataFiles("foo_data", ["foo.info"], "$sitedir/foo", "foo")})
        self.assertEqual(pkg.extra_source_files, ["setup.py", "yeah.txt"])

    def test_scripts(self):
        top = self.top_node

        top.make_node("foo").write("")

        top.make_node("setup.py").write("""\
from distutils.core import setup

setup(name="foo", scripts=["foo"])
""")

        monkey_patch(top, "distutils", "setup.py")
        dist, package_objects = analyse_setup_py("setup.py", ["-n", "-q"])
        pkg, options = build_pkg(dist, package_objects, top)


# Path: bento/core/pkg_objects.py
# class DataFiles(object):
#     @classmethod
#     def from_parse_dict(cls, d):
#         return cls(**d)
# 
#     def __init__(self, name, files=None, target_dir=None, source_dir=None):
#         self.name = name
# 
#         if files is not None:
#             self.files = files
#         else:
#             self.files = []
# 
#         if target_dir is not None:
#             self.target_dir = target_dir
#         else:
#             self.target_dir = "$sitedir"
# 
#         if source_dir is not None:
#             self.source_dir = source_dir
#         else:
#             self.source_dir = "."
# 
#     def __repr__(self):
#         return "DataSection(files=%r, target_dir=%r, source_dir=%r)" % \
#                (self.files, self.target_dir, self.source_dir)
# 
#     def __eq__(self, other):
#         return self.__dict__ == other.__dict__
# 
# Path: bento/testing/sub_test_case.py
# class SubprocessTestCase(unittest.TestCase):
#     """Simple unittest.TestCase subclass to execute every test in a different
#     subprocess.
#     
#     Class-level tearDown and setUp are executed in the same subprocess as their
#     corresponding test.
#     """
#     def run(self, result=None):
#         is_sub = os.environ.get("SUBTESTPIPE", "0")
#         if is_sub == "0":
#             module_name = self.__module__
#             class_name = self.__class__.__name__
#             function_name = self._testMethodName
#             result.startTest(self)
#             try:
#                 out, err, st = _execute_test(module_name, class_name, function_name)
#                 out = out.decode()
#                 err = err.decode()
#                 sys.stdout.write(out)
#                 if st == 0:
#                     result.addSuccess(self)
#                 elif st == 1:
#                     result.errors.append([self, err])
#                     sys.stderr.write("ERROR\n")
#                 elif st == 2:
#                     result.failures.append([self, err])
#                     sys.stderr.write("FAILED\n")
#                 else:
#                     raise ValueError("Unexpected subprocess test failure")
#             finally:
#                 result.stopTest(self)
#         else:
#             return super(SubprocessTestCase, self).run(result)


# Path: bento/convert/tests/test_convert.py
import os
import sys
import tempfile
import shutil
import os.path as op
from bento.compat.api.moves \
    import \
        unittest
from bento.core.package \
    import \
        PackageDescription
from bento.core.pkg_objects \
    import \
        DataFiles
from bento.testing.sub_test_case \
    import \
        SubprocessTestCase
from bento.core.node \
    import \
        create_first_node
from bento.convert.core \
    import \
        monkey_patch, analyse_setup_py, build_pkg, _convert_numpy_data_files, prune_extra_files


        top.make_node("setup.py").write("""\
import setuptools
from distutils.core import setup

setup(name="foo", include_package_data=True, packages=["foo"])
""")

        monkey_patch(top, "setuptools", "setup.py")
        dist, package_objects = analyse_setup_py("setup.py", ["-n", "-q"])
        pkg, options = build_pkg(dist, package_objects, top)

        self.assertEqual(pkg.data_files, {"foo_data": DataFiles("foo_data", ["foo.info"], "$sitedir/foo", "foo")})
        self.assertEqual(pkg.extra_source_files, ["setup.py", "yeah.txt"])

    def test_scripts(self):
        top = self.top_node

        top.make_node("foo").write("")

        top.make_node("setup.py").write("""\
from distutils.core import setup

setup(name="foo", scripts=["foo"])
""")

        monkey_patch(top, "distutils", "setup.py")
        dist, package_objects = analyse_setup_py("setup.py", ["-n", "-q"])
        pkg, options = build_pkg(dist, package_objects, top)


        self.assertEqual(pkg.data_files, {"foo_scripts": DataFiles("foo_scripts", ["foo"], "$bindir", ".")})