# Path: turbulenz_tools/utils/dependencies.py
# def find_file_in_dirs(filename, dirs, error_on_multiple = False):
#     found = []
#     for d in dirs:
#         fn = join(d, filename)
#         if exists(fn):
#             if error_on_multiple:
#                 if fn not in found:
#                     found.append(fn)
#             else:
#                 return abspath(fn)
#     if error_on_multiple:
#         if len(found) > 1:
#             raise Exception("File '%s' matched several locations: %s" % found)
#         if len(found) == 1:
#             return abspath(found[0])
#     return None
# 
# Path: turbulenz_tools/utils/profiler.py
# class Profiler(object):
# 
#     _profiler_impl = ProfilerDummyImpl()
# 
#     @classmethod
#     def enable(cls):
#         if not isinstance(cls._profiler_impl, ProfilerDummyImpl):
#             raise Exception("Profiler.enable_profiler() called twice")
#         cls._profiler_impl = ProfilerImpl()
# 
#     @classmethod
#     def start(cls, section_name):
#         cls._profiler_impl.start(section_name)
# 
#     @classmethod
#     def stop(cls, section_name):
#         cls._profiler_impl.stop(section_name)
# 
#     @classmethod
#     def get_root_nodes(cls):
#         return cls._profiler_impl.get_root_nodes()
# 
#     @classmethod
#     def dump_data(cls):
#         cls._profiler_impl.dump_data()
# 
# Path: turbulenz_tools/tools/toolsexception.py
# class ToolsException(Exception):
#     pass
# 
# Path: turbulenz_tools/tools/templates.py
# def read_file_utf8(filename):
#     with open(filename, 'rb') as f:
#         text = f.read().decode('utf-8-sig')
# 
#         # This is unpleasant, but it is thorough.  This is the
#         # single-pass equivalent of:
#         # return (in_data.replace('\r\n', '\n').replace('\n\r', '\n'))\
#         #     .replace('\r', '\n');
#         return LF_RE.sub(_sanitize_crlf, text)


from turbulenz_tools.utils.dependencies import find_file_in_dirs
from turbulenz_tools.utils.profiler import Profiler
from turbulenz_tools.tools.toolsexception import ToolsException
from turbulenz_tools.tools.templates import read_file_utf8
from re import compile as re_compile
from logging import getLogger
    from turbulenz_tools.version import SDK_VERSION as engine_version
import os.path
import glob

    if options.mode == 'plugin':
        out.append("""
    if (!TurbulenzEngine.onload)
    {
        window.alert("Entry point 'TurbulenzEngine.onload' must be defined.");
        return;
    }
    TurbulenzEngine.onload.call(this);
}());""")

    if options.mode == 'canvas':
        out.append('window.TurbulenzEngine = TurbulenzEngine;}());')

    # Combine all parts into a single string

    return ("\n".join(out), inc_js)

def render_js_extract_includes(context, options, templates_js, injects):
    """
    Renders the templates in templates_js against the given context
    and just collects the set of 'javascript('...')' includes.  Will
    optionally handle a list of files to be injected.

    Returns an array of absolute paths, removing duplicates.
    """

    includes = []

    def _find_in_dirs_or_error(name):

# Path: turbulenz_tools/utils/dependencies.py
# def find_file_in_dirs(filename, dirs, error_on_multiple = False):
#     found = []
#     for d in dirs:
#         fn = join(d, filename)
#         if exists(fn):
#             if error_on_multiple:
#                 if fn not in found:
#                     found.append(fn)
#             else:
#                 return abspath(fn)
#     if error_on_multiple:
#         if len(found) > 1:
#             raise Exception("File '%s' matched several locations: %s" % found)
#         if len(found) == 1:
#             return abspath(found[0])
#     return None
# 
# Path: turbulenz_tools/utils/profiler.py
# class Profiler(object):
# 
#     _profiler_impl = ProfilerDummyImpl()
# 
#     @classmethod
#     def enable(cls):
#         if not isinstance(cls._profiler_impl, ProfilerDummyImpl):
#             raise Exception("Profiler.enable_profiler() called twice")
#         cls._profiler_impl = ProfilerImpl()
# 
#     @classmethod
#     def start(cls, section_name):
#         cls._profiler_impl.start(section_name)
# 
#     @classmethod
#     def stop(cls, section_name):
#         cls._profiler_impl.stop(section_name)
# 
#     @classmethod
#     def get_root_nodes(cls):
#         return cls._profiler_impl.get_root_nodes()
# 
#     @classmethod
#     def dump_data(cls):
#         cls._profiler_impl.dump_data()
# 
# Path: turbulenz_tools/tools/toolsexception.py
# class ToolsException(Exception):
#     pass
# 
# Path: turbulenz_tools/tools/templates.py
# def read_file_utf8(filename):
#     with open(filename, 'rb') as f:
#         text = f.read().decode('utf-8-sig')
# 
#         # This is unpleasant, but it is thorough.  This is the
#         # single-pass equivalent of:
#         # return (in_data.replace('\r\n', '\n').replace('\n\r', '\n'))\
#         #     .replace('\r', '\n');
#         return LF_RE.sub(_sanitize_crlf, text)


# Path: turbulenz_tools/tools/appcodegen.py
from turbulenz_tools.utils.dependencies import find_file_in_dirs
from turbulenz_tools.utils.profiler import Profiler
from turbulenz_tools.tools.toolsexception import ToolsException
from turbulenz_tools.tools.templates import read_file_utf8
from re import compile as re_compile
from logging import getLogger
    from turbulenz_tools.version import SDK_VERSION as engine_version
import os.path
import glob


    if options.mode == 'plugin':
        out.append("""
    if (!TurbulenzEngine.onload)
    {
        window.alert("Entry point 'TurbulenzEngine.onload' must be defined.");
        return;
    }
    TurbulenzEngine.onload.call(this);
}());""")

    if options.mode == 'canvas':
        out.append('window.TurbulenzEngine = TurbulenzEngine;}());')

    # Combine all parts into a single string

    return ("\n".join(out), inc_js)

def render_js_extract_includes(context, options, templates_js, injects):
    """
    Renders the templates in templates_js against the given context
    and just collects the set of 'javascript('...')' includes.  Will
    optionally handle a list of files to be injected.

    Returns an array of absolute paths, removing duplicates.
    """

    includes = []

    def _find_in_dirs_or_error(name):

        file_path = find_file_in_dirs(name, options.templatedirs)