def find_local_child_template()

in qs_cfn_lint_rules/stack/StackHelper.py [0:0]


def find_local_child_template(parent_template_path, child_template_path):
    final_template_path = ""

    # Start where the Parent template is
    project_root = Path(
        os.path.dirname(parent_template_path)
    )

    # Get rid of any "//"
    child_template_path_tmp = os.path.normpath(child_template_path)

    # Take the path piece by piece and try in current folder
    while "/" in str(child_template_path_tmp):
        child_template_path_tmp = remove_one_level(child_template_path_tmp)
        final_template_path = Path(
            "/".join(
                [str(project_root), str(child_template_path_tmp)]
            )
        )
        if final_template_path.exists() and final_template_path.is_file():
            return str(final_template_path)

    # Take the path piece by piece and try in one folder up folder
    project_root = Path(
        os.path.normpath(
            os.path.dirname(parent_template_path) + "/../"
        )
    )
    # Get rid of any "//"
    child_template_path_tmp = os.path.normpath(child_template_path)

    while "/" in str(child_template_path_tmp):
        child_template_path_tmp = remove_one_level(child_template_path_tmp)
        final_template_path = Path(
            "/".join(
                [str(project_root), str(child_template_path_tmp)]
            )
        )
        if final_template_path.exists() and final_template_path.is_file():
            return str(final_template_path)

    message = "Failed to discover local path for %s."
    raise Exception(message % child_template_path)