예제 #1
0
def _process_single_config(config: dict):
    conf = config[CONF_SOURCE]
    if conf[CONF_TYPE] == TYPE_GIT:
        with cv.prepend_path([CONF_SOURCE]):
            components_dir = _process_git_config(config[CONF_SOURCE],
                                                 config[CONF_REFRESH])
    elif conf[CONF_TYPE] == TYPE_LOCAL:
        components_dir = Path(CORE.relative_config_path(conf[CONF_PATH]))
    else:
        raise NotImplementedError()

    if config[CONF_COMPONENTS] == "all":
        num_components = len(list(components_dir.glob("*/__init__.py")))
        if num_components > 100:
            # Prevent accidentally including all components from an esphome fork/branch
            # In this case force the user to manually specify which components they want to include
            raise cv.Invalid(
                "This source is an ESPHome fork or branch. Please manually specify the components you want to import using the 'components' key",
                [CONF_COMPONENTS],
            )
        allowed_components = None
    else:
        for i, name in enumerate(config[CONF_COMPONENTS]):
            expected = components_dir / name / "__init__.py"
            if not expected.is_file():
                raise cv.Invalid(
                    f"Could not find __init__.py file for component {name}. Please check the component is defined by this source (search path: {expected})",
                    [CONF_COMPONENTS, i],
                )
        allowed_components = config[CONF_COMPONENTS]

    loader.install_meta_finder(components_dir,
                               allowed_components=allowed_components)
예제 #2
0
def _process_single_config(config: dict):
    conf = config[CONF_SOURCE]
    if conf[CONF_TYPE] == TYPE_GIT:
        key = f"{conf[CONF_URL]}@{conf.get(CONF_REF)}"
        repo_dir = _compute_destination_path(key)
        if not repo_dir.is_dir():
            cmd = ["git", "clone", "--depth=1"]
            if CONF_REF in conf:
                cmd += ["--branch", conf[CONF_REF]]
            cmd += [conf[CONF_URL], str(repo_dir)]
            ret = subprocess.run(cmd, capture_output=True, check=False)
            _handle_git_response(ret)

        else:
            # Check refresh needed
            file_timestamp = Path(repo_dir / ".git" / "FETCH_HEAD")
            # On first clone, FETCH_HEAD does not exists
            if not file_timestamp.exists():
                file_timestamp = Path(repo_dir / ".git" / "HEAD")
            age = datetime.datetime.now() - datetime.datetime.fromtimestamp(
                file_timestamp.stat().st_mtime
            )
            if age.seconds > config[CONF_REFRESH].total_seconds:
                _LOGGER.info("Executing git pull %s", key)
                cmd = ["git", "pull"]
                ret = subprocess.run(
                    cmd, cwd=repo_dir, capture_output=True, check=False
                )
                _handle_git_response(ret)

        if (repo_dir / "esphome" / "components").is_dir():
            components_dir = repo_dir / "esphome" / "components"
        elif (repo_dir / "components").is_dir():
            components_dir = repo_dir / "components"
        else:
            raise cv.Invalid(
                "Could not find components folder for source. Please check the source contains a 'components' or 'esphome/components' folder",
                [CONF_SOURCE],
            )

    elif conf[CONF_TYPE] == TYPE_LOCAL:
        components_dir = Path(CORE.relative_config_path(conf[CONF_PATH]))
    else:
        raise NotImplementedError()

    if config[CONF_COMPONENTS] == "all":
        num_components = len(list(components_dir.glob("*/__init__.py")))
        if num_components > 100:
            # Prevent accidentally including all components from an esphome fork/branch
            # In this case force the user to manually specify which components they want to include
            raise cv.Invalid(
                "This source is an ESPHome fork or branch. Please manually specify the components you want to import using the 'components' key",
                [CONF_COMPONENTS],
            )
        allowed_components = None
    else:
        for i, name in enumerate(config[CONF_COMPONENTS]):
            expected = components_dir / name / "__init__.py"
            if not expected.is_file():
                raise cv.Invalid(
                    f"Could not find __init__.py file for component {name}. Please check the component is defined by this source (search path: {expected})",
                    [CONF_COMPONENTS, i],
                )
        allowed_components = config[CONF_COMPONENTS]

    loader.install_meta_finder(components_dir, allowed_components=allowed_components)