fresh-cat-90827
12/23/2021, 5:46 PMBUILD files (for instance, to add some boilerplate code across the monorepo or individual projects, such as disabling a formatter globally and then enabling it on the a project basis). I’ve explored this path and would love to share with the community. To learn more, see the thread.fresh-cat-90827
12/23/2021, 5:46 PMBUILD file for all python_library and python_tests targets. I am not considering a manual approach which would only work for a toy project. So we go for a programmatic update of the BUILD files.
One path to take was to use an AST / CST (abstract or concrete syntax tree) based approach (since BUILD files are valid Python files) and then just add extra arguments to a function call. This is fairly easy given the low complexity of the BUILD files in my case. A few libraries to consider are:
• https://github.com/python-rope/rope
• https://github.com/PyCQA/redbaron
• https://github.com/berkerpeksag/astor
• https://github.com/Instagram/LibCST
However, this may mean some work and also not everyone wants to get into the AST business 😄
Another approach was to use an OOTB tool to edit BUILD files which I did.
Because BUILD files in Pants share semantics with the BUILD files used in Bazel, I shamelessly used buildozer (part of Bazel build tools) to update my BUILD files. Keep in mind it will reformat the BUILD file with the Bazel style which may be not what you want.
$ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_tests
$ buildozer 'set skip_yapf True' 'set skip_mypy True' 'set skip_flake8 True' ///...:%python_library
Running these have modified all libraries and tests targets to have those arguments. E.g.
python_library(sources=["*/**/*.py", "!tests/"])
was turned into
python_library(
skip_flake8 = True,
skip_mypy = True,
skip_yapf = True,
sources = [
"*/**/*.py",
"!tests/",
],
)
However, I run Python formatter on the BUILD files anyway afterwards, so that wasn’t a problem for me:
python_library(
skip_flake8=True,
skip_mypy=True,
skip_yapf=True,
sources=[
"*/**/*.py",
"!tests/",
],
)
There are easy ways to have fine-grained control over what targets you’d like to run the editing command on, but you may need to consult the Bazel docs as they may differ from the ones used in Pants, e.g. editing only a single project files:
$ buildozer 'set skip_yapf False' 'set skip_mypy False' projectdir/subdir:%python_library
There may be plans to have some functionality similar to what buildozer provides as core of Pants, but until then it may be a sensible approach for some of us. I think it may be worth experimenting with.curved-television-6568
12/23/2021, 5:49 PMKeep in mind it will reformat theThis is easily remedied in a follow up step with:file with the Bazel style which may be not what you want.BUILD
./pants update-build-fileshundreds-father-404
12/23/2021, 5:50 PMupdate-build-files goal in Pants 2.8+, which gives a generic plugin hook for modifying BUILD files. Right now, it integrates Black and also has some deprecation fixers, like renaming python_library to python_sources
It could be expanded to do other things like what you're talking abouthappy-kitchen-89482
12/23/2021, 5:53 PMbusy-vase-39202
01/04/2022, 6:03 PMfresh-cat-90827
01/04/2022, 6:24 PMbusy-vase-39202
01/04/2022, 6:25 PMhundreds-father-404
01/04/2022, 6:27 PMbusy-vase-39202
01/04/2022, 6:30 PMfresh-cat-90827
01/04/2022, 6:35 PM