Verify old version of Python has the lib2to3 import available (#170187)

The `yapf.sh` script checks for specific "known-to-work-with-YAPF"
versions of Python to run YAPF with. On my machine, I have `python3.11`
and `python3.12` (`python3` is a symlink to `python3.12`). However, the
`python3.11` release doesn't have `lib2to3` so the YAPF script fails
with:

```
Checking Python formatting...
To fix, run `et format` or:                                                                                   

git apply <<DONE
  /usr/local/google/home/het/Projects/flutter/engine/src/flutter/tools/gn
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/local/google/home/het/Projects/flutter/engine/src/flutter/third_party/yapf/yapf/__main__.py", line 16, in <module>
    import yapf
  File "/usr/local/google/home/het/Projects/flutter/engine/src/flutter/third_party/yapf/yapf/__init__.py", line 35, in <module>
    from yapf.yapflib import errors
  File "/usr/local/google/home/het/Projects/flutter/engine/src/flutter/third_party/yapf/yapf/yapflib/errors.py", line 16, in <module>
    from lib2to3.pgen2 import tokenize
ModuleNotFoundError: No module named 'lib2to3'

DONE
```

On my Ubuntu machine I cannot install `lib2to3` through normal APT
packages; `apt install python3.11-lib2to3` fails since that package has
been obsoleted. I've been able to work around this by manually editing
the `yapf.sh` script to check if `lib2to3` can be imported before using
that version of Python.

See https://github.com/flutter/flutter/issues/158384

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Harry Terkelsen 2025-06-10 15:51:57 -07:00 committed by GitHub
parent 3f99c8d192
commit db8a64f90d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@ set -e
function follow_links() (
cd -P "$(dirname -- "$1")"
file="$PWD/$(basename -- "$1")"
while [[ -h "$file" ]]; do
while [[ -L "$file" ]]; do
cd -P "$(dirname -- "$file")"
file="$(readlink -- "$file")"
cd -P "$(dirname -- "$file")"
@ -34,16 +34,36 @@ function follow_links() (
)
SCRIPT_DIR=$(follow_links "$(dirname -- "${BASH_SOURCE[0]}")")
SRC_DIR="$(cd "$SCRIPT_DIR/../.."; pwd -P)"
YAPF_DIR="$(cd "$SRC_DIR/flutter/third_party/yapf"; pwd -P)"
SRC_DIR="$(
cd "$SCRIPT_DIR/../.."
pwd -P
)"
YAPF_DIR="$(
cd "$SRC_DIR/flutter/third_party/yapf"
pwd -P
)"
has_lib2to3_check_script="
import sys
version = sys.version_info
try:
__import__('lib2to3')
print(f'Python3 version {version.major}.{version.minor} has '
f'the lib2to3 import.')
except ImportError:
print(f'Python3 version {version.major}.{version.minor} does not have '
f'the lib2to3 import.',
file=sys.stderr)
sys.exit(1)
"
# TODO: https://github.com/flutter/flutter/issues/158384
# Migrate to a supported Python formatter.
if command -v python3.10 &> /dev/null; then
if command -v python3.10 &>/dev/null && (python3.10 -c "$has_lib2to3_check_script" || exit 1); then
PYTHON_EXEC="python3.10"
elif command -v python3.11 &> /dev/null; then
elif command -v python3.11 &>/dev/null && (python3.11 -c "$has_lib2to3_check_script" || exit 1); then
PYTHON_EXEC="python3.11"
elif command -v python3.12 &> /dev/null; then
elif command -v python3.12 &>/dev/null && (python3.12 -c "$has_lib2to3_check_script" || exit 1); then
PYTHON_EXEC="python3.12"
else
python3 -c "