Was stumped today by the adhoc_tool that I am crea...
# general
b
Was stumped today by the adhoc_tool that I am creating. I want to use a python script that takes an xml file and a few template files as input, and generates some language bindings as an output. Focusing first on python-in and python-out, here's what I have so far. Repo layout:
Copy code
├── interface
│   ├── BUILD
│   ├── interface.dtd
│   ├── interface.xml
│   └── xmlconvert.py
└── pants.toml
And the BUILD file:
Copy code
python_source(
    name="xml_convert_py",
    source="xmlconvert.py",
)

files(name="interface_deps", sources=["interface.xml"])

adhoc_tool(
    name="xmlconvert",
    runnable=":xml_convert_py",
    execution_dependencies=["interface.xml:interface_deps"],
    output_files=[<tbd, need to fix the FileNotFoundError first>]
)
Running
pants run interface:xml_convert_py
, I get a FileNotFoundError. The pants sandbox does not receive the interface.xml file. How can I solve this?
I can tell that pants knows about the interface.xml file: If I intentionally add a typo (e.g. I name it not-interface.xml, I get a warning about an "unmatched glob" in the CLI.
f
You may need to use
resources
instead of
files
for the
interface.xml
file.
And have the dependency be from the
python_source
target to the
resources
target.
Then you should be able to use one of the resource importing packages (e.g,
importlib.resources
) to access it.
The other way would be to debug why the input file as a
files
is not showing up in the sandbox.
For that, you should use
--keep-sandboxes=on_failure
to preserve the sandbox and see what files actually made it in there.
b
> You may need to use
resources
instead of
files
for the
interface.xml
file. Resources looks good. The idea there is to turn this script into a module, perhaps running it with an entry point, and accessing that xml file as a package resource? I didn't think about this. Coming back to Pants after a few months' hiatus...
preserve the sandbox and see what files
I've looked at this, the only file that made it in there is the
xmlconvert.py
file.
Copy code
~> tree
.
├── interface
│   ├── __pycache__
│   │   └── xmlconvert.cpython-310.pyc
│   └── xmlconvert.py
├── __run.sh
├── xml_convert_py.pex
│   ├── __main__.py
│   ├── __pex__
│   │   └── __init__.py
│   └── PEX-INFO
├── xml_convert_py.pex_bin_python_shim.sh
└── xml_convert_py.pex_pex_shim.sh