I have noticed there is interest in programmatical...
# general
f
I have noticed there is interest in programmatically modifying
BUILD
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.
I had to add boilerplate code to a
BUILD
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/ropehttps://github.com/PyCQA/redbaronhttps://github.com/berkerpeksag/astorhttps://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.
Copy code
$ 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.
Copy code
python_library(sources=["*/**/*.py", "!tests/"])
was turned into
Copy code
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:
Copy code
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:
Copy code
$ 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.
👀 1
📣 1
c
Keep in mind it will reformat the 
BUILD
 file with the Bazel style which may be not what you want.
This is easily remedied in a follow up step with:
Copy code
./pants update-build-files
❤️ 1
h
Thanks for sharing! We currently have the
update-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 about
❤️ 1
h
This is neat! I'm impressed it works
🎯 1
b
This could be a good blog post, pretty much verbatim. Any interest?
f
Sure, happy to write 🙂 as long as you guys are happy to tell people that Bazel tools are okay to use 😉 my understanding is that you folks would be interested in having people moving from Bazel to Pants 🙂
b
You're essentially providing a window into one aspect of that movement. 😉
🙌 1
h
Imo, it's also definitely okay to use Bazel tools and Bazel itself too! Ultimately our goal is for people to have an excellent build experience, however they do that. It's part of why we're trying to put so much effort into incremental adoption, so you can use Pants in addition to other tools if that makes sense for your team: https://www.pantsbuild.org/docs/existing-repositories We hope to keep improving Pants such that you don't need to mix multiple tools, but I think it's important you still can it if makes sense for you
🙌 1
1
b
Yep. If a Bazel user can make the transition from Pants easier by using tools that they already are comfortable with, that's a win for them and for Pants. Thank you Bazel team 😉
f
okay then 🙂 I’ll DM Carina to discuss the details
🚀 1
❤️ 2