I have non-python files (templates to send emails)...
# general
m
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
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
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
I've tried that, it didn't work. Let me check again.
h
That page has more details on the difference between
files()
and
resources()
and when you would use each
m
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
Let me replicate that, see what I get..
h
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
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
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
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
I didn't see any empty directory with my snippet above
(extracting the content of pex)
w
odd.