Hi - I'm running a Django application in Pants and...
# general
p
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
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
That worked great - thank you!
g
there is an official example too on github
p
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
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
Aside: there is also a tutorial video from last year that might prove helpful?

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

g
why would i want to mix them?
p
Yeah I would, I don't have control over the app structure unfortunately - so any non-Py file could be located anywhere
e
@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
Ah yes that worked perfectly - thank you!
❤️ 1