Files
brave-core/tools/cr/vpython_utils.py
cdesouza-chromium 1796f9c9a0 [brockit] Tooling path further restructure for relative paths (#36322)
This change does further restruturing to `tools/cr`, as now we are fully
reliant on paths being completely relative, and derived primarily from
`Repository.brave.root` as an anchor for all other relative paths.

With relative paths already landed, we can now further break down
`alias`, and make tests more reliable, by using `FakeChromiumSrc`
sandboxing for all tests. In particular, `tools/cr/commit.py` has also
been introduced, and now the hook destination path is fully relative to
`brave-core` root. This potentially fixes a recurring issue that in some
cases running tests for `install-hook` was uninstalling hooks in the
current `brave-core` checkout.

As an additional, `git cr` calls were not propagating the `cwd`
correctly, and this can now be corrected too.

This change also adds a README.md, to provide some overview of this
path, and some instructions that may be useful to be picked by AI agents
when writing tests.

For better guarantees, `vpython3_utils.py` now handles the file path
resolution to the vpython3 runtime across all applications in this path.

Finally, logging policy is now established with a default logger for
anything using `terminal.py`. This logger can be replaced by individual
applications, but it is of great benefit to be able to call anything
using `terminal.py` with `--verbose` at any moment.
2026-05-11 17:27:13 +01:00

54 lines
2.0 KiB
Python

# Copyright (c) 2026 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
"""Locates the `vpython3` interpreter for tools/cr scripts.
Use `VPYTHON3_PATH` when spawning vpython3 from tools/cr code so callers
work whether or not depot_tools is on the user's PATH.
"""
from pathlib import Path
import platform
import shutil
import repository
def _compute_vpython3_path() -> Path:
"""Resolves the `vpython3` interpreter for use by tools/cr scripts.
Precedence:
1. POSIX with `vpython3` on `$PATH` — `Path('vpython3')` so callers
do a fresh `$PATH` lookup at invocation time.
2. Windows with `vpython3` on `$PATH` — the absolute path returned
by `shutil.which`, because git's bundled bash (used to execute
`!`-prefixed aliases) doesn't reliably resolve `.bat` wrappers
like `vpython3.bat` via `$PATH`.
3. Neither — the chromium-bundled `third_party/depot_tools/vpython3`,
resolved to absolute at module import so later cwd changes (e.g.
`FakeChromiumRepo.setup()`) don't invalidate it.
"""
found = shutil.which('vpython3')
if found is None:
return (repository.chromium.root / 'third_party' / 'depot_tools' /
'vpython3').resolve()
if platform.system() == 'Windows':
return Path(found)
return Path('vpython3')
VPYTHON3_PATH: Path = _compute_vpython3_path()
def is_found_in_path_variable() -> bool:
"""Returns True if `VPYTHON3_PATH` is resolved via the shell's `$PATH`.
A PATH-resolved value is a bare command name like `Path('vpython3')`,
which has no parent directory (`parent == Path('.')`); shell and
subprocess machinery look it up via `$PATH` at invocation time. The
chromium-bundled fallback, by contrast, is an absolute path with a
real parent directory.
"""
return VPYTHON3_PATH.parent == Path('.')