-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
58 lines (45 loc) · 1.27 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from contextlib import contextmanager
import subprocess
import tempfile
import os
import shutil
from termcolor import colored
def log(*text):
print(colored(" ".join(text), "green"))
@contextmanager
def temp_dir():
d = tempfile.mkdtemp()
try:
yield d
finally:
shutil.rmtree(d)
@contextmanager
def mount(dev, mountpoint, opts=[]):
subprocess.run(["mount"] + opts + [dev, mountpoint], check=True)
try:
yield
finally:
subprocess.run(["umount", mountpoint], check=True)
def write_to_file(filename, contents, newline=True):
with open(filename, "w") as fi:
fi.write(contents)
if newline:
fi.write("\n")
def replace_lines_matching(filename, replacements, newline=True):
def maybe_replace(line):
for k, v in replacements.items():
if k in line:
return v
return line
with open(filename, "r") as fi:
lines = [li.strip() for li in fi.readlines()]
with open(filename, "w") as fi:
fi.write("\n".join(maybe_replace(line) for line in lines))
if newline:
fi.write("\n")
def get_part_uuid(dev):
return (
subprocess.check_output(["blkid", "-o", "value", "-s", "UUID", dev])
.decode()
.strip()
)