-
Notifications
You must be signed in to change notification settings - Fork 0
/
dism_operations.py
60 lines (49 loc) · 1.91 KB
/
dism_operations.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
59
60
import subprocess
def dism_get_wiminfo(source_file_path: str) -> str:
try:
result = subprocess.run(
f"dism /get-wiminfo /wimfile:{source_file_path}",
stdout=subprocess.PIPE,
check=True,
)
return result.stdout.decode()
except subprocess.CalledProcessError as error:
match error.returncode:
case 2:
raise FileNotFoundError
case _:
raise error
def dism_export_image(
source_file_path: str, source_index: str, destination_file_path: str
) -> None:
try:
subprocess.run(
f"dism /export-image /sourceimagefile:{source_file_path} /sourceindex:{source_index} /destinationimagefile:{destination_file_path} /compress:max /checkintegrity",
stdout=subprocess.PIPE,
check=True,
)
except subprocess.CalledProcessError as error:
match error.returncode:
case 2:
raise FileNotFoundError
case _:
raise error
def parse_dism_get_wiminfo_output(dism_get_wiminfo_output: str) -> list[dict[str, str]]:
# strip start and end of output, leaving only the image information
start_phrase: str = "Index : 1"
end_phrase: str = "bytes"
index: int = dism_get_wiminfo_output.find(start_phrase)
trimmed_output: str = dism_get_wiminfo_output[index:]
index = trimmed_output.rfind(end_phrase) + len(end_phrase)
trimmed_output = trimmed_output[:index]
# split string to images
images: list[str] = trimmed_output.split("\r\n\r\n")
# parse each image to a dictionary and append it to a list
parsed_output: list[dict[str, str]] = []
for image in images:
parsed_image: dict[str, str] = {}
for line in image.split("\r\n"):
key, val = line.split(" : ")
parsed_image[key] = val
parsed_output.append(parsed_image)
return parsed_output