def parse_tlpdb_to_dict(tlpdb_path):
    """Reads given tlpdb database and creates dict with packages, their dependencies and files
    """

    with open(tlpdb_path, "r") as f:
        packages = f.read().split("\n\n")

    pkg_dict = {}
    for pkg in packages:
        if not pkg == "":
            pkg_lines = pkg.split("\n")
            pkg_name = pkg_lines[0].split(" ")[1]
            # We only care about files and depends
            pkg_dict[pkg_name] = {"depends" : [], "files" : []}
            i = 0
            while i < len(pkg_lines):
                if pkg_lines[i].split(" ")[0].startswith("runfiles"):
                    # Start of file list
                    i += 1
                    while i < len(pkg_lines) and pkg_lines[i].startswith(" "):
                        # files starts with space, for example
                        # " texmf-dist/tex/latex/collref/collref.sty"
                        pkg_dict[pkg_name]["files"].append(pkg_lines[i].split(" ")[1])
                        i += 1

                if i == len(pkg_lines):
                    break

                if pkg_lines[i].split(" ")[0] == "depend":
                    pkg_dict[pkg_name]["depends"].append(pkg_lines[i].split(" ")[1])

                i += 1

    return pkg_dict