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) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 20 | bus.add_signal_receiver(self.DownloadHandler, |
| 21 | dbus_interface = "org.openbmc.Flash", signal_name = "Download") |
| 22 | |
| 23 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 24 | def DownloadComplete(self,outfile): |
| 25 | print "Download Complete: "+outfile |
| 26 | return outfile |
| 27 | |
| 28 | @dbus.service.signal(DBUS_NAME) |
| 29 | def DownloadError(self): |
| 30 | pass |
| 31 | |
| 32 | def DownloadHandler(self,url,filename): |
| 33 | try: |
| 34 | filename = str(filename) |
| 35 | client = tftpy.TftpClient(url, TFTP_PORT) |
| 36 | print "Downloading: "+filename+" from "+url |
| 37 | outfile = DOWNLOAD_DIR+"/"+filename |
| 38 | client.download(filename,outfile) |
| 39 | self.DownloadComplete(outfile) |
| 40 | |
| 41 | except Exception as e: |
| 42 | print "ERROR DownloadManager: "+str(e) |
| 43 | self.DownloadError() |
| 44 | |
| 45 | |
| 46 | |
| 47 | if __name__ == '__main__': |
| 48 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 49 | bus = Openbmc.getDBus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 50 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 51 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 52 | mainloop = gobject.MainLoop() |
| 53 | |
| 54 | print "Running Download Manager" |
| 55 | mainloop.run() |
| 56 | |