How would I go about running a `python_source` and...
# general
e
How would I go about running a
python_source
and collecting its output? I have a python script that runs my app and writes the generated api spec (to stdout, but could easily switch it to a file). I'm trying to figure out how to run this and then use the output file for something (take a diff against the PR target, so as to check for changes to the spec). It seems I need to use something with
run_shell_command
, but I can't seem to figure it out (aside from exporting the venv, then having the shell command use
./dist/export/<.......>/bin/python
to run the script, which I would like to avoid because it feels like their ought to be a cleaner way)
or alternatively, can I
pants run 3rdparty/python:poetry#python
somehow? despite having
python
in my
pyproject.toml
, this command gives
Copy code
pants.build_graph.address.ResolveError: The address `3rdparty/python:poetry#python` from CLI arguments was not generated by the target `3rdparty/python:poetry`. Did you mean one of these addresses?
so it seems that pants actually strips
python
out (which I suppose I can see a case for)
f
You would likely need to use
adhoc_tool
.
b
Fleshing out that suggestion: something along the lines of
adhoc_tool(name="generated", runnable="./script.py", stdout="file_name_to_capture_to.txt")
is a likely starting point. Depending on
path/to/directory:generated
will then act like depending on a
file(name="generated", source="file_name_to_capture_to.txt")
if there was a file by that name on disk (i.e. it'll appear in sandboxes). https://github.com/pantsbuild/pants/discussions/18235#discussioncomment-6655594 might also be relevant to you.
e
Thanks for the great suggestions! I ended up just using
pants run /path/to/script.py | tail -n 1 > api_spec.json
since I wanted it to appear directly in my repo (eg. to save as a github artifact afterwards)