b4b73a8acc
tracker-policy-pr / check-pr (pull_request) Successful in 10s
test / integration-docker (pull_request) Successful in 26s
test / unit (pull_request) Successful in 36s
lint / lint (push) Failing after 42s
test / stage-firecracker-inputs (pull_request) Successful in 2s
test / build-infra (pull_request) Successful in 3m57s
test / integration-firecracker (pull_request) Successful in 1m55s
test / coverage (pull_request) Successful in 2m14s
test / publish-infra (pull_request) Has been skipped
Remove the private _read_tty_line duplicate from backend/__init__.py and the local definition from cli/_common.py. Both now import from the shared bot_bottle.util module.
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
"""Cross-cutting utility helpers used by multiple modules.
|
|
|
|
Top-level (i.e. backend-agnostic) — backend-specific helpers live one
|
|
level deeper, under their backend package."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ipaddress
|
|
import os
|
|
import sys
|
|
|
|
|
|
def is_ip_literal(value: str) -> bool:
|
|
try:
|
|
ipaddress.ip_address(value)
|
|
except ValueError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def read_tty_line() -> str:
|
|
"""Mirror `IFS= read -r REPLY </dev/tty`. Falls back to stdin."""
|
|
try:
|
|
with open("/dev/tty", "r", encoding="utf-8") as tty:
|
|
return tty.readline().rstrip("\n")
|
|
except OSError:
|
|
return sys.stdin.readline().rstrip("\n")
|
|
|
|
|
|
def expand_tilde(path: str) -> str:
|
|
"""Expand a leading '~' to $HOME. Leaves paths without a leading
|
|
tilde unchanged. Falls back to the empty string if $HOME is unset
|
|
(callers should already have checked HOME if they care)."""
|
|
if path.startswith("~"):
|
|
home = os.environ.get("HOME", "")
|
|
return home + path[1:]
|
|
return path
|