Hope everybody is having a good monday so far! Sm...
# general
c
Hope everybody is having a good monday so far! Small question, what would be the best approach to include multiple folder with static files like, I have see the
resources
and the
files
targets and wondering how I can define correctly this dependencies. I have already done it for a single file with
resource()
and add it to the
python_sources
dependencies but for folder that I want to include everything. Thanks in advance 🙂
w
Could you use globs?
r
The way I have done is using glob e.g. for all CSV files
Copy code
resources(name="res", sources=["**/*.csv"])
c
Copy code
python_sources(
    dependencies=[":static-files"],
)

resources(
    name="static-files",
    sources=["html/*", "static/*", "templates/*"],
)
Do you think something like this would work if I really want everything inside some folders ?
r
yeah but I think you will also have to add dunder init there and hence you will need BUILD inside those directories also with
python_sources
so that later you can read it inside python.
c
I would prefer to avoid having useless
__init__.py
everywhere to mark those folder as package since they are not but I will try to do this from the BUILD file one level over and see how it goes. Thanks for the help @refined-addition-53644 🙂
r
This is bit python thing rather than pants though. If you want read these files using say
pkg_resources
from setuptools or
resources
from importlib you need to make them a package. https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package
e
You do not need to read the files as resources though. You can use
__file__
to derive a real file system path and then use file system APIs if you wish.
A long time ago PEX forced resource access, but no longer. PEXes are always unzipped.
So, the only confusing thing here is having to use a
resources
target to get files into a PEX for access as files instead of using a
files
target, which would make more word sense. That is just a historical oddity.
c
We are currently using
<http://MANIFEST.in|MANIFEST.in>
file at the root of each project, but we are migrating away from this with Poetry
What are the main difference between
files
and
resources
?
e
The resources trim the leading source root. But you expect that anyway I'd guess. So ~ none, except that
files
doesn't work with
pex_binary
for purely historical reasons; so, for Python, just always use
resources
And know that that does not mean you need to then access those
resources
via resource APIs.
Josh has long term work to get rid of the distinction and collapse both targets to a single
asset
target.
✅ 1
c
Now I understand better the whole thing, I will try but anyway I don't need to packages them since we are containerizing everything already and the packaging is happening within the docker build. The bundling will always include correctly those file since we already handling this. That was more for unit tests