When a module is asked to be imported, check if we have previously imported
    it.  If so, check if the time stamp of it, a companion yaml file, or any
    modules it imports have changed.  If so, reimport the module.

    :params: see __builtin__.__import__

def watch_import(name, globals=None, *args, **kwargs):
    """
    When a module is asked to be imported, check if we have previously imported
    it.  If so, check if the time stamp of it, a companion yaml file, or any
    modules it imports have changed.  If so, reimport the module.

    :params: see __builtin__.__import__
    """
    # Don"t monitor builtin modules.  types seem special, so don"t monitor it
    # either.
    monitor = not imp.is_builtin(name) and name not in ("types", )
    # Don"t monitor modules if we don"t know where they came from
    monitor = monitor and isinstance(globals, dict) and globals.get("__name__")
    if not monitor:
        return builtin_import(name, globals, *args, **kwargs)
    # This will be the dotted module name except for service modules where it
    # will be the absolute file path.
    parent = globals["__name__"]
    key = parent + "." + name
    module_reload_changed(key)
    try:
        module = builtin_import(name, globals, *args, **kwargs)
    except ImportError:
        raise
    if getattr(module, "__file__", None):
        if key not in WatchList:
            tangelo.log_info("WATCH", "Monitoring import %s from %s" % (name, parent))
        imp.acquire_lock()
        try:
            if key not in WatchList:
                filemtime = module_getmtime(module.__file__) or 0
                filemtime = latest_submodule_time(key, filemtime)
                WatchList[key] = {
                    "time": filemtime
                }
            WatchList[key].update({
                "parent": parent,
                "name": name,
                "file": module.__file__
            })
        finally:
            imp.release_lock()
    return module