With a bit of import magic, we load the given path as a python module,
    while providing it access to the given stack under the import name '_context'.
    This function returns the module's global dictionary.

def _run_resources_file(path, stack):
    """
    With a bit of import magic, we load the given path as a python module,
    while providing it access to the given stack under the import name '_context'.
    This function returns the module's global dictionary.
    """
    import imp, sys, random, string, copy
    class ContextImporter(object):
        def find_module(self, fullname, path=None):
            if fullname == '_context':
                self.path = path
                return self
            return None
        def load_module(self, name):
            mod = imp.new_module("_context")
            mod.stack = stack
            return mod
    # Temporarily add the context importer into the meta path
    old_meta_path = copy.copy(sys.meta_path)
    old_path = copy.copy(sys.path)
    # Save the modules list, in order to remove modules loaded after this point
    old_sys_modules = sys.modules.keys()
    # Run the module
    # Prepare import environment
    abspath = os.path.abspath(path)
    sys.meta_path.append(ContextImporter())
    sys.path.append(os.path.dirname(abspath))   # Enable importing files within module's folder
    # Perform the importing
    srcf = open(abspath, "r")
    module_name = ''.join(random.choice(string.digits + string.lowercase) for i in range(16))
    srcmodule = imp.load_source("cloudcast._template_" + module_name, abspath, srcf)
    srcf.close()
    # Restore meta path, modules list and return module
    sys.meta_path = old_meta_path
    sys.path = old_path
    for modname in sys.modules.keys():
        if not modname in old_sys_modules:
            del sys.modules[modname]
    return srcmodule