# Path: bento/installed_package_description.py
# class InstalledSection(object):
#     @classmethod
#     def from_source_target_directories(cls, category, name, source_dir, target_dir, files):
#         files = [(f, f) for f in files]
#         return cls(category, name, source_dir, target_dir, files)
# 
#     def __init__(self, category, name, srcdir, target, files):
#         self.category = category
#         self.name = name
#         if os.sep != "/":
#             self.source_dir = bento.utils.path.normalize_path(srcdir)
#             self.target_dir = bento.utils.path.normalize_path(target)
#             self.files = [(bento.utils.path.normalize_path(f), bento.utils.path.normalize_path(g)) for f, g in files]
#         else:
#             self.source_dir = srcdir
#             self.target_dir = target
#             self.files = files
# 
#     def __repr__(self):
#         ret = """\
# InstalledSection(%(category)s,
#     %(name)s,
#     %(source_dir)s,
#     %(target_dir)s,
#     %(files)s)""" % {"name": self.name, "category": self.category,
#             "source_dir": self.source_dir, "target_dir": self.target_dir,
#             "files": self.files}
#         return ret
# 
#     def __eq__(self, other):
#         return self.category == other.category and \
#                 self.name == other.name and \
#                 self.source_dir == other.source_dir and \
#                 self.target_dir == other.target_dir and \
#                 self.files == other.files
# 
# Path: bento/utils/utils.py
# def cpu_count():
#     '''
#     Returns the number of CPUs in the system
#     '''
#     if sys.platform == 'win32':
#         try:
#             num = int(os.environ['NUMBER_OF_PROCESSORS'])
#         except (ValueError, KeyError):
#             num = 0
#     elif 'bsd' in sys.platform or sys.platform == 'darwin':
#         try:
#             num = int(os.popen('sysctl -n hw.ncpu').read())
#         except ValueError:
#             num = 0
#     else:
#         try:
#             num = os.sysconf('SC_NPROCESSORS_ONLN')
#         except (ValueError, OSError, AttributeError):
#             num = 0
# 
#     if num >= 1:
#         return num
#     else:
#         return 1
#         #raise NotImplementedError('cannot determine number of cpus')
# 
# def extract_exception():
#     """Extract the last exception.
# 
#     Used to avoid the except ExceptionType as e, which cannot be written the
#     same across supported versions. I.e::
# 
#         try:
#             ...
#         except Exception, e:
#             ...
# 
#     becomes:
# 
#         try:
#             ...
#         except Exception:
#             e = extract_exception()
#     """
#     return sys.exc_info()[1]


import sys
import os
import shutil
import bento.errors
import yaku.task_manager
import yaku.context
import yaku.scheduler
import yaku.errors
from bento.installed_package_description \
    import \
        InstalledSection
from bento.errors \
    import \
        CommandExecutionFailure
from bento.utils.utils \
    import \
        cpu_count, extract_exception


def build_extension(bld, extension, env=None):
    builder = bld.builders["pyext"]
    try:
        if env is None:
            env = {"PYEXT_CPPPATH": extension.include_dirs}
        else:
            val = env.get("PYEXT_CPPPATH", [])
            val.extend(extension.include_dirs)
        tasks = builder.extension(extension.name, extension.sources, env)
        if len(tasks) > 1:
            outputs = tasks[0].gen.outputs
        else:
            outputs = []
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building extension %s failed: %s" % \
              (extension.name, str(e))
        raise CommandExecutionFailure(msg)

def build_compiled_library(bld, clib, env=None):
    builder = bld.builders["ctasks"]
    try:
        for p in clib.include_dirs:
            builder.env["CPPPATH"].insert(0, p)
        outputs = builder.static_library(clib.name, clib.sources, env)
        return [n.bldpath() for n in outputs]
    except RuntimeError:

# Path: bento/installed_package_description.py
# class InstalledSection(object):
#     @classmethod
#     def from_source_target_directories(cls, category, name, source_dir, target_dir, files):
#         files = [(f, f) for f in files]
#         return cls(category, name, source_dir, target_dir, files)
# 
#     def __init__(self, category, name, srcdir, target, files):
#         self.category = category
#         self.name = name
#         if os.sep != "/":
#             self.source_dir = bento.utils.path.normalize_path(srcdir)
#             self.target_dir = bento.utils.path.normalize_path(target)
#             self.files = [(bento.utils.path.normalize_path(f), bento.utils.path.normalize_path(g)) for f, g in files]
#         else:
#             self.source_dir = srcdir
#             self.target_dir = target
#             self.files = files
# 
#     def __repr__(self):
#         ret = """\
# InstalledSection(%(category)s,
#     %(name)s,
#     %(source_dir)s,
#     %(target_dir)s,
#     %(files)s)""" % {"name": self.name, "category": self.category,
#             "source_dir": self.source_dir, "target_dir": self.target_dir,
#             "files": self.files}
#         return ret
# 
#     def __eq__(self, other):
#         return self.category == other.category and \
#                 self.name == other.name and \
#                 self.source_dir == other.source_dir and \
#                 self.target_dir == other.target_dir and \
#                 self.files == other.files
# 
# Path: bento/utils/utils.py
# def cpu_count():
#     '''
#     Returns the number of CPUs in the system
#     '''
#     if sys.platform == 'win32':
#         try:
#             num = int(os.environ['NUMBER_OF_PROCESSORS'])
#         except (ValueError, KeyError):
#             num = 0
#     elif 'bsd' in sys.platform or sys.platform == 'darwin':
#         try:
#             num = int(os.popen('sysctl -n hw.ncpu').read())
#         except ValueError:
#             num = 0
#     else:
#         try:
#             num = os.sysconf('SC_NPROCESSORS_ONLN')
#         except (ValueError, OSError, AttributeError):
#             num = 0
# 
#     if num >= 1:
#         return num
#     else:
#         return 1
#         #raise NotImplementedError('cannot determine number of cpus')
# 
# def extract_exception():
#     """Extract the last exception.
# 
#     Used to avoid the except ExceptionType as e, which cannot be written the
#     same across supported versions. I.e::
# 
#         try:
#             ...
#         except Exception, e:
#             ...
# 
#     becomes:
# 
#         try:
#             ...
#         except Exception:
#             e = extract_exception()
#     """
#     return sys.exc_info()[1]


# Path: bento/commands/build_yaku.py
import sys
import os
import shutil
import bento.errors
import yaku.task_manager
import yaku.context
import yaku.scheduler
import yaku.errors
from bento.installed_package_description \
    import \
        InstalledSection
from bento.errors \
    import \
        CommandExecutionFailure
from bento.utils.utils \
    import \
        cpu_count, extract_exception



def build_extension(bld, extension, env=None):
    builder = bld.builders["pyext"]
    try:
        if env is None:
            env = {"PYEXT_CPPPATH": extension.include_dirs}
        else:
            val = env.get("PYEXT_CPPPATH", [])
            val.extend(extension.include_dirs)
        tasks = builder.extension(extension.name, extension.sources, env)
        if len(tasks) > 1:
            outputs = tasks[0].gen.outputs
        else:
            outputs = []
        return [n.bldpath() for n in outputs]
    except RuntimeError:
        e = extract_exception()
        msg = "Building extension %s failed: %s" % \
              (extension.name, str(e))
        raise CommandExecutionFailure(msg)

def build_compiled_library(bld, clib, env=None):
    builder = bld.builders["ctasks"]
    try:
        for p in clib.include_dirs:
            builder.env["CPPPATH"].insert(0, p)
        outputs = builder.static_library(clib.name, clib.sources, env)
        return [n.bldpath() for n in outputs]
    except RuntimeError:

        e = extract_exception()