lemon-oxygen-2929
02/04/2024, 11:29 PMsilly-queen-7197
02/05/2024, 12:49 AMfresh-cat-90827
02/05/2024, 10:43 AM# cheeseshop/BUILD
python_sources()
file(
name="project-version",
source="VERSION",
)
archive(
name="my-archive",
files=[":project-version"],
format="zip",
output_path="uploads/s3.zip"
)
now
$ rm -rf dist/; pants package cheeseshop:my-archive
10:34:04.36 [INFO] Wrote dist/uploads/s3.zip
$ tree dist
dist
└── uploads
└── s3.zip
2 directories, 1 file
now
# cheeseshop/upload_archive.py
from zipfile import ZipFile
print(ZipFile("dist/uploads/s3.zip").namelist())
assert ZipFile("dist/uploads/s3.zip").namelist() == ["cheeseshop/VERSION"]
do
$ pants run cheeseshop/upload_archive.py
['cheeseshop/VERSION']
so with this your workflow would be a shell script:
$ pants package cheeseshop:my-archive
$ pants run cheeseshop/upload_archive.py
what you want to do IIUC is to be able to just run pants run cheeseshop/upload_archive.py and whatever runtime dependencies it has (explicitly declared manually) should be satisfied: in this case, it's an archive target my-archive.
I don't think you can do this with python_sources. This is something that is supported for python_tests however, see https://www.pantsbuild.org/2.19/reference/targets/python_tests#runtime_package_dependencies. Perhaps it's not that dumb to have this field for python_sources, too; e.g. you may indeed require having certain artifacts produced before running a script (uploading archives is a good use case, same for pushing system packages to a repository etc).
Before we have support for this (if at all), a somewhat cleaner way to document that your sources depend on some packages (archives in your case) is to add those dependencies manually with `overrides`:
python_sources(
overrides={"upload_archive.py": {"dependencies": [":my-archive-1", ":my-archive-2"]}},
)
archive(
name="my-archive-1",
files=[":project-version"],
format="zip",
output_path="uploads/s3-1.zip"
)
archive(
name="my-archive-2",
files=[":project-version"],
format="zip",
output_path="uploads/s3-2.zip"
so now first package only those archives that are needed:
$ pants dependencies cheeseshop/upload_archive.py | xargs pants --filter-target-type=archive package
10:42:10.57 [INFO] Wrote dist/uploads/s3-1.zip
10:42:10.57 [INFO] Wrote dist/uploads/s3-2.zip
and then
$ pants run cheeseshop/upload_archive.py
having all those prerequisites packagedlemon-oxygen-2929
02/05/2024, 12:45 PMlemon-oxygen-2929
02/05/2024, 12:51 PMprint(ZipFile("dist/uploads/s3.zip").namelist())
depends on running the command from the pants buildroot.
One reason I would like to do this using dependencies is to take advantage of the sandbox environment, so that I don't need a way for the script to find the archive at runtime regardless of the context in which it's run.lemon-oxygen-2929
02/05/2024, 12:54 PMlemon-oxygen-2929
02/05/2024, 1:18 PMruntime_package_dependencies only exists for test targets? I think that does exactly what I want to do, but on a "runnable" target.
Or is it just "nobody has wanted this badly enough to implement it yet"?fresh-cat-90827
02/05/2024, 3:19 PMadhoc_tool yet, though, so can't really help much with this.
> Is there a reason runtime_package_dependencies only exists for test targets?
I was able to find one thread that seems to be related to the discussion we have, but it's fairly old, so I am not sure how relevant it is.
> Or is it just "nobody has wanted this badly enough to implement it yet"?
I think there is a concern that certain types of packages will start depending on other types of packages where it doesn't make sense which may lead to confusion or obscure bugs (e.g. a pex binary depending on another pex binary, but also include the sources of that binary as dependencies, that kind of thing).
If you're interested, perhaps you could file an issue to advocate having this field (or in some other way be able to have packages at runtime) for the python_source target? Then other maintainers could chip in?lemon-oxygen-2929
02/06/2024, 1:18 AMI am not sure I follow you here, I am afraid. I haven't usedNo worries at all, thank you for your engagement on this!yet, though, so can't really help much with this.adhoc_tool
If you're interested, perhaps you could file an issue to advocate having this field (or in some other way be able to have packages at runtime) for theYes, thank you! I've been wanting to exhaust other options first, but yeah, I think that's probably the right path forward.target?python_source
limited-art-78990
08/28/2024, 7:10 AMlemon-oxygen-2929
10/15/2024, 9:07 PM