This repository was archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (181 loc) · 4.96 KB
/
Copy pathmain.py
File metadata and controls
183 lines (181 loc) · 4.96 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os, re, sys, hashlib
from pprint import pprint
tempFile = os.environ["TEMP"].replace("\\", "/")+"/python-diskpart.txt"
class diskpart:
def __init__(self, fetch_uid=False):
self.mainC = "diskpart /s "+tempFile
self.selected = False
self.selectedPart = False
self.opts = {
"fetch_uid": fetch_uid
}
self.parts = {
"disk": {
0: "disk_num",
1: "status",
2: "size",
3: "free",
4: "dyn",
5: "gpt"
},
"volume": {
0: "volume_num",
1: "letter",
2: "name",
3: "format",
4: "type",
5: "size",
6: "status",
7: "info"
},
"partition": {
0: "partition",
1: "type",
2: "size",
3: "offset"
}}
self.listDisk()
self.listVolume()
def write(self, cmd):
with open(tempFile, "w") as f:
f.write(cmd)
def exec(self, cmd):
test = os.popen(cmd).read()
return test
def listDisk(self):
cmd = "list disk"
self.write(cmd)
command = self.mainC+' | findstr /r "Disk.*Online ----"'
result = self.exec(command)
template = result.split("\n")[0]
temp = result.split("\n")[1:-1]
disks = {}
parts = self.lister(template, temp)
ind=0
for text in temp:
for part in parts:
p = text[parts[part]["begin"]:parts[part]["end"]].replace(" ", "")
if part == 0:
disks[ind] = {"partitions": {}}
elif part == 4 or part == 5:
if p == "*":
disks[ind][self.parts["disk"][part]] = True
else:
disks[ind][self.parts["disk"][part]] = False
else:
disks[ind][self.parts["disk"][part]] = p
ind += 1
if self.opts["fetch_uid"]:
command = self.mainC+' | findstr /r "Disk.ID:"'
bulkC = ""
for disk in disks:
bulkC += "select disk {}\r\nuniqueid disk\r\n".format(disk)
self.write(bulkC)
result = self.exec(command).split("\n")[:-1]
ind = 0
for uid in result:
uid = re.subn("(Disk ID: )|[{}]", "", uid)[0]
disks[ind]["uid"] = uid
disks[ind]["display_id"] = hashlib.md5(uid.encode("utf-8")).hexdigest()[:6]
ind += 1
self.disks = disks
return self.disks
def listVolume(self):
cmd = "list volume"
self.write(cmd)
command = self.mainC+' | findstr /r "Volume.* ----"'
result = self.exec(command)
template = result.split("\n")[1]
temp = result.split("\n")[2:-1]
volumes = {}
parts = self.lister(template, temp)
ind=0
for text in temp:
for part in parts:
p = text[parts[part]["begin"]:parts[part]["end"]].replace(" ", "")
if part == 0:
volumes[ind] = {}
elif part == 1 or part == 2 or part == 7:
if p == "":
volumes[ind][self.parts["volume"][part]] = False
else:
volumes[ind][self.parts["volume"][part]] = p
else:
volumes[ind][self.parts["volume"][part]] = p
ind += 1
self.volumes = volumes
return self.volumes
def listPartition(self):
if type(self.selected) == int:
cmd = "select disk {}\nlist partition".format(self.selected)
self.write(cmd)
command = self.mainC+' | findstr /r "Partition ---"'
result = self.exec(command)
template = result.split("\n")[1]
temp = result.split("\n")[2:-1]
partitions = {}
parts = self.lister(template, temp)
ind=0
for text in temp:
for part in parts:
p = text[parts[part]["begin"]:parts[part]["end"]].replace(" ", "")
if part == 0:
partitions[ind] = {}
elif part == 1:
if p == "Unknown":
partitions[ind][self.parts["partition"][part]] = False
else:
partitions[ind][self.parts["partition"][part]] = p
else:
partitions[ind][self.parts["partition"][part]] = p
ind += 1
self.disks[self.selected]["partitions"] = partitions
return partitions
else:
raise Exception("You need to select a disk before using this function.")
def clean(self):
if type(self.selected) == int:
cmd = "select disk {}\r\nclean".format(self.selected)
self.write(cmd)
self.exec(self.mainC)
else:
raise Exception("You need to select a disk before using this function.")
def createPartition(self, partition):
if type(self.selected) == int:
if partition and type(partition) == str:
cmd = "select disk {}\r\ncreate partition {}\r\n".format(self.selected, partition)
self.write(cmd)
self.exec(self.mainC)
self.listPartition()
else:
raise ValueError("Expected string as input but got '{}' instead.".format(type(partition)))
else:
raise Exception("You need to select a disk before using this function.")
def selectDisk(self, diskNum):
try:
int(diskNum)
except:
raise ValueError("Expected integer as input but got '{}' instead.".format(diskNum))
else:
if int(diskNum) <= len(self.disks):
self.selected = diskNum
else:
raise Exception("Selected '{}' disk is not present.".format(diskNum))
def lister(self, template, data):
lastVal = 0
totalLen = 0
partNum = 0
parts = {}
for i in template:
if i == "-":
if lastVal == 0:
parts[partNum] = {"begin": totalLen}
lastVal = 1
else:
if lastVal == 1:
parts[partNum]["end"] = totalLen
partNum += 1
lastVal = 0
totalLen +=1
parts[len(parts)-1]["end"] = totalLen
return parts