https://pantsbuild.org/ logo
#general
Title
# general
m

modern-wolf-36228

09/08/2021, 1:56 PM
I have non-python files (templates to send emails). Currently they are on the MANIFEST.in and I generate the wheel with them. Now I'm building our monorepo, so... Can I include those files into the pex binary? Having them outside is not an issue, fwiw.
c

curved-television-6568

09/08/2021, 2:00 PM
I’ve not used this myself, but I think what you may want is to use
resource
, see https://www.pantsbuild.org/v2.7/docs/resources
h

happy-kitchen-89482

09/08/2021, 2:00 PM
Yes, see https://www.pantsbuild.org/docs/resources#resources for how to use
resources()
targets to have resources embedded in the pex file. This is useful if you're loading the files using
pkgutil
or similar.
😁 1
m

modern-wolf-36228

09/08/2021, 2:02 PM
I've tried that, it didn't work. Let me check again.
h

happy-kitchen-89482

09/08/2021, 2:02 PM
That page has more details on the difference between
files()
and
resources()
and when you would use each
m

modern-wolf-36228

09/08/2021, 2:14 PM
ah, perfect. Thanks 🙂
This is `potato/BUILD`:
Copy code
python_library(dependencies=["./resources"])

pex_binary(
    name="binary",
    entry_point="__main__.py",
    dependencies=["//:asyncpg", "//:python-multipart"],
)
Then I have `potato/resources/BUILD`:
Copy code
resources(sources=["./emails"])
Then I have `potato/resources/emails/BUILD`:
Copy code
resources(sources=["*.html", "./logo.png"])
But those resources are not included in the potato pex 😢
c

curved-television-6568

09/08/2021, 2:44 PM
Let me replicate that, see what I get..
h

hundreds-father-404

09/08/2021, 2:48 PM
Do you mean for the sources to be emails/* with a glob?
Also the dependency should be potato/resources or potato/resources:resources, not ./resources. I totally see why you're doing that - this is a bit confusing that the latter means "include a specific file" because it's a file path, whereas you want to include all the files from the resources target
c

curved-television-6568

09/08/2021, 2:54 PM
Indeed, I noticed that too, but doesn’t know the details in my head like Eric 😉 so, here it is, if you run
$ ./pants dependencies --transitive :binary
(if had in the root, but otherwise, add the path to potato as necessary..) To inspect your dependencies (what will end up in you pex file) This will inform you that there’s something amiss…
Copy code
Exception message: 1 Exception encountered:

  InvalidSpecPath: Address spec has un-normalized path part './resources'
Some minor tweaks, yields
Copy code
$ ./pants dependencies --transitive :binary
//__main__.py:main
resources
resources/emails/demo.html
resources/emails/logo.png
potato/BUILD
Copy code
python_library(name="main", dependencies=["resources"])

pex_binary(
    name="binary",
    entry_point="__main__.py",
    # dependencies=["//:asyncpg", "//:python-multipart"],
)
potato/resources/BUILD
Copy code
resources(sources=[], dependencies=["./emails"])
Notice, that potato is my root where I have
pants
.. so adjust paths/targets accordingly if you have them deeper down.
Most notable is, that I changed your
./emails
from sources to dependencies, as you seem to nest resource targets, it is a dependency, not a source.
However, I’m not entirely sure about the practice of nesting resources.. perhaps that’s entirely fine? (I have no real practical experience with resources…)
m

modern-wolf-36228

09/08/2021, 3:19 PM
it worked
Thanks!
The knowledge missing was the right "dependencies path", and the sources being empty on the
resources/BUILD
(which make sense 🙂)
thanks @curved-television-6568 and @hundreds-father-404
❤️ 1
w

witty-crayon-22786

09/08/2021, 4:01 PM
that’s an interesting edge case… we warn if globs aren’t matched. but that glob was matched… just not recursively.
👍 1
the effect was an empty directory being included.
m

modern-wolf-36228

09/08/2021, 4:02 PM
I didn't see any empty directory with my snippet above
(extracting the content of pex)
w

witty-crayon-22786

09/08/2021, 4:02 PM
odd.
6 Views