Created
May 20, 2022 13:59
-
-
Save Cheaterman/83e237a556a0a61854bcd1ce214f7a06 to your computer and use it in GitHub Desktop.
/tmp/testudisks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
from dbus import SystemBus, Interface | |
from dbus.mainloop.glib import DBusGMainLoop, threads_init | |
from gi.repository import GLib | |
UDISKS2_SERVICE = 'org.freedesktop.UDisks2' | |
BLOCK_INTERFACE = f'{UDISKS2_SERVICE}.Block' | |
FILESYSTEM_INTERFACE = f'{UDISKS2_SERVICE}.Filesystem' | |
bus = SystemBus(mainloop=DBusGMainLoop()) | |
def on_interface_added(path, interfaces): | |
if FILESYSTEM_INTERFACE not in interfaces: | |
return | |
device = bytes(interfaces[BLOCK_INTERFACE]['Device']).decode('utf8') | |
print('Abount to mount block device:', device) | |
filesystem = Interface( | |
bus.get_object(UDISKS2_SERVICE, path), | |
FILESYSTEM_INTERFACE | |
) | |
mountpoint = filesystem.Mount({}) | |
print('Mounted block device at:', mountpoint) | |
def dbus_thread(): | |
bus.add_signal_receiver(on_interface_added, 'InterfacesAdded') | |
loop = GLib.MainLoop() | |
loop.run() | |
threads_init() | |
threading.Thread( | |
target=dbus_thread, | |
daemon=True, | |
).start() | |
while True: | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment