forked from turnkeylinux/ebsmount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudevdb.py
106 lines (85 loc) · 3.18 KB
/
udevdb.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
# Copyright (c) 2010 Alon Swartz <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from executil import ExecError, getoutput
class Device:
"""class to hold device information enumerated from udev database"""
def __init__(self, s, volinfo=True):
self.path = ''
self.name = ''
self.symlinks = []
self.env = {}
self._parse_raw_data(s)
if volinfo:
self._get_volinfo()
def _parse_raw_data(self, s):
for entry in s.splitlines():
type, value = entry.split(':', 1)
type = type.strip()
value = value.strip()
if type == "P":
self.path = value
continue
if type == "N":
self.name = value
continue
if type == "S":
self.symlinks.append(value)
continue
if type == "E":
name, val = value.split("=", 1)
self.env[name] = val.lstrip("=")
def _get_volinfo(self):
if self.env.has_key('DEVTYPE') and self.env['DEVTYPE'] == 'disk':
try:
volume_info = getoutput('vol_id /dev/%s' % self.name)
except ExecError:
return
for value in volume_info.splitlines():
name, val = value.split("=")
if self.env.has_key(name):
continue
if not val:
continue
self.env[name] = val
def query(device=None, volinfo=True):
"""query udev database and return device(s) information
if no device is specified, all devices will be returned
optionally query volume info (vol_id) on disk devices
"""
if device:
cmd = "udevadm info --query all --name %s" % device
else:
cmd = "udevadm info --export-db"
devices = []
for s in getoutput(cmd + " 2>/dev/null").split('\n\n'):
devices.append(Device(s, volinfo))
return devices
def _disk_devices():
"""debug/test method to print disk devices"""
devices = query()
for dev in devices:
if dev.env.has_key('DEVTYPE') and dev.env['DEVTYPE'] == 'disk':
print '/dev/' + dev.name
attrs = dev.env.keys()
attrs.sort()
column_len = max([ len(attr) + 1 for attr in attrs ])
for attr in attrs:
name = attr + ":"
print " %s %s" % (name.ljust(column_len), dev.env[attr])
print
def main():
_disk_devices() #used in debugging/testing
if __name__ == '__main__':
main()