11"""Utility functions to manipulate Bazel files"""
22import os
33import textwrap
4+ import json
45from typing import Iterable , List , Dict , Set
56
67from extract_wheels .lib import namespace_pkgs , wheel , purelib
78
89
9- def generate_build_file_contents (name : str , dependencies : List [str ]) -> str :
10+ def generate_build_file_contents (name : str , dependencies : List [str ], pip_data_exclude : List [ str ] ) -> str :
1011 """Generate a BUILD file for an unzipped Wheel
1112
1213 Args:
@@ -20,6 +21,8 @@ def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
2021 there may be no Python sources whatsoever (e.g. packages written in Cython: like `pymssql`).
2122 """
2223
24+ data_exclude = ["**/*.py" , "**/* *" , "BUILD" , "WORKSPACE" ] + pip_data_exclude
25+
2326 return textwrap .dedent (
2427 """\
2528 package(default_visibility = ["//visibility:public"])
@@ -29,14 +32,14 @@ def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
2932 py_library(
3033 name = "{name}",
3134 srcs = glob(["**/*.py"], allow_empty = True),
32- data = glob(["**/*"], exclude=["**/*.py", "**/* *", "BUILD", "WORKSPACE"] ),
35+ data = glob(["**/*"], exclude={data_exclude} ),
3336 # This makes this directory a top-level in the python import
3437 # search path for anything that depends on this.
3538 imports = ["."],
3639 deps = [{dependencies}],
3740 )
3841 """ .format (
39- name = name , dependencies = "," .join (dependencies )
42+ name = name , dependencies = "," .join (dependencies ), data_exclude = json . dumps ( data_exclude )
4043 )
4144 )
4245
@@ -116,7 +119,7 @@ def setup_namespace_pkg_compatibility(wheel_dir: str) -> None:
116119 namespace_pkgs .add_pkgutil_style_namespace_pkg_init (ns_pkg_dir )
117120
118121
119- def extract_wheel (wheel_file : str , extras : Dict [str , Set [str ]]) -> str :
122+ def extract_wheel (wheel_file : str , extras : Dict [str , Set [str ]], pip_data_exclude : List [ str ] ) -> str :
120123 """Extracts wheel into given directory and creates a py_library target.
121124
122125 Args:
@@ -145,7 +148,7 @@ def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]]) -> str:
145148
146149 with open (os .path .join (directory , "BUILD" ), "w" ) as build_file :
147150 contents = generate_build_file_contents (
148- sanitise_name (whl .name ), sanitised_dependencies ,
151+ sanitise_name (whl .name ), sanitised_dependencies , pip_data_exclude ,
149152 )
150153 build_file .write (contents )
151154
0 commit comments