@@ -27,26 +27,38 @@ def _construct_pypath(rctx):
2727def _parse_optional_attrs (rctx , args ):
2828 """Helper function to parse common attributes of pip_repository and whl_library repository rules.
2929
30+ This function also serializes the structured arguments as JSON
31+ so they can be passed on the command line to subprocesses.
32+
3033 Args:
3134 rctx: Handle to the rule repository context.
3235 args: A list of parsed args for the rule.
3336 Returns: Augmented args list.
3437 """
35- if rctx .attr .extra_pip_args :
38+
39+ # Check for None so we use empty default types from our attrs.
40+ # Some args want to be list, and some want to be dict.
41+ if rctx .attr .extra_pip_args != None :
3642 args += [
3743 "--extra_pip_args" ,
38- struct (args = rctx .attr .extra_pip_args ).to_json (),
44+ struct (arg = rctx .attr .extra_pip_args ).to_json (),
3945 ]
4046
41- if rctx .attr .pip_data_exclude :
47+ if rctx .attr .pip_data_exclude != None :
4248 args += [
4349 "--pip_data_exclude" ,
44- struct (exclude = rctx .attr .pip_data_exclude ).to_json (),
50+ struct (arg = rctx .attr .pip_data_exclude ).to_json (),
4551 ]
4652
4753 if rctx .attr .enable_implicit_namespace_pkgs :
4854 args .append ("--enable_implicit_namespace_pkgs" )
4955
56+ if rctx .attr .environment != None :
57+ args += [
58+ "--environment" ,
59+ struct (arg = rctx .attr .environment ).to_json (),
60+ ]
61+
5062 return args
5163
5264_BUILD_FILE_CONTENTS = """\
@@ -102,10 +114,8 @@ def _pip_repository_impl(rctx):
102114
103115 result = rctx .execute (
104116 args ,
105- environment = {
106- # Manually construct the PYTHONPATH since we cannot use the toolchain here
107- "PYTHONPATH" : pypath ,
108- },
117+ # Manually construct the PYTHONPATH since we cannot use the toolchain here
118+ environment = {"PYTHONPATH" : _construct_pypath (rctx )},
109119 timeout = rctx .attr .timeout ,
110120 quiet = rctx .attr .quiet ,
111121 )
@@ -126,6 +136,16 @@ and py_test targets must specify either `legacy_create_init=False` or the global
126136This option is required to support some packages which cannot handle the conversion to pkg-util style.
127137 """ ,
128138 ),
139+ "environment" : attr .string_dict (
140+ doc = """
141+ Environment variables to set in the pip subprocess.
142+ Can be used to set common variables such as `http_proxy`, `https_proxy` and `no_proxy`
143+ Note that pip is run with "--isolated" on the CLI so PIP_<VAR>_<NAME>
144+ style env vars are ignored, but env vars that control requests and urllib3
145+ can be passed.
146+ """ ,
147+ default = {},
148+ ),
129149 "extra_pip_args" : attr .string_list (
130150 doc = "Extra arguments to pass on to pip. Must not contain spaces." ,
131151 ),
@@ -221,7 +241,6 @@ py_binary(
221241def _impl_whl_library (rctx ):
222242 # pointer to parent repo so these rules rerun if the definitions in requirements.bzl change.
223243 _parent_repo_label = Label ("@{parent}//:requirements.bzl" .format (parent = rctx .attr .repo ))
224- pypath = _construct_pypath (rctx )
225244 args = [
226245 rctx .attr .python_interpreter ,
227246 "-m" ,
@@ -232,12 +251,11 @@ def _impl_whl_library(rctx):
232251 rctx .attr .repo ,
233252 ]
234253 args = _parse_optional_attrs (rctx , args )
254+
235255 result = rctx .execute (
236256 args ,
237- environment = {
238- # Manually construct the PYTHONPATH since we cannot use the toolchain here
239- "PYTHONPATH" : pypath ,
240- },
257+ # Manually construct the PYTHONPATH since we cannot use the toolchain here
258+ environment = {"PYTHONPATH" : _construct_pypath (rctx )},
241259 quiet = rctx .attr .quiet ,
242260 timeout = rctx .attr .timeout ,
243261 )
0 commit comments