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 #38.
  • Loading branch information
MishaSeltzer committed Dec 19, 2020
1 parent 186bade commit 58d06b8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 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()

10 changes: 5 additions & 5 deletions thirdparty/pip/requirements-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"lxml": {
"dependencies": [],
"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 @@ -176,7 +176,7 @@
},
"lxml": {
"dependencies": [],
"is_direct": false,
"is_direct": true,
"source": "lxml_4_3_3_cp36_cp36m_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 @@ -300,7 +300,7 @@
},
"lxml": {
"dependencies": [],
"is_direct": false,
"is_direct": true,
"source": "lxml_4_3_3_cp27_cp27mu_manylinux1_x86_64",
"version": "4.3.3"
},
Expand Down Expand Up @@ -432,7 +432,7 @@
},
"lxml": {
"dependencies": [],
"is_direct": false,
"is_direct": true,
"source": "lxml_4_3_3_cp36_cp36m_manylinux1_x86_64",
"version": "4.3.3"
},
Expand Down Expand Up @@ -622,4 +622,4 @@
"url": "https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl"
}
}
}
}
4 changes: 3 additions & 1 deletion thirdparty/pip/requirements.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
isort
google-cloud-logging
isort[pyproject]
lxml
pathlib2 ; python_version < "3.0"
pyang
pytest
Expand Down

0 comments on commit 58d06b8

Please sign in to comment.