def _extract_root_entry()

in kotlin/internal/js/importer.py [0:0]


def _extract_root_entry(jar, filename_pattern, output_path, touch=False):
    """
    Extracts a root entry from a jar. and write it to a path.

    output_path is absolute and the basename is used to extract the entry from the jar.

    :param jar: The jar from which to make the extraction.
    :param filename_pattern: Regular expression to match when searching for the file in the jar.
    :param output_path: An absolute file path to where the entry should be written.
    :param touch: Should the file be touched if it was not found in the jar.
    """
    target = None
    for filename in jar.namelist():
        if filename_pattern.match(filename):
            target = filename
            break

    if not target:
        if touch:
            f = open(output_path, 'a')
            f.close()
            return
        else:
            raise FileNotFoundError("No file matching {0} was found in jar".format(filename_pattern))

    # Extract the target file to a temporary location.
    temp_dir = tempfile.gettempdir()
    temp_file = os.path.join(temp_dir, os.path.basename(target))
    jar.extract(target, path=temp_dir)

    # Move the temp file into the final output location.
    shutil.move(temp_file, output_path)