Exemple #1
0
def extract_wheel(
    wheel_file: str,
    extras: Dict[str, Set[str]],
    pip_data_exclude: List[str],
    enable_implicit_namespace_pkgs: bool,
) -> str:
    """Extracts wheel into given directory and creates py_library and filegroup targets.

    Args:
        wheel_file: the filepath of the .whl
        extras: a list of extras to add as dependencies for the installed wheel
        pip_data_exclude: list of file patterns to exclude from the generated data section of the py_library
        enable_implicit_namespace_pkgs: if true, disables conversion of implicit namespace packages and will unzip as-is

    Returns:
        The Bazel label for the extracted wheel, in the form '//path/to/wheel'.
    """

    whl = wheel.Wheel(wheel_file)
    directory = sanitise_name(whl.name)

    os.mkdir(directory)
    # copy the original wheel
    shutil.copy(whl.path, directory)
    whl.unzip(directory)

    # Note: Order of operations matters here
    purelib.spread_purelib_into_root(directory)

    if not enable_implicit_namespace_pkgs:
        setup_namespace_pkg_compatibility(directory)

    extras_requested = extras[whl.name] if whl.name in extras else set()
    whl_deps = sorted(whl.dependencies(extras_requested))

    sanitised_dependencies = [
        '"//%s"' % sanitise_name(d) for d in whl_deps
    ]
    sanitised_wheel_file_dependencies = [
        '"//%s:%s"' % (sanitise_name(d), WHEEL_FILE_LABEL) for d in whl_deps
    ]

    with open(os.path.join(directory, "BUILD"), "w") as build_file:
        contents = generate_build_file_contents(
            sanitise_name(whl.name), sanitised_dependencies, sanitised_wheel_file_dependencies, pip_data_exclude
        )
        build_file.write(contents)

    os.remove(whl.path)

    return "//%s" % directory
Exemple #2
0
def extract_wheel(
    wheel_file: str,
    extras: Dict[str, Set[str]],
    pip_data_exclude: List[str],
    enable_implicit_namespace_pkgs: bool,
    incremental: bool = False,
    incremental_repo_prefix: Optional[str] = None,
) -> Optional[str]:
    """Extracts wheel into given directory and creates py_library and filegroup targets.

    Args:
        wheel_file: the filepath of the .whl
        extras: a list of extras to add as dependencies for the installed wheel
        pip_data_exclude: list of file patterns to exclude from the generated data section of the py_library
        enable_implicit_namespace_pkgs: if true, disables conversion of implicit namespace packages and will unzip as-is
        incremental: If true the extract the wheel in a format suitable for an external repository. This
            effects the names of libraries and their dependencies, which point to other external repositories.
        incremental_repo_prefix: If incremental is true, use this prefix when creating labels from wheel
            names instead of the default.

    Returns:
        The Bazel label for the extracted wheel, in the form '//path/to/wheel'.
    """

    whl = wheel.Wheel(wheel_file)
    if incremental:
        directory = "."
    else:
        directory = sanitise_name(whl.name)

        os.mkdir(directory)
        # copy the original wheel
        shutil.copy(whl.path, directory)
    whl.unzip(directory)

    # Note: Order of operations matters here
    purelib.spread_purelib_into_root(directory)

    if not enable_implicit_namespace_pkgs:
        setup_namespace_pkg_compatibility(directory)

    extras_requested = extras[whl.name] if whl.name in extras else set()
    whl_deps = sorted(whl.dependencies(extras_requested))

    if incremental:
        # check for mypy Optional validity
        if incremental_repo_prefix is None:
            raise TypeError(
                "incremental_repo_prefix arguement cannot be None if incremental == True")
        sanitised_dependencies = [
            sanitised_repo_library_label(d, repo_prefix=incremental_repo_prefix) for d in whl_deps
        ]
        sanitised_wheel_file_dependencies = [
            sanitised_repo_file_label(d, repo_prefix=incremental_repo_prefix) for d in whl_deps
        ]
    else:
        sanitised_dependencies = [
            sanitised_library_label(d) for d in whl_deps
        ]
        sanitised_wheel_file_dependencies = [
            sanitised_file_label(d) for d in whl_deps
        ]

    library_name = PY_LIBRARY_LABEL if incremental else sanitise_name(whl.name)

    directory_path = Path(directory)
    entry_points = []
    for name, entry_point in sorted(whl.entry_points().items()):
        entry_point_script = f"{WHEEL_ENTRY_POINT_PREFIX}_{name}.py"
        (directory_path / entry_point_script).write_text(generate_entry_point_contents(entry_point))
        entry_points.append(generate_entry_point_rule(
            entry_point_script,
            library_name,
        ))

    with open(os.path.join(directory, "BUILD.bazel"), "w") as build_file:
        contents = generate_build_file_contents(
            library_name,
            sanitised_dependencies,
            sanitised_wheel_file_dependencies,
            pip_data_exclude,
            ["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
            entry_points,
        )
        build_file.write(contents)

    if not incremental:
        os.remove(whl.path)
        return f"//{directory}"
    return None