I have a python script I'm calling with `pants run...
# general
g
I have a python script I'm calling with
pants run
and the script outputs a file. That file is being dropped in the sandbox instead of a directory in the monorepo. Is there a known pattern for calling scripts with
pants run
that write files to the filesystem in a standard/normal place (not the sandbox)? In my case we're generating api client using openapi.
f
Would the
run_shell_command
target type (https://www.pantsbuild.org/stable/reference/targets/run_shell_command) be useful? Your Python script's target would be an
execution_dependencies
for the
run_shell_command
and then you could just copy the captured output into the appropriate part of the repository.
e.g.
cp {chroot}/some/output.txt dest/path/output.txt
Then you would
pants run
the
run_shell_command
target instead of the original Python target.
Somewhat related, the following example runs a file instead of copying it with
run_shell_command
but the use of
{chroot}
is relevant:https://github.com/pantsbuild/example-adhoc/blob/main/javascript/BUILD
g
ah,
{chroot}
is a good thing to know. I think with this knowledge I should be able to get something working.
b
The working directory/cwd/pwd of the process should be where you run
pants run
, while the Python files used for executing will be in the chroot. Are you using
__file__
or similar to locate the output file relative to a source file? If so, changing that might be a good approach
g
Yes, I was using
__file__
to create the file relative to the script.
b
Ah okay, have you thus solved the issue, or do you need more help?
(ah, re-reading the original usecase of doing codegen, maybe https://github.com/pantsbuild/pants/discussions/18235#discussioncomment-6655594 will be of interest to you.)
g
Thanks. I have tackled it yet because I authored a workaround script that exports the virtual env and then run the script.
b
Sorry for hijacking this thread and thanks @broad-processor-92400 for the very helpful links and info. I am just working on an almost identical problem. I could solve it by following the example of your last link and replacing the
shell_command
with a
python_source
wrapped in an
adhoc_tool
(because
python_source
does not support output_files directly). Now I am wondering whether the
{chroot}
substitution is documented somewhere (would think about a PR otherwise) and whether there is a similar way to inject the build root path into the
cp
command. I know that the working dir will be the directory where the
BUILD
file is located and I could work relative to that. However I think injecting build root would make it a bit easier to comprehend the file reference.
g
For what it's worth I solved my problem by adding an input for the output path. So I just pass in the output path to the script via cli args, i.e.
pants run //my/script.py -- --output my/path.txt