2021-09-30 01:00:28 +02:00
|
|
|
from shutil import which
|
2021-10-01 18:25:42 +02:00
|
|
|
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
|
2021-10-01 18:25:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def umount(dest):
|
|
|
|
return subprocess.run(
|
|
|
|
[
|
|
|
|
'umount',
|
|
|
|
'-lc',
|
|
|
|
dest,
|
|
|
|
],
|
2021-10-04 12:57:52 +02:00
|
|
|
capture_output=True,
|
2021-10-01 18:25:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-10-19 06:40:30 +02:00
|
|
|
def mount(src: str, dest: str, options=['bind'], fs_type=None, register_unmount=True) -> subprocess.CompletedProcess:
|
2021-10-01 18:25:42 +02:00
|
|
|
opts = []
|
|
|
|
for opt in options:
|
|
|
|
opts += ['-o', opt]
|
|
|
|
|
2021-10-10 18:29:08 +02:00
|
|
|
if fs_type:
|
|
|
|
opts += ['-t', fs_type]
|
2021-10-01 18:25:42 +02:00
|
|
|
|
2021-10-04 20:16:27 +02:00
|
|
|
result = subprocess.run(
|
2021-10-10 18:29:08 +02:00
|
|
|
['mount'] + opts + [
|
2021-10-04 20:16:27 +02:00
|
|
|
src,
|
|
|
|
dest,
|
|
|
|
],
|
|
|
|
capture_output=False,
|
|
|
|
)
|
2021-10-19 06:40:30 +02:00
|
|
|
if result.returncode == 0 and register_unmount:
|
2021-10-04 12:57:52 +02:00
|
|
|
atexit.register(umount, dest)
|
|
|
|
return result
|
2021-10-17 03:19:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def git(cmd: list[str], dir='.', capture_output=False) -> subprocess.CompletedProcess:
|
|
|
|
return subprocess.run(['git'] + cmd, cwd=dir, capture_output=capture_output)
|