tools: Add 'rich' as dependency

Updates the code to be able to install multiple packages and show
a useful error message if pip is not available.

Rich will be used by future tools for prettier terminal output
of our tools, most importantly to be able to print status updates
of parallel presubmit checks.

BUG=b:239255137
TEST=uninstall python3-pip, removed all packages. Ran ./tools/cl

Change-Id: I50bfdf2bee279e6d6d02c7dd23131cba1fed2a8c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3824003
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Dennis Kempin 2022-08-10 17:07:34 +00:00 committed by crosvm LUCI
parent 108a335d7d
commit 3e1ed7926f
2 changed files with 47 additions and 8 deletions

View file

@ -10,3 +10,23 @@ wheel: <
name: "infra/python/wheels/argh-py2_py3"
version: "version:0.26.2"
>
wheel: <
name: "infra/python/wheels/mdurl-py3"
version: "version:0.1.2"
>
wheel: <
name: "infra/python/wheels/markdown-it-py-py3"
version: "version:2.1.0"
>
wheel: <
name: "infra/python/wheels/typing-extensions-py3"
version: "version:4.3.0"
>
wheel: <
name: "infra/python/wheels/pygments-py3"
version: "version:2.14.0"
>
wheel: <
name: "infra/python/wheels/rich-py3"
version: "version:13.2.0"
>

View file

@ -18,24 +18,43 @@ import sys
import subprocess
def ensure_package_exists(package: str):
"""Installs the specified package via pip if it does not exist."""
try:
__import__(package)
except ImportError:
def ensure_packages_exist(*packages: str):
"""Installs the specified packages via pip if it does not exist."""
missing_packages: List[str] = []
for package in packages:
try:
__import__(package)
except ImportError:
missing_packages.append(package)
if missing_packages:
try:
__import__("pip")
except ImportError:
print(f"Missing the 'pip' package. Please install 'python3-pip'.")
sys.exit(1)
package_list = ", ".join(missing_packages)
print(
f"Missing the python package {package}. Do you want to install? [y/N] ",
f"Missing python dependencies. Do you want to install {package_list}? [y/N] ",
end="",
flush=True,
)
response = sys.stdin.readline()
if response[:1].lower() == "y":
subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", package])
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "--user", *missing_packages]
)
print("Success. Please re-run the command.")
sys.exit(0)
else:
sys.exit(1)
ensure_package_exists("argh")
# Note: These packages can be installed automatically on developer machines, but need to be
# provided as CIPD packages for vpython in Luci CI. See tools/.vpython3 for how to add them.
ensure_packages_exist("argh", "rich")
from io import StringIO
from math import ceil