|
| 1 | +#!/usr/bin/env python |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +sys.path.insert(0, str(Path(__file__).parent.resolve())) |
| 25 | + |
| 26 | +from common_precommit_utils import console |
| 27 | + |
| 28 | +# update this version when we switch to a newer version of Python |
| 29 | +required_version = tuple(map(int, "3.9".split("."))) |
| 30 | +required_version_str = f"{required_version[0]}.{required_version[1]}" |
| 31 | +global_version = tuple( |
| 32 | + map( |
| 33 | + int, |
| 34 | + subprocess.run( |
| 35 | + [ |
| 36 | + "python3", |
| 37 | + "-c", |
| 38 | + 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")', |
| 39 | + ], |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + check=True, |
| 43 | + ) |
| 44 | + .stdout.strip() |
| 45 | + .split("."), |
| 46 | + ) |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +if global_version < required_version: |
| 51 | + console.print(f"[red]Python {required_version_str} or higher is required to install pre-commit.\n") |
| 52 | + console.print(f"[green]Current version is {global_version}\n") |
| 53 | + console.print( |
| 54 | + "[bright_yellow]Please follow those steps:[/]\n\n" |
| 55 | + f" * make sure that `python3 --version` is at least {required_version_str}\n" |
| 56 | + f" * run `pre-commit clean`\n" |
| 57 | + f" * run `pre-commit install --install-hooks`\n\n" |
| 58 | + ) |
| 59 | + console.print( |
| 60 | + "There are various ways you can set `python3` to point to a newer version of Python.\n\n" |
| 61 | + f"For example run `pyenv global {required_version_str}` if you use pyenv, or\n" |
| 62 | + f"you can use venv with python {required_version_str} when you use pre-commit, or\n" |
| 63 | + "you can use `update-alternatives` if you use Ubuntu, or\n" |
| 64 | + "you can set `PATH` to point to the newer version of Python.\n\n" |
| 65 | + ) |
| 66 | + sys.exit(1) |
| 67 | +else: |
| 68 | + console.print(f"Python version is sufficient: {required_version_str}") |
0 commit comments