Skip to content

Commit

Permalink
Demonstrate resources in dists and pexes. (pantsbuild#13315)
Browse files Browse the repository at this point in the history
[ci skip-rust]

[ci skip-build-wheels]
  • Loading branch information
benjyw authored Oct 23, 2021
1 parent c40c5d1 commit 6b58987
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/python/pants/backend/python/goals/setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ async def get_sources(
# files() targets aren't owned by a single exported target - they aren't code, so
# we allow them to be in multiple dists. This is helpful for, e.g., embedding
# a standard license file in a dist.
# TODO: This doesn't actually work, the generated setup.py has no way of referencing
# these, since they aren't in a package, so they won't get included in the built dists.
# There is a separate `license_files()` setup.py kwarg that we should use for this
# special case (see https://setuptools.pypa.io/en/latest/references/keywords.html).
file_targets = targets_with_sources_types(
[FileSourceField], transitive_targets.closure, union_membership
)
Expand Down
12 changes: 12 additions & 0 deletions testprojects/src/python/hello/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()

resources(name="resource", sources=["dist_resource.txt"])

python_distribution(
name="hello-dist",
dependencies=["testprojects/src/python/hello/greet", ":resource"],
provides=setup_py(
name="hello-dist",
version="0.0.1",
zip_safe=True,
),
)
1 change: 1 addition & 0 deletions testprojects/src/python/hello/dist_resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A resource to embed in the dist.
7 changes: 6 additions & 1 deletion testprojects/src/python/hello/greet/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
resources(
name="greeting",
sources=["greeting.txt"],
)

python_sources(dependencies=[":greeting"])
5 changes: 4 additions & 1 deletion testprojects/src/python/hello/greet/greet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import pkgutil

from colors import green


def greet(greetee):
"""Given the name, return a greeting for a person of that name."""
return green("Hello, {}!".format(greetee))
greeting = pkgutil.get_data(__name__, "greeting.txt").decode().strip()
return green("{}, {}!".format(greeting, greetee))
1 change: 1 addition & 0 deletions testprojects/src/python/hello/greet/greeting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
5 changes: 5 additions & 0 deletions tests/python/pants_test/integration/list_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ def test_list_testproject() -> None:
assert pants_run.stdout.strip() == "\n".join(
[
"testprojects/src/python/hello:hello",
"testprojects/src/python/hello:hello-dist",
"testprojects/src/python/hello:resource",
"testprojects/src/python/hello/__init__.py",
"testprojects/src/python/hello/dist_resource.txt:resource",
"testprojects/src/python/hello/greet:greet",
"testprojects/src/python/hello/greet:greeting",
"testprojects/src/python/hello/greet/__init__.py",
"testprojects/src/python/hello/greet/greet.py",
"testprojects/src/python/hello/greet/greeting.txt:greeting",
"testprojects/src/python/hello/main:main",
"testprojects/src/python/hello/main:lib",
"testprojects/src/python/hello/main/__init__.py:lib",
Expand Down

0 comments on commit 6b58987

Please sign in to comment.