Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

wheel: support for 'plugin' type entry_points #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 4 experimental/examples/wheel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ py_wheel(
description_file = "README.md",
# Package data. We're building "example_customized-0.0.1-py3-none-any.whl"
distribution = "example_customized",
entry_points = {
"console_scripts": ["another = foo.bar:baz"],
"group2": ["second = second.main:s", "first = first.main:f"]
},
homepage = "www.example.com",
license = "Apache 2.0",
python_tag = "py3",
Expand Down
11 changes: 10 additions & 1 deletion 11 experimental/examples/wheel/wheel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ def test_customized_wheel(self):
'example_customized-0.0.1.dist-info/WHEEL')
metadata_contents = zf.read(
'example_customized-0.0.1.dist-info/METADATA')
entry_point_contents = zf.read('example_customized-0.0.1.dist-info/entry_points.txt')
# The entries are guaranteed to be sorted.
self.assertEquals(record_contents, b"""\
example_customized-0.0.1.dist-info/METADATA,sha256=TeeEmokHE2NWjkaMcVJuSAq4_AXUoIad2-SLuquRmbg,372
example_customized-0.0.1.dist-info/RECORD,,
example_customized-0.0.1.dist-info/WHEEL,sha256=F01lGfVCzcXUzzQHzUkBmXAcu_TXd5zqMLrvrspncJo,85
example_customized-0.0.1.dist-info/entry_points.txt,sha256=olLJ8FK88aft2pcdj4BD05F8Xyz83Mo51I93tRGT2Yk,74
example_customized-0.0.1.dist-info/entry_points.txt,sha256=mEWsq4sMoyqR807QV8Z3KPocGfKvtgTo1lBFTRb6b78,150
experimental/examples/wheel/lib/data.txt,sha256=9vJKEdfLu8bZRArKLroPZJh1XKkK3qFMXiM79MBL2Sg,12
experimental/examples/wheel/lib/module_with_data.py,sha256=K_IGAq_CHcZX0HUyINpD1hqSKIEdCn58d9E9nhWF2EA,636
experimental/examples/wheel/lib/simple_module.py,sha256=72-91Dm6NB_jw-7wYQt7shzdwvk5RB0LujIah8g7kr8,636
Expand All @@ -101,6 +102,14 @@ def test_customized_wheel(self):

This is a sample description of a wheel.
""")
self.assertEquals(entry_point_contents, b"""\
[console_scripts]
another = foo.bar:baz
customized_wheel = experimental.examples.wheel.main:main

[group2]
first = first.main:f
second = second.main:s""")

def test_custom_package_root_wheel(self):
filename = os.path.join(os.environ['TEST_SRCDIR'],
Expand Down
45 changes: 37 additions & 8 deletions 45 experimental/python/wheel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def _py_wheel_impl(ctx):
other_inputs = []

# Wrap the inputs into a file to reduce command line length.
packageinputfile = ctx.actions.declare_file(ctx.attr.name + '_target_wrapped_inputs.txt')
content = ''
packageinputfile = ctx.actions.declare_file(ctx.attr.name + "_target_wrapped_inputs.txt")
content = ""
for input_file in inputs_to_package.to_list():
content += _input_file_to_arg(input_file) + '\n'
ctx.actions.write(output = packageinputfile, content=content)
content += _input_file_to_arg(input_file) + "\n"
ctx.actions.write(output = packageinputfile, content = content)
other_inputs.append(packageinputfile)

args = ctx.actions.args()
Expand Down Expand Up @@ -140,8 +140,30 @@ def _py_wheel_impl(ctx):
for r in requirements:
args.add("--extra_requires", r + ";" + option)

for name, ref in ctx.attr.console_scripts.items():
args.add("--console_script", name + " = " + ref)
# Merge console_scripts into entry_points.
entrypoints = dict(ctx.attr.entry_points) # Copy so we can mutate it
if ctx.attr.console_scripts:
# Copy a console_scripts group that may already exist, so we can mutate it.
console_scripts = list(entrypoints.get("console_scripts", []))
entrypoints["console_scripts"] = console_scripts
for name, ref in ctx.attr.console_scripts.items():
console_scripts.append("{name} = {ref}".format(name = name, ref = ref))

# If any entry_points are provided, construct the file here and add it to the files to be packaged.
# see: https://packaging.python.org/specifications/entry-points/
if entrypoints:
lines = []
for group, entries in sorted(entrypoints.items()):
if lines:
# Blank line between groups
lines.append("")
lines.append("[{group}]".format(group = group))
lines += sorted(entries)
entry_points_file = ctx.actions.declare_file(ctx.attr.name + "_entry_points.txt")
content = "\n".join(lines)
ctx.actions.write(output = entry_points_file, content = content)
other_inputs.append(entry_points_file)
args.add("--entry_points_file", entry_points_file)

if ctx.attr.description_file:
description_file = ctx.file.description_file
Expand Down Expand Up @@ -208,7 +230,14 @@ _requirement_attrs = {
_entrypoint_attrs = {
"console_scripts": attr.string_dict(
dhalperi marked this conversation as resolved.
Show resolved Hide resolved
doc = """\
console_script entry points, e.g. 'experimental.examples.wheel.main:main'.
Deprecated console_script entry points, e.g. {'main': 'experimental.examples.wheel.main:main'}.

Deprecated: prefer the `entry_points` attribute, which supports `console_scripts` as well as other entry points.
""",
),
"entry_points": attr.string_list_dict(
doc = """\
entry_points, e.g. {'console_scripts': ['main = experimental.examples.wheel.main:main']}.
""",
),
}
Expand Down Expand Up @@ -281,7 +310,7 @@ Targets to be included in the distribution.
The targets to package are usually `py_library` rules or filesets (for packaging data files).

Note it's usually better to package `py_library` targets and use
`console_scripts` attribute to specify entry points than to package
`entry_points` attribute to specify `console_scripts` than to package
`py_binary` rules. `py_binary` targets would wrap a executable script that
tries to locate `.runfiles` directory which is not packaged in the wheel.
""",
Expand Down
22 changes: 7 additions & 15 deletions 22 experimental/rules_python/wheelmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def add_string(self, filename, contents):

def add_file(self, package_filename, real_filename):
"""Add given file to the distribution."""

def arcname_from(name):
# Always use unix path separators.
normalized_arcname = name.replace(os.path.sep, '/')
Expand Down Expand Up @@ -157,15 +158,6 @@ def add_metadata(self, extra_headers, description, classifiers, requires,
metadata += "\n"
self.add_string(self.distinfo_path('METADATA'), metadata)

def add_entry_points(self, console_scripts):
"""Write entry_points.txt file to the distribution."""
# https://packaging.python.org/specifications/entry-points/
if not console_scripts:
return
lines = ["[console_scripts]"] + console_scripts
contents = '\n'.join(lines)
self.add_string(self.distinfo_path('entry_points.txt'), contents)

def add_recordfile(self):
"""Write RECORD file to the distribution."""
record_path = self.distinfo_path('RECORD')
Expand Down Expand Up @@ -235,6 +227,8 @@ def main():
"Can be supplied multiple times")
wheel_group.add_argument('--description_file',
help="Path to the file with package description")
wheel_group.add_argument('--entry_points_file',
help="Path to a correctly-formatted entry_points.txt file")

contents_group = parser.add_argument_group("Wheel contents")
contents_group.add_argument(
Expand All @@ -246,10 +240,6 @@ def main():
'--input_file_list', action='append',
help='A file that has all the input files defined as a list to avoid the long command'
)
contents_group.add_argument(
'--console_script', action='append',
help="Defines a 'console_script' entry point. "
"Can be supplied multiple times.")

requirements_group = parser.add_argument_group("Package requirements")
requirements_group.add_argument(
Expand Down Expand Up @@ -314,14 +304,16 @@ def main():
classifiers = arguments.classifier or []
requires = arguments.requires or []
extra_headers = arguments.header or []
console_scripts = arguments.console_script or []

maker.add_metadata(extra_headers=extra_headers,
description=description,
classifiers=classifiers,
requires=requires,
extra_requires=extra_requires)
maker.add_entry_points(console_scripts=console_scripts)

if arguments.entry_points_file:
maker.add_file(maker.distinfo_path("entry_points.txt"), arguments.entry_points_file)

maker.add_recordfile()


Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.