我试图用Python编写一个系统来检查USB驱动器上是否存在一个文件,如果没有驱动器,它将等待dbus系统注册一个新设备,然后再次检查。在
我要检查一下mtab。我要检查一下文件是否存在。我有dbus位在工作,但我目前正在努力解决的是,当驱动器注册时,它会从dbus位中脱离出来,这样我就可以检查mtab,然后检查文件。在
我希望这是有道理的。在
我将为糟糕的编码风格道歉-我只是刚刚开始。在
到目前为止,我得到的是:
#!/usr/bin/env python
import string, time, os, dbus, gobject, sys
from dbus.mainloop.glib import DBusGMainLoop
def device_added_callback(device):
print ("Block device added. Check if it is partitioned")
usbdev = "".join(device.split("/")[5:6])
if usbdev.endswith("1") == 1:
print ("Block device is partitioned. Waiting for it to be mounted.")
# This is where I need to break out of the USB bit so I can check mtab and then check the file exits.
def waitforusb():
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
proxy = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
iface = dbus.Interface(proxy, "org.freedesktop.UDisks")
devices = iface.get_dbus_method('EnumerateDevices')()
usbdev = iface.connect_to_signal('DeviceAdded', device_added_callback)
mainloop = gobject.MainLoop()
mainloop.run()
return usbdev
def checkusbispresent():
f = open("/etc/mtab")
lines = f.readlines()
f.close()
for line in lines:
mtpt = "".join(line.split()[1:2])
isthere = mtpt.find("media")
if isthere == 1:
return mtpt
def checkserialfile(mtpt):
_serialfile=mtpt+"/serial.lic"
if ( not os.path.isfile(_serialfile)):
print("Error: serial file not found, please download it now")
else:
print("Serial file found, attempting validation... ")
usbdrive = checkusbispresent()
if ( usbdrive is not None ):
checkserialfile(usbdrive)
else:
print ("USB drive is not present. Please add it now.")
added = waitforusb()
print added
明白了!在
我非常怀疑这是最优雅的解决方案,但我会在以后的某个阶段攻击优雅。在
我把mainloop设为全局的,然后我可以从device\u added\u callback中访问它:
相关问题 更多 >
编程相关推荐