acoustic-library-86413
08/27/2024, 10:11 AMacoustic-library-86413
08/27/2024, 10:13 AMpackage-name/
BUILD
package_name/
__init__.py
0000_initial.py
0001.py
0002.py
run.py
decorators.py
providers.py
Some pseudocode for run.py
# run.py
# import some files here dynamically, e.g. `0000_initial.py` is never referenced
for file in files:
file.run_static_func()
My BUILD file currently has:
resources(name="scripts", sources=["./**/*"])
python_sources()
elegant-florist-94385
08/27/2024, 10:40 AMpython_sources(sources=["./**/*.py"])
and it should find all the python files you need. It will also recognize them as python files so that they can be linted, typechecked, etc.elegant-florist-94385
08/27/2024, 10:40 AMwide-midnight-78598
08/27/2024, 10:55 AMpython_sources()
will result in the same, non-recursive, resultacoustic-library-86413
08/27/2024, 11:20 AMpants run run.py
with os.listdir
with os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__))))
does not print any of the files that aren't explicitly imported or referenced, using either of the syntaxes, hmm.
The result is:
['__pycache__', 'run.py', '__init__.py']
elegant-florist-94385
08/27/2024, 11:46 AMpython_source
target that is known by pants. This is done.
Next, since run.py
is your entrypoint, pants needs to know what files are required by that entrypoint. Typically, dependency inference (ie. reading the import statements) can do this for you. But, as you've identified, with dynamic imports going on, pants can't tell. So you'll need to add the dependencies yourself.
I'll walk through a step by step of my thought process here:
• Following @wide-midnight-78598’s suggestion. Put python_sources()
right in the same directory, and change it to python_sources(name="source_files", dependencies=[":source_files"])
• It does seem a little strange for a target to reference itself, and indeed this may cause circular reference issues, so it might make more sense to do:
◦ `python_sources(name="deps", sources=["./*.py", "!./run.py"])`This is sources for all the files except run.py
◦ python_sources(name="run", sources="[./run.py"], dependencies=[":deps"])
◦ Now, there is no overlap, but maintaining this will be not a lot of fun in the future.
• IMO, this is the best way to do it: Though it will require changing your source a little to account.
◦ put the dynamically imported files into a subdirectory and have subdir/BUILD: python_sources()
◦ BUILD file in directory with run.py
, decorators.py
, and providers.py
(presumably these are the statically imported files): python_sources(dependencies=["./subdir:subdir"])
◦ Now your dynamic stuff is nicely contained and can easily be covered by a single target generator, and its straightforward to make any other [group of] targets depend upon it.
◦ This would imply needing to change the search path used in your code to discover the files for importacoustic-library-86413
08/27/2024, 11:53 AMwide-midnight-78598
08/27/2024, 11:55 AMpex_binary
part of the build file as well?elegant-florist-94385
08/27/2024, 11:58 AMacoustic-library-86413
08/27/2024, 12:31 PMpython_sources(name="source_files", dependencies=[":source_files"])
works excellently. Thank you so much for the insight! I don't think I would have thought of that myself.elegant-florist-94385
08/27/2024, 12:34 PMelegant-florist-94385
08/27/2024, 12:35 PMrun.py
file. Its a cleaner way to do the option 2 above. This would enable you to (for example) have some other package/distribution import something from providers.py
without suddenly depending on the entire directory