Skip to content

Commit c08239f

Browse files
committed
Only set public visibility to direct dependencies
This resolves issue #38.
1 parent f8646f4 commit c08239f

5 files changed

Lines changed: 50 additions & 11 deletions

File tree

src/bin/generate_pip_repositories.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,26 @@ def _generate_package_for_requirement(
179179
write_file(repos_file_path, repos_file_content)
180180

181181
def _generate_build_file_content(self, build_rules):
182-
return textwrap.dedent("""
183-
package(default_visibility = ["//visibility:public"])
184-
185-
{rules}
186-
""").strip().format(
187-
rules="\n".join(build_rules),
188-
)
182+
return textwrap.dedent("\n".join(build_rules))
189183

190184
def _generate_rules_for_requirement(self, requirement_name, python_subtree):
191185
top_alias = SelectAlias(requirement_name)
192186

187+
# We want to make the top alias "visible" only if the package is specified
188+
# in the requirements.txt file (i.e. has `is_direct=True` field in the lock
189+
# file). Since there's an `is_direct` key for each python_version/platform,
190+
# we're going to set the visibility to public if any of those is True.
191+
visibility = "//:__subpackages__"
192+
193193
for python_version, platform_subtree in python_subtree.items():
194194
for platform, requirement_details in platform_subtree.items():
195195
yield self._generate_py_library(
196196
requirement_details,
197197
python_version,
198198
platform,
199199
)
200+
if requirement_details["is_direct"]:
201+
visibility = "//visibility:public"
200202

201203
version_alias = self._generate_python_version_alias(
202204
python_version,
@@ -208,6 +210,7 @@ def _generate_rules_for_requirement(self, requirement_name, python_subtree):
208210
python_version_label = _make_python_version_label(python_version)
209211
top_alias.actual[python_version_label] = version_alias.name
210212

213+
top_alias.visibility = visibility
211214
yield str(top_alias)
212215

213216
def _generate_py_library(self, requirement_details, python_version, platform):
@@ -331,16 +334,19 @@ class SelectAlias(object):
331334
def __init__(self, name):
332335
self.name = name
333336
self.actual = {}
337+
self.visibility = "//visibility:private"
334338

335339
def __str__(self):
336340
return textwrap.dedent("""
337341
alias(
338342
name = "{name}",
339343
actual = select({actual}),
344+
visibility = ["{visibility}"],
340345
)
341346
""").strip().format(
342347
name=self.name,
343348
actual=self.actual,
349+
visibility=self.visibility,
344350
)
345351

346352

test/BUILD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ py_test(
5959
tags = ["new"],
6060
)
6161

62+
py_test(
63+
name = "visibility_test",
64+
srcs = ["visibility_test.py"],
65+
data = [
66+
":visibility_test_pytest_six",
67+
":visibility_test_lib_six",
68+
],
69+
)
70+
71+
genquery(
72+
name = "visibility_test_pytest_six",
73+
expression = "visible('@pip//pytest', '@pip//six')",
74+
scope = ["@pip//pytest"],
75+
)
76+
77+
py_library(
78+
name = "visibility_test_library",
79+
deps = ["@pip//pytest"],
80+
)
81+
82+
genquery(
83+
name = "visibility_test_lib_six",
84+
expression = "visible('//test:visibility_test_library', '@pip//six')",
85+
scope = [":visibility_test_library"],
86+
)
87+
6288
genrule(
6389
name = "pyang-main",
6490
srcs = [pyang_repo + "//scripts:pyang"],

test/visibility_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with open("./test/visibility_test_pytest_six") as f:
2+
assert f.read().strip() == "@pip//six:six"
3+
4+
with open("./test/visibility_test_lib_six") as f:
5+
assert not f.read().strip()
6+

thirdparty/pip/requirements-lock.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"lxml": {
161161
"dependencies": [],
162162
"extras": [],
163-
"is_direct": false,
163+
"is_direct": true,
164164
"source": "lxml_4_3_3_cp27_cp27m_macosx_10_6_intel_macosx_10_9_intel_macosx_10_9_x86_64_macosx_10_10_intel_macosx_10_10_x86_64",
165165
"version": "4.3.3"
166166
},
@@ -477,7 +477,7 @@
477477
"lxml": {
478478
"dependencies": [],
479479
"extras": [],
480-
"is_direct": false,
480+
"is_direct": true,
481481
"source": "lxml_4_3_3_cp37_cp37m_macosx_10_6_intel_macosx_10_9_intel_macosx_10_9_x86_64_macosx_10_10_intel_macosx_10_10_x86_64",
482482
"version": "4.3.3"
483483
},
@@ -795,7 +795,7 @@
795795
"lxml": {
796796
"dependencies": [],
797797
"extras": [],
798-
"is_direct": false,
798+
"is_direct": true,
799799
"source": "lxml_4_3_3_cp27_cp27mu_manylinux1_x86_64",
800800
"version": "4.3.3"
801801
},
@@ -1112,7 +1112,7 @@
11121112
"lxml": {
11131113
"dependencies": [],
11141114
"extras": [],
1115-
"is_direct": false,
1115+
"is_direct": true,
11161116
"source": "lxml_4_3_3_cp36_cp36m_manylinux1_x86_64",
11171117
"version": "4.3.3"
11181118
},

thirdparty/pip/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
google-cloud-logging
22
isort[pyproject]
3+
lxml
34
pathlib2 ; python_version < "3.0"
45
pyang
56
pytest

0 commit comments

Comments
 (0)