Skip to content

Commit

Permalink
Only set public visibility to direct dependencies
Browse files Browse the repository at this point in the history
This resolves issue apt-itude#38.
  • Loading branch information
MishaSeltzer committed Dec 18, 2020
1 parent f8646f4 commit c08239f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/bin/generate_pip_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,26 @@ def _generate_package_for_requirement(
write_file(repos_file_path, repos_file_content)

def _generate_build_file_content(self, build_rules):
return textwrap.dedent("""
package(default_visibility = ["//visibility:public"])
{rules}
""").strip().format(
rules="\n".join(build_rules),
)
return textwrap.dedent("\n".join(build_rules))

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

# We want to make the top alias "visible" only if the package is specified
# in the requirements.txt file (i.e. has `is_direct=True` field in the lock
# file). Since there's an `is_direct` key for each python_version/platform,
# we're going to set the visibility to public if any of those is True.
visibility = "//:__subpackages__"

for python_version, platform_subtree in python_subtree.items():
for platform, requirement_details in platform_subtree.items():
yield self._generate_py_library(
requirement_details,
python_version,
platform,
)
if requirement_details["is_direct"]:
visibility = "//visibility:public"

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

top_alias.visibility = visibility
yield str(top_alias)

def _generate_py_library(self, requirement_details, python_version, platform):
Expand Down Expand Up @@ -331,16 +334,19 @@ class SelectAlias(object):
def __init__(self, name):
self.name = name
self.actual = {}
self.visibility = "//visibility:private"

def __str__(self):
return textwrap.dedent("""
alias(
name = "{name}",
actual = select({actual}),
visibility = ["{visibility}"],
)
""").strip().format(
name=self.name,
actual=self.actual,
visibility=self.visibility,
)


Expand Down
26 changes: 26 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ py_test(
tags = ["new"],
)

py_test(
name = "visibility_test",
srcs = ["visibility_test.py"],
data = [
":visibility_test_pytest_six",
":visibility_test_lib_six",
],
)

genquery(
name = "visibility_test_pytest_six",
expression = "visible('@pip//pytest', '@pip//six')",
scope = ["@pip//pytest"],
)

py_library(
name = "visibility_test_library",
deps = ["@pip//pytest"],
)

genquery(
name = "visibility_test_lib_six",
expression = "visible('//test:visibility_test_library', '@pip//six')",
scope = [":visibility_test_library"],
)

genrule(
name = "pyang-main",
srcs = [pyang_repo + "//scripts:pyang"],
Expand Down
6 changes: 6 additions & 0 deletions test/visibility_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with open("./test/visibility_test_pytest_six") as f:
assert f.read().strip() == "@pip//six:six"

with open("./test/visibility_test_lib_six") as f:
assert not f.read().strip()

8 changes: 4 additions & 4 deletions thirdparty/pip/requirements-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"lxml": {
"dependencies": [],
"extras": [],
"is_direct": false,
"is_direct": true,
"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",
"version": "4.3.3"
},
Expand Down Expand Up @@ -477,7 +477,7 @@
"lxml": {
"dependencies": [],
"extras": [],
"is_direct": false,
"is_direct": true,
"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",
"version": "4.3.3"
},
Expand Down Expand Up @@ -795,7 +795,7 @@
"lxml": {
"dependencies": [],
"extras": [],
"is_direct": false,
"is_direct": true,
"source": "lxml_4_3_3_cp27_cp27mu_manylinux1_x86_64",
"version": "4.3.3"
},
Expand Down Expand Up @@ -1112,7 +1112,7 @@
"lxml": {
"dependencies": [],
"extras": [],
"is_direct": false,
"is_direct": true,
"source": "lxml_4_3_3_cp36_cp36m_manylinux1_x86_64",
"version": "4.3.3"
},
Expand Down
1 change: 1 addition & 0 deletions thirdparty/pip/requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
google-cloud-logging
isort[pyproject]
lxml
pathlib2 ; python_version < "3.0"
pyang
pytest
Expand Down

0 comments on commit c08239f

Please sign in to comment.