forked from Mirror/pmbootstrap
Introduce a new module: pmb.core to contain explicitly typed pmbootstrap API. The first component being Suffix and SuffixType. This explicitly defines what suffixes are possible, future changes should aim to further constrain this API (e.g. by validating against available device codenames or architectures for buildroot suffixes). Additionally, migrate the entire codebase over to using pathlib.Path. This is a relatively new part of the Python standard library that uses a more object oriented model for path handling. It also uses strong type hinting and has other features that make it much cleaner and easier to work with than pure f-strings. The Chroot class overloads the "/" operator the same way the Path object does, allowing one to write paths relative to a given chroot as: builddir = chroot / "home/pmos/build" The Chroot class also has a string representation ("native", or "rootfs_valve-jupiter"), and a .path property for directly accessing the absolute path (as a Path object). The general idea here is to encapsulate common patterns into type hinted code, and gradually reduce the amount of assumptions made around the codebase so that future changes are easier to implement. As the chroot suffixes are now part of the Chroot class, we also implement validation for them, this encodes the rules on suffix naming and will cause a runtime exception if a suffix doesn't follow the rules.
143 lines
4.6 KiB
Python
143 lines
4.6 KiB
Python
# Copyright 2023 Oliver Smith
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
import datetime
|
|
import logging
|
|
import os
|
|
import re
|
|
import readline
|
|
import sys
|
|
|
|
import pmb.config
|
|
from pmb.core.types import PmbArgs
|
|
|
|
|
|
class ReadlineTabCompleter:
|
|
"""Store intermediate state for completer function."""
|
|
|
|
def __init__(self, options):
|
|
""":param options: list of possible completions."""
|
|
self.options = sorted(options)
|
|
self.matches = []
|
|
|
|
def completer_func(self, input_text, iteration):
|
|
"""
|
|
:param input_text: text that shall be autocompleted
|
|
:param iteration: how many times "tab" was hit
|
|
"""
|
|
# First time: build match list
|
|
if iteration == 0:
|
|
if input_text:
|
|
self.matches = [s for s in self.options
|
|
if s and s.startswith(input_text)]
|
|
else:
|
|
self.matches = self.options[:]
|
|
|
|
# Return the N'th item from the match list, if we have that many.
|
|
if iteration < len(self.matches):
|
|
return self.matches[iteration]
|
|
return None
|
|
|
|
|
|
def ask(question="Continue?", choices=["y", "n"], default="n",
|
|
lowercase_answer=True, validation_regex=None, complete=None):
|
|
"""Ask a question on the terminal.
|
|
|
|
:param question: display prompt
|
|
:param choices: short list of possible answers, displayed after prompt if set
|
|
:param default: default value to return if user doesn't input anything
|
|
:param lowercase_answer: if True, convert return value to lower case
|
|
:param validation_regex: if set, keep asking until regex matches
|
|
:param complete: set to a list to enable tab completion
|
|
"""
|
|
styles = pmb.config.styles
|
|
|
|
while True:
|
|
date = datetime.datetime.now().strftime("%H:%M:%S")
|
|
line = question
|
|
if choices:
|
|
line += f" ({str.join('/', choices)})"
|
|
if default:
|
|
line += f" [{default}]"
|
|
line_color = f"[{date}] {styles['BOLD']}{line}{styles['END']}"
|
|
line = f"[{date}] {line}"
|
|
|
|
if complete:
|
|
readline.parse_and_bind('tab: complete')
|
|
delims = readline.get_completer_delims()
|
|
if '-' in delims:
|
|
delims = delims.replace('-', '')
|
|
readline.set_completer_delims(delims)
|
|
readline.set_completer(
|
|
ReadlineTabCompleter(complete).completer_func)
|
|
|
|
ret = input(f"{line_color}: ")
|
|
|
|
# Stop completing (question is answered)
|
|
if complete:
|
|
# set_completer(None) would use the default file system completer
|
|
readline.set_completer(lambda text, state: None)
|
|
|
|
if lowercase_answer:
|
|
ret = ret.lower()
|
|
if ret == "":
|
|
ret = str(default)
|
|
|
|
pmb.helpers.logging.logfd.write(f"{line}: {ret}\n")
|
|
pmb.helpers.logging.logfd.flush()
|
|
|
|
# Validate with regex
|
|
if not validation_regex:
|
|
return ret
|
|
|
|
pattern = re.compile(validation_regex)
|
|
if pattern.match(ret):
|
|
return ret
|
|
|
|
logging.fatal("ERROR: Input did not pass validation (regex: " +
|
|
validation_regex + "). Please try again.")
|
|
|
|
|
|
def confirm(args, question="Continue?", default=False, no_assumptions=False):
|
|
"""Convenience wrapper around ask for simple yes-no questions with validation.
|
|
|
|
:param no_assumptions: ask for confirmation, even if "pmbootstrap -y' is set
|
|
:returns: True for "y", False for "n"
|
|
"""
|
|
default_str = "y" if default else "n"
|
|
if args.assume_yes and not no_assumptions:
|
|
logging.info(question + " (y/n) [" + default_str + "]: y")
|
|
return True
|
|
answer = ask(question, ["y", "n"], default_str, True, "(y|n)")
|
|
return answer == "y"
|
|
|
|
|
|
def progress_print(args, progress):
|
|
"""Print a snapshot of a progress bar to STDOUT.
|
|
|
|
Call progress_flush to end printing progress and clear the line. No output is printed in
|
|
non-interactive mode.
|
|
|
|
:param progress: completion percentage as a number between 0 and 1
|
|
"""
|
|
width = 79
|
|
try:
|
|
width = os.get_terminal_size().columns - 6
|
|
except OSError:
|
|
pass
|
|
chars = int(width * progress)
|
|
filled = "\u2588" * chars
|
|
empty = " " * (width - chars)
|
|
percent = int(progress * 100)
|
|
if pmb.config.is_interactive and not args.details_to_stdout:
|
|
sys.stdout.write(f"\u001b7{percent:>3}% {filled}{empty}")
|
|
sys.stdout.flush()
|
|
sys.stdout.write("\u001b8\u001b[0K")
|
|
|
|
|
|
def progress_flush(args):
|
|
"""Finish printing a progress bar.
|
|
|
|
This will erase the line. Does nothing in non-interactive mode.
|
|
"""
|
|
if pmb.config.is_interactive and not args.details_to_stdout:
|
|
sys.stdout.flush()
|