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

plain-summer-72727

09/20/2022, 12:23 AM
Hi - I'm running a Django application in Pants and I'm trying to understand the best way to include non-py files when creating a PEX binary. I have the following in my BUILD file:
Copy code
python_sources(
    name="lib",
    dependencies=[
        "//:root#auth0-python",
        "//:root#django-admin-rangefilter",
        ...,
    ],
)

pex_binary(
    name="manage",
    entry_point="manage.py",
    dependencies=[
        ":lib",
    ],
    restartable=True,
)
My Django application has various HTML files, SQL files, and other non-Python files that are required for it to function. What's the typical pattern to make sure that these files are present in the PEX when running
./pants package src/myapp/manage.py
?
1
g

gentle-sugar-52379

09/20/2022, 12:43 AM
beware, new user too 🙂 in my templates folder i have a build file with
resources(name="templates", sources=["**/*.html"])
and in my module folder i have a build file with python_sources where i reference this dependency like so:
python_sources(dependencies=["./templates"])
p

plain-summer-72727

09/20/2022, 12:51 AM
That worked great - thank you!
g

gentle-sugar-52379

09/20/2022, 12:53 AM
there is an official example too on github
p

plain-summer-72727

09/20/2022, 12:57 AM
Out of curiosity, what if you want to support all non-Py files? This works great if I just have HTML files I want to copy over but I can see it getting repetitive w/ PDFs, SQL, JSON, and whatever other files an app may have.
I tried this with no luck:
Copy code
python_source(
    name="lib",
    dependencies=[
        ":non-py", 
        "...",
    ] 
)

resources(
    name="non-py",
    sources=["!**/*.py"],
)
g

gentle-sugar-52379

09/20/2022, 1:01 AM
thats doesnt look right
i would split the files like i explained
every dir 1 build file. i find it a lot easier
i would make a dir with non python files then
b

busy-vase-39202

09/20/2022, 1:04 AM
Aside: there is also a tutorial video from last year that might prove helpful?

https://www.youtube.com/watch?v=Glillzb_TqQ

g

gentle-sugar-52379

09/20/2022, 1:04 AM
why would i want to mix them?
p

plain-summer-72727

09/20/2022, 1:11 AM
Yeah I would, I don't have control over the app structure unfortunately - so any non-Py file could be located anywhere
e

enough-analyst-54434

09/20/2022, 2:33 AM
@plain-summer-72727 you were so close!:
Copy code
jsirois@Gill-Windows:~/dev/pantsbuild/example-django (main) $ cat <<EOF > BUILD.experiment
resources(
    name="non-py",
    sources=["**/*", "!**/*.py"]
)
EOF
jsirois@Gill-Windows:~/dev/pantsbuild/example-django (main) $ ./pants filedeps //:non-py
.flake8
.gitignore
.isort.cfg
BUILD
BUILD.experiment
LICENSE
README.md
build-support/generate_constraints.sh
helloworld/BUILD
helloworld/greet/BUILD
helloworld/greet/migrations/BUILD
helloworld/person/BUILD
helloworld/person/migrations/BUILD
helloworld/service/admin/BUILD
helloworld/service/frontend/BUILD
helloworld/service/user/BUILD
helloworld/service/welcome/BUILD
helloworld/translate/BUILD
helloworld/translate/migrations/BUILD
helloworld/ui/BUILD
helloworld/ui/static/BUILD
helloworld/ui/static/helloworld/ui/helloworld.css
helloworld/ui/templates/BUILD
helloworld/ui/templates/helloworld/ui/index.html
helloworld/util/BUILD
lockfiles/mypy.txt
lockfiles/pytest.txt
lockfiles/python-default.txt
mypy.ini
pants
pants.ci.toml
pants.toml
pants_from_sources
pytest.ini
requirements.txt
The key is
resources
has no default globs. By defualt it captures nothing. So you need to have
"***/**"
as the basis for "all" for the exclude to subtract from; otherwise you subtract from 0.
👍 1
p

plain-summer-72727

09/20/2022, 4:58 AM
Ah yes that worked perfectly - thank you!
❤️ 1
8 Views