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 |
Norman James | 408920d | 2015-10-08 07:02:45 -0500 | [diff] [blame] | 9 | import Openbmc |
| 10 | if (len(sys.argv) < 2): |
| 11 | print "Usage: download_manager.py [system name]" |
| 12 | exit(1) |
| 13 | System = __import__(sys.argv[1]) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 14 | |
| 15 | |
| 16 | DBUS_NAME = 'org.openbmc.managers.Download' |
| 17 | OBJ_NAME = '/org/openbmc/managers/Download' |
| 18 | TFTP_PORT = 69 |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 19 | |
| 20 | class DownloadManagerObject(dbus.service.Object): |
| 21 | def __init__(self,bus,name): |
| 22 | dbus.service.Object.__init__(self,bus,name) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 23 | bus.add_signal_receiver(self.DownloadHandler, |
| 24 | dbus_interface = "org.openbmc.Flash", signal_name = "Download") |
| 25 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 26 | @dbus.service.signal(DBUS_NAME,signature='ss') |
| 27 | def DownloadComplete(self,outfile,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 28 | print "Download Complete: "+outfile |
| 29 | return outfile |
| 30 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 31 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 32 | def DownloadError(self,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 33 | pass |
| 34 | |
| 35 | def DownloadHandler(self,url,filename): |
| 36 | try: |
| 37 | filename = str(filename) |
| 38 | client = tftpy.TftpClient(url, TFTP_PORT) |
| 39 | print "Downloading: "+filename+" from "+url |
Norman James | 408920d | 2015-10-08 07:02:45 -0500 | [diff] [blame] | 40 | outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 41 | client.download(filename,outfile) |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 42 | self.DownloadComplete(outfile,filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 43 | |
| 44 | except Exception as e: |
| 45 | print "ERROR DownloadManager: "+str(e) |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 46 | self.DownloadError(filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 47 | |
| 48 | |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 52 | bus = Openbmc.getDBus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 53 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 54 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 55 | mainloop = gobject.MainLoop() |
| 56 | |
| 57 | print "Running Download Manager" |
| 58 | mainloop.run() |
| 59 | |