Hello,
I am writing a program in python that detects inserted usb drives using dbus in linux and copies all of the files from it over to a dir on the computer. Here's my code:
import dbus
import gobject
import shutil
import os
import subprocess
import time
def move_files(pth):
p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["grep", pth], stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(["tr", "-s", '" "'], stdin=p2.stdout,
stdout=subprocess.PIPE)
p4 = subprocess.Popen(["cut", "-d ", "-f6"], stdin=p3.stdout, stdout=subprocess.PIPE)
path = str(p4.communicate()[0])
print path
class DeviceAddedListener:
def __init__(self):
self.bus = dbus.SystemBus()
self.hal_manager_obj = self.bus.get_object(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager")
self.hal_manager = dbus.Interface(self.hal_manager_obj,
"org.freedesktop.Hal.Manager")
self.hal_manager.connect_to_signal("DeviceAdded", self._filter)
def _filter(self, udi):
device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")
if device.QueryCapability("volume"):
return self.do_something(device)
def do_something(self, volume):
device_file = volume.GetProperty("block.device")
label = volume.GetProperty("volume.label")
fstype = volume.GetProperty("volume.fstype")
mounted = volume.GetProperty("volume.is_mounted")
mount_point = volume.GetProperty("volume.mount_point")
try:
size = volume.GetProperty("volume.size")
except:
size = 0
return move_files (device_file)
if __name__ == '__main__':
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
DeviceAddedListener()
loop.run()
It successfully detects the usb, but for some reason when I call move_files to print the usb path, it prints a blank string. Why is this? Thx in advance!