kupferbootstrap/utils.py

48 lines
1.1 KiB
Python
Raw Normal View History

2021-09-30 01:00:28 +02:00
from shutil import which
import atexit
import subprocess
2021-09-30 01:00:28 +02:00
def programs_available(programs) -> bool:
if type(programs) is str:
programs = [programs]
for program in programs:
if not which(program):
return False
return True
def umount(dest):
return subprocess.run(
[
'umount',
'-lc',
dest,
],
capture_output=True,
)
2021-10-19 06:40:30 +02:00
def mount(src: str, dest: str, options=['bind'], fs_type=None, register_unmount=True) -> subprocess.CompletedProcess:
opts = []
for opt in options:
opts += ['-o', opt]
if fs_type:
opts += ['-t', fs_type]
result = subprocess.run(
['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
def git(cmd: list[str], dir='.', capture_output=False) -> subprocess.CompletedProcess:
return subprocess.run(['git'] + cmd, cwd=dir, capture_output=capture_output)