-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpydiskutil.py
executable file
·135 lines (121 loc) · 4.69 KB
/
pydiskutil.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
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
#!/usr/bin/env python
'''
This script collects data from diskutil
'''
##############################################################################
# Copyright 2014 Joseph Chilcote
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
##############################################################################
import os, sys, subprocess
import plistlib
class Diskutil(object):
def __init__(self):
'''Initialize object'''
self.data = self.get_data()
def get_data(self):
'''Returns a dict of all diskutil data'''
cmd = '/usr/sbin/diskutil', 'list', '-plist'
task = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = task.communicate()
plist = plistlib.readPlistFromString(out)
return plist
def get_alldisks(self):
'''Returns a dict of all disk identifiers'''
d = self.data
d1 = {}
d1['All Disks'] = d['AllDisks']
return d1
def get_wholedisks(self):
'''Returns a dict of all whole disk identifiers'''
d = self.data
d1 = {}
d1['Whole Disks'] = d['WholeDisks']
return d1
def get_allpartitions(self):
'''Returns a dict of all disk partitions'''
d = self.data
d1 = {}
d1['Partitions'] = d['AllDisksAndPartitions']
return d1
def get_volumes(self):
'''Returns a dict of all mounted volumes'''
d = self.data
d1 = {}
d1['Volumes'] = d['VolumesFromDisks']
return d1
def get_partitions(self):
'''Returns a dict of all partitions'''
d = self.data
d1 = {}
disks = d['AllDisks']
partitions = d['AllDisksAndPartitions']
for partition in partitions:
if 'Partitions' in partition:
for p in partition['Partitions']:
if 'MountPoint' in p:
d1[p['DeviceIdentifier']] = (
p['VolumeName'],
p['Content'],
p['Size'],
p['MountPoint']
)
elif 'VolumeName' in p and not 'MountPoint' in p:
d1[p['DeviceIdentifier']] = (
p['VolumeName'],
p['Content'],
p['Size'],
''
)
else:
d1[p['DeviceIdentifier']] = (
'',
p['Content'],
p['Size'],
''
)
else:
d1[partition['DeviceIdentifier']] = (
partition['VolumeName'],
partition['Content'],
partition['Size'],
partition['MountPoint']
)
return d1
def get_bootvolume(self):
'''Returns the boot volume'''
d = {}
p = self.get_partitions()
for k, v in p.items():
if '/' in v:
d['Boot Volume'] = v[0]
return d
def main():
du = Diskutil()
# print du.get_data()
# print du.get_alldisks()
# print du.get_wholedisks()
vol = du.get_volumes()
for k, v in vol.items():
print '%s: %s' % (k, v)
bv = du.get_bootvolume()
for k, v in bv.items():
print '%s: %s' % (k, v)
p = du.get_partitions()
for k, v in sorted(p.items()):
print '%s: %s' % (k, v)
print du.get_alldisks()
print du.get_wholedisks()
if __name__ == "__main__":
main()