Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import gobject |
| 5 | import dbus |
| 6 | import dbus.service |
| 7 | import dbus.mainloop.glib |
| 8 | import tftpy |
| 9 | |
| 10 | |
| 11 | |
| 12 | DBUS_NAME = 'org.openbmc.managers.Download' |
| 13 | OBJ_NAME = '/org/openbmc/managers/Download' |
| 14 | TFTP_PORT = 69 |
| 15 | DOWNLOAD_DIR = '/tmp' |
| 16 | |
| 17 | class DownloadManagerObject(dbus.service.Object): |
| 18 | def __init__(self,bus,name): |
| 19 | dbus.service.Object.__init__(self,bus,name) |
| 20 | bus = dbus.SessionBus() |
| 21 | bus.add_signal_receiver(self.DownloadHandler, |
| 22 | dbus_interface = "org.openbmc.Flash", signal_name = "Download") |
| 23 | |
| 24 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 25 | def DownloadComplete(self,outfile): |
| 26 | print "Download Complete: "+outfile |
| 27 | return outfile |
| 28 | |
| 29 | @dbus.service.signal(DBUS_NAME) |
| 30 | def DownloadError(self): |
| 31 | pass |
| 32 | |
| 33 | def DownloadHandler(self,url,filename): |
| 34 | try: |
| 35 | filename = str(filename) |
| 36 | client = tftpy.TftpClient(url, TFTP_PORT) |
| 37 | print "Downloading: "+filename+" from "+url |
| 38 | outfile = DOWNLOAD_DIR+"/"+filename |
| 39 | client.download(filename,outfile) |
| 40 | self.DownloadComplete(outfile) |
| 41 | |
| 42 | except Exception as e: |
| 43 | print "ERROR DownloadManager: "+str(e) |
| 44 | self.DownloadError() |
| 45 | |
| 46 | |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 50 | |
| 51 | bus = dbus.SessionBus() |
| 52 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 53 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 54 | mainloop = gobject.MainLoop() |
| 55 | |
| 56 | print "Running Download Manager" |
| 57 | mainloop.run() |
| 58 | |