1
0
Fork 0
mirror of https://github.com/dbarzin/pandora-box.git synced 2025-07-19 21:39:40 +02:00
pandora-box/tests/detect-usb.py

35 lines
917 B
Python
Raw Normal View History

2022-06-11 16:26:40 +02:00
#!/usr/bin/python3
import pyudev
import psutil
import time
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block')
# enumerate at device connection
for device in iter(monitor.poll, None):
if 'ID_FS_TYPE' in device:
if device.action == 'add':
if device.device_node[5:7] == 'sd':
print("New device {}".format(device.device_node))
# loop until device is mounted
found=False
loop=0
while (not found) and (loop<10):
# need to sleep before devide is mounted
time.sleep(1)
for partition in psutil.disk_partitions():
if partition.device == device.device_node:
print("Mounted at {}".format(partition.mountpoint))
found=True
loop+=1
if device.action == 'remove':
if device.device_node[5:7] == 'sd':
print('Device removed')