import re


def load_log_file(file) -> dict:
    """Loads Targets and their durations from ninja logfile `file` and returns them in a dict"""

    with open(file, 'r') as file_pointer:
        lines = file_pointer.read().splitlines()

    # {Target: duration} dict
    target_duration_dict = {}
    for line in lines:
        line = line.strip()

        # pattern to match target names that are an absolute path - these should be skipped
        # --> an issue appeared with new versions of cmake where the targets are duplicated for some
        # reason and therefore they must be filtered here
        duplicate_pattern = re.compile("[0-9]+\s+[0-9]+\s+[0-9]+\s+/.*")
        if not line.startswith('#') and not duplicate_pattern.match(line):
            # calculate target compilation duration and add it to dict
            line = line.split()
            duration = int(line[1]) - int(line[0])
            target_duration_dict[line[3]] = duration

    return target_duration_dict