Utils: add check_findmnt(), log_or_exception(), add PathLike type hint
This commit is contained in:
parent
863fbc1df0
commit
2e5205c9e9
1 changed files with 28 additions and 3 deletions
31
utils.py
31
utils.py
|
@ -2,6 +2,10 @@ from shutil import which
|
||||||
import atexit
|
import atexit
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from os import PathLike
|
||||||
|
|
||||||
|
|
||||||
def programs_available(programs) -> bool:
|
def programs_available(programs) -> bool:
|
||||||
if type(programs) is str:
|
if type(programs) is str:
|
||||||
|
@ -12,18 +16,18 @@ def programs_available(programs) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def umount(dest):
|
def umount(dest: PathLike, lazy=False):
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
[
|
[
|
||||||
'umount',
|
'umount',
|
||||||
'-lc',
|
'-c' + ('l' if lazy else ''),
|
||||||
dest,
|
dest,
|
||||||
],
|
],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def mount(src: str, dest: str, options=['bind'], fs_type=None, register_unmount=True) -> subprocess.CompletedProcess:
|
def mount(src: PathLike, dest: PathLike, options=['bind'], fs_type=None, register_unmount=True) -> subprocess.CompletedProcess:
|
||||||
opts = []
|
opts = []
|
||||||
for opt in options:
|
for opt in options:
|
||||||
opts += ['-o', opt]
|
opts += ['-o', opt]
|
||||||
|
@ -43,5 +47,26 @@ def mount(src: str, dest: str, options=['bind'], fs_type=None, register_unmount=
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def check_findmnt(path: PathLike):
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
'findmnt',
|
||||||
|
'-n',
|
||||||
|
'-o',
|
||||||
|
'source',
|
||||||
|
path,
|
||||||
|
],
|
||||||
|
capture_output=True,
|
||||||
|
)
|
||||||
|
return result.stdout.decode().strip()
|
||||||
|
|
||||||
|
|
||||||
def git(cmd: list[str], dir='.', capture_output=False) -> subprocess.CompletedProcess:
|
def git(cmd: list[str], dir='.', capture_output=False) -> subprocess.CompletedProcess:
|
||||||
return subprocess.run(['git'] + cmd, cwd=dir, capture_output=capture_output)
|
return subprocess.run(['git'] + cmd, cwd=dir, capture_output=capture_output)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue