forked from boyska/freepto-usb-utils
-
Notifications
You must be signed in to change notification settings - Fork 3
/
usb-disk-utility
executable file
·45 lines (38 loc) · 1.34 KB
/
usb-disk-utility
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
#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE
from pyudev import Context
import parted
context = Context()
def get_usb_disks():
devices = []
for device in context.list_devices(DEVTYPE='disk'):
if device.get('ID_BUS') == 'usb':
devices.append(device)
return devices
def get_partitions(dev_node):
partitions = []
for device in context.list_devices(DEVTYPE='disk'):
if device.device_node == dev_node:
for partition in list(device.children):
partitions.append(partition)
return partitions
def get_size(dev_node):
return parted.Device(dev_node).getSize('GB')
if len(sys.argv) > 1:
cmd = sys.argv[1]
if cmd == 'get_disks':
for disk in get_usb_disks():
vendor = disk.get('ID_VENDOR')
model = disk.get('ID_MODEL')
size = get_size(disk.device_node)
print disk.device_node, vendor, model, size
elif cmd == 'get_partitions':
disk = sys.argv[2]
for d in get_usb_disks():
if d.device_node == disk:
for partition in get_partitions(disk):
size = get_size(partition.device_node)
fs_type = partition.get('ID_FS_TYPE')
print partition.device_node, fs_type, size
# vim: set ts=4 sw=4 et fdm=marker: