kupferbootstrap/utils.py

131 lines
3.4 KiB
Python
Raw Normal View History

import atexit
import grp
2022-09-09 20:49:18 +02:00
import hashlib
import logging
2022-09-04 02:39:35 +02:00
import os
import pwd
2022-02-18 06:32:04 +01:00
import subprocess
2022-09-04 02:39:35 +02:00
import tarfile
2022-02-18 06:32:04 +01:00
from shutil import which
2022-09-04 02:39:35 +02:00
from typing import Generator, IO, Optional, Union, Sequence
2022-08-15 17:41:23 +02:00
from exec.cmd import run_cmd, run_root_cmd
2022-08-20 02:20:47 +02:00
_programs_available = dict[str, bool]()
2021-09-30 01:00:28 +02:00
2022-08-20 02:20:47 +02:00
def programs_available(programs: Union[str, Sequence[str]], lazy: bool = True) -> bool:
global _programs_available
2021-09-30 01:00:28 +02:00
if type(programs) is str:
programs = [programs]
for program in programs:
2022-08-20 02:20:47 +02:00
if program not in _programs_available or not lazy:
avail = bool(which(program))
_programs_available[program] = avail
if not _programs_available[program]:
2021-09-30 01:00:28 +02:00
return False
return True
2022-02-18 06:32:04 +01:00
def umount(dest: str, lazy=False):
return run_root_cmd(
[
'umount',
'-c' + ('l' if lazy else ''),
dest,
],
capture_output=True,
)
2022-02-18 06:32:04 +01:00
def mount(src: str, dest: str, options: list[str] = ['bind'], fs_type: Optional[str] = None, register_unmount=True) -> subprocess.CompletedProcess:
opts = []
for opt in options:
opts += ['-o', opt]
if fs_type:
opts += ['-t', fs_type]
result = run_root_cmd(
['mount'] + opts + [
src,
dest,
],
capture_output=False,
)
2021-10-19 06:40:30 +02:00
if result.returncode == 0 and register_unmount:
atexit.register(umount, dest)
return result
2022-02-18 06:32:04 +01:00
def check_findmnt(path: str):
result = run_root_cmd(
[
'findmnt',
'-n',
'-o',
'source',
path,
],
capture_output=True,
)
return result.stdout.decode().strip()
def git(cmd: list[str], dir='.', capture_output=False, user: Optional[str] = None) -> subprocess.CompletedProcess:
result = run_cmd(['git'] + cmd, cwd=dir, capture_output=capture_output, switch_user=user)
assert isinstance(result, subprocess.CompletedProcess)
return result
def log_or_exception(raise_exception: bool, msg: str, exc_class=Exception, log_level=logging.WARNING):
if raise_exception:
raise exc_class(msg)
else:
logging.log(log_level, msg)
def get_user_name(uid: Union[str, int]) -> str:
if isinstance(uid, int) or uid.isnumeric():
return pwd.getpwuid(int(uid)).pw_name
return uid
def get_group_name(gid: Union[str, int]) -> str:
if isinstance(gid, int) or gid.isnumeric():
return grp.getgrgid(int(gid)).gr_name
return gid
def get_uid(user: Union[int, str]) -> int:
if isinstance(user, int) or user.isnumeric():
return int(user)
return pwd.getpwnam(user).pw_uid
def get_gid(group: Union[int, str]) -> int:
if isinstance(group, int) or group.isnumeric():
return int(group)
return grp.getgrnam(group).gr_gid
2022-09-04 02:39:35 +02:00
def read_files_from_tar(tar_file: str, files: Sequence[str]) -> Generator[tuple[str, IO], None, None]:
assert os.path.exists(tar_file)
with tarfile.open(tar_file) as index:
for path in files:
fd = index.extractfile(index.getmember(path))
assert fd
yield path, fd
2022-09-09 20:49:18 +02:00
# stackoverflow magic from https://stackoverflow.com/a/44873382
def sha256sum(filename):
h = hashlib.sha256()
b = bytearray(128 * 1024)
mv = memoryview(b)
with open(filename, 'rb', buffering=0) as f:
while n := f.readinto(mv):
h.update(mv[:n])
return h.hexdigest()