jw-pkg/src/python/jw/pkg/lib/util.py
Jan Lindemann 372356a8df
lib.util: Add kebab_to_pascal() and pascal_to_kebab()
PathFilter.create() currently instantiates individual filters in a match -
case over the filter URI's protocol. The plan is to instantiate filters
without match / case based on a protocol naming scheme only, so we better
introduce one at this point. Adopt jw-python's kebab_to_pascal() and
pascal_to_kebab() functions as a foundation for a future naming convention.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-18 19:13:01 +01:00

398 lines
13 KiB
Python

from __future__ import annotations
import json
import os
import re
import sys
from enum import Enum, auto
from typing import TYPE_CHECKING, Any, Iterable, TypeVar, cast
from .base import Input, InputMode, Result
from .FileContext import FileContext
from .log import DEBUG, ERR, log
from .Uri import Uri
if TYPE_CHECKING:
from argparse import Namespace
from .ExecContext import ExecContext
from .PathFilter import PathFilter
from .ProcFilter import ProcFilter, ProcPipeline
T = TypeVar('T')
class AskpassKey(Enum):
Username = auto()
Password = auto()
def pretty_cmd(cmd: list[str] | None = None, wd: str | None = None) -> str:
if cmd is None:
cmd = sys.argv
tokens = [cmd[0]]
for token in cmd[1:]:
if token.find(' ') != -1:
token = '"' + token + '"'
tokens.append(token)
ret = ' '.join(tokens)
if wd is not None:
ret += f' in {wd}'
return ret
def kebab_to_pascal(value: str) -> str:
return ''.join(part.capitalize() for part in value.split('-'))
def pascal_to_kebab(value: str) -> str:
# Handles normal CamelCase / PascalCase, including many acronym cases
s1 = re.sub(r'(.)([A-Z][a-z]+)', r'\1-\2', value)
s2 = re.sub(r'([a-z0-9])([A-Z])', r'\1-\2', s1)
return s2.lower()
# See ExecContext.run() for what this function does
async def run_cmd(
*args: Any,
ec: ExecContext | None = None,
verbose: bool | None = None,
cmd_input: Input = InputMode.NonInteractive,
**kwargs: Any,
) -> Result:
if verbose is None:
verbose = False if ec is None else ec.verbose_default
if ec is None:
from .ec.Local import Local
interactive = cmd_input == InputMode.Interactive
ec = Local(verbose_default = verbose, interactive = interactive)
kwargs['verbose'] = verbose
kwargs['cmd_input'] = cmd_input
return await ec.run(*args, **kwargs)
async def run_curl(
args: list[str],
wd: str | None = None,
throw: bool | None = None,
verbose: bool | None = None,
cmd_input: Input = InputMode.NonInteractive,
ec: ExecContext | None = None,
decode: bool = False,
) -> Result:
if verbose is None:
verbose = False if ec is None else ec.verbose_default
cmd = ['curl']
if not verbose:
cmd.append('-s')
cmd.extend(args)
return await run_cmd(
cmd, wd = wd, throw = throw, verbose = verbose, cmd_input = cmd_input, ec = ec
)
async def run_curl_into(
expected_type: type[T],
args: list[str],
**kwargs: Any,
) -> T:
result = await run_curl(args, **kwargs)
stdout = result.stdout_str
try:
ret = json.loads(stdout)
except Exception as e:
log(
ERR,
f'Failed to parse {len(stdout)} bytes of Curl output ({str(e)})',
file = sys.stderr,
)
raise
if not isinstance(ret, expected_type):
raise TypeError(
f'Expected {expected_type.__name__}, got {type(ret).__name__} from Curl'
)
return ret
async def run_askpass(
askpass_env: list[str],
key: AskpassKey,
host: str | None = None,
ec: ExecContext | None = None,
throw: bool = False,
) -> str | None:
if host is not None: # Currently unsupported
raise NotImplementedError(f'Tried to run askpass with host "{host}"')
for var in askpass_env:
exe = os.getenv(var)
if exe is None:
continue
exe_arg = ''
match var:
case 'GIT_ASKPASS':
match key:
case AskpassKey.Username:
exe_arg += 'Username'
case AskpassKey.Password:
exe_arg += 'Password'
case 'SSH_ASKPASS':
match key:
case AskpassKey.Username:
continue # Can't get user name from SSH_ASKPASS
case AskpassKey.Password:
exe_arg += 'Password'
case _:
pass
result = await run_cmd([exe, exe_arg], throw = throw, ec = ec)
if result.status == 0 and result.stdout_or_none is not None:
ret = result.stdout_str_or_none
if ret:
return ret
msg = (
f"Trying to get user data from {', '.join(askpass_env)} didn't produce anything"
)
if throw:
raise Exception(msg)
log(DEBUG, msg)
return None
async def run_sudo(
cmd: list[str],
*args: Any,
interactive: bool = True,
ec: ExecContext | None = None,
**kwargs: Any,
) -> Result:
if ec is None:
from .ec.Local import Local
ec = Local(interactive = interactive)
return await ec.sudo(cmd, *args, **kwargs)
async def get(
uri: str | Uri,
*args: Any,
ctx: FileContext | None = None,
content_filter: ProcFilter | list[ProcFilter] | ProcPipeline | None = None,
**kwargs: Any,
) -> Result:
uri = Uri.pimp(uri)
if ctx is None or uri.id != ctx.uri.id:
from .FileContext import FileContext
ctx = FileContext.create(uri)
from .ProcFilter import run as run_pipeline
return await run_pipeline(await ctx.get(uri.path, *args, **kwargs), content_filter)
async def copy(
src: str | FileContext | Iterable[str],
dst: str | FileContext,
owner: str | None = None,
group: str | None = None,
mode: int | None = None,
throw: bool = True,
recursive: bool = False,
parents: bool = False,
default_path_filter: str | PathFilter | None = None,
) -> Exception | str | list[str]:
async def __copy_tree(src_path: str, dst_path: str) -> list[str]:
ret: list[str] = []
s_st = await ctx.src.stat(src_path)
if dst_path and not await ctx.dst.is_dir(dst_path):
await ctx.dst.mkdir(dst_path, ftype = s_st.ftype)
for dirent in await ctx.src.readdir(src_path):
s_path = f'{src_path}/{dirent}' if src_path else dirent
d_path = f'{dst_path}/{dirent}' if dst_path else dirent
s_st = await ctx.src.stat(s_path)
# -- Descend into directories that are not fetchable
# whole; an entry, which is both, lands as a file
if s_st.is_dir and not s_st.is_gettable:
ret.extend(await __copy_tree(s_path, d_path))
continue
result = await ctx.src.get(s_path, throw = True)
await ctx.dst.put(
path = d_path,
content = result.stdout,
owner = owner,
group = group,
mode = mode,
ftype = s_st.ftype,
throw = True,
)
ret.append(d_path)
return ret
# Destination address / addresses
ret: str | list[str]
if not isinstance(src, (str, FileContext)):
ret = []
for uri in src: # TODO: Group identical netlocs into one CopyContext
rr = await copy(
uri,
dst,
owner,
group,
mode,
throw,
recursive,
parents,
default_path_filter,
)
if isinstance(rr, Exception):
return rr
if isinstance(rr, list):
ret.extend(rr)
if isinstance(rr, str):
ret.append(rr)
else:
raise Exception(f'copy() returned unexpected type {type(rr)}')
return ret
try:
from .CopyContext import CopyContext
async with CopyContext(src, dst,
default_path_filter = default_path_filter) as ctx:
src_uri = src.uri if isinstance(src, FileContext) else Uri(src)
ret = '' if ctx.dst.chroot else ctx.dst.root
if parents:
# -- cp --parents: the source lands at its source path
# below the destination root, each parent level
# typed by the source-side node it mirrors
parts = [p for p in src_uri.path.split('/') if p]
if parts:
src_path = '/'.join(parts)
base = ret
ret = f'{base}/{src_path}' if base else src_path
s_cur = ''
for part in parts[:-1]:
s_cur = f'{s_cur}/{part}' if s_cur else part
d_cur = f'{base}/{s_cur}' if base else s_cur
if await ctx.dst.is_dir(d_cur):
continue
level_ctx = FileContext.create(
f'{ctx.src.uri.scheme_plus_authority}/{s_cur}',
chroot = True,
)
async with level_ctx:
ftype = (await level_ctx.stat('')).ftype
await ctx.dst.mkdir(d_cur, ftype = ftype)
elif ret:
# Land the source's basename within existing destination directories
basename = os.path.basename(src_uri.path.rstrip('/'))
if basename and await ctx.dst.is_dir(ret):
ret += '/' + basename
src_root = '' if ctx.src.chroot else ctx.src.root
s_st = await ctx.src.stat(src_root)
if s_st.is_dir and not s_st.is_gettable:
# -- A directory is not a single fetchable object,
# and cp copies directories only with -r
if not recursive:
raise Exception(
f'Copying directory "{src}" requires recursive=True'
)
return await __copy_tree(src_root, ret)
result = await ctx.src.get(src_root, throw = True)
await ctx.dst.put(
path = ret,
content = result.stdout,
owner = owner,
group = group,
mode = mode,
ftype = s_st.ftype,
throw = True,
)
return ret
except Exception as e:
if throw:
raise
log(ERR, f'Failed to copy {src} -> {dst} ({str(e)})')
return e
async def get_username( # export
args: Namespace | None = None,
url: str | None = None,
askpass_env: list[str] = [],
ec: ExecContext | None = None,
) -> str | None:
url_user = None if url is None else Uri(url).username
if args is not None:
if args.username is not None:
if url_user is not None and url_user != args.username:
raise Exception(
f'Username mismatch: called with --username="{args.username}", '
f'URL has user name "{url_user}"'
)
return str(args.username)
if url_user is not None:
return url_user
return await run_askpass(askpass_env, AskpassKey.Username, ec = ec)
async def get_password( # export
args: Namespace | None = None,
url: str | None = None,
askpass_env: list[str] = [],
ec: ExecContext | None = None,
) -> str | None:
if args is None and url is None and not askpass_env:
raise Exception(
'Neither URL nor command-line arguments nor askpass environment variable '
"available, can't get password"
)
if args is not None and hasattr(args, 'password'):
# use getattr(), because we don't necessarily want to have insecure
# --password among options
ret = cast('str | None', getattr(args, 'password'))
if ret is not None:
return ret
if url is not None:
ret = Uri(url).password
if ret is not None:
return ret
return await run_askpass(askpass_env, AskpassKey.Password, ec = ec)
async def get_profile_env( # export
throw: bool = True,
keep: Iterable[str] | bool = False,
ec: ExecContext | None = None,
) -> dict[str, str]:
"""
Get a fresh environment from /etc/profile
Args:
keep:
- False -> Don't keep anything
- True -> Keep what's in the current environment
- List of strings -> Keep those variables
Returns:
Dictionary with fresh environment
"""
mod_env: dict[str, str] | None = None
if (not keep) or isinstance(keep, Iterable):
mod_env = {
'HOME': os.environ.get('HOME', '/'),
'USER': os.environ.get('USER', ''),
'PATH': '/usr/bin:/bin',
}
# Run bash as a login shell, which sources /etc/profile, then print
# environment as NUL-separated key=value pairs
cmd = ['/usr/bin/env', '-i', '/bin/bash', '-lc', 'env -0']
result = await run_cmd(
cmd, throw = throw, verbose = True, mod_env = mod_env, ec = ec
)
ret: dict[str, str] = {}
stdout = result.stdout_or_none
if stdout is not None:
for entry in stdout.rstrip(b'\0').split(b'\0'):
if not entry:
continue
bkey, bval = entry.split(b'=', 1)
ret[bkey.decode()] = bval.decode()
if isinstance(keep, Iterable):
for key in keep:
val = os.getenv(key)
if val is not None:
ret[key] = val
return ret