Port to python 3
Port code to python 3 yet remain backward compatible to python 2.
This port is in preparation for yocto 2.4 upgrade.
Partially resolves openbmc/openbmc#2920
Change-Id: If174ce9444bde4e21fb086764f4def017e423201
Signed-off-by: CamVan Nguyen <ctnguyen@us.ibm.com>
diff --git a/pychassisctl/chassis_control.py b/pychassisctl/chassis_control.py
index 0abd422..2a6f175 100644
--- a/pychassisctl/chassis_control.py
+++ b/pychassisctl/chassis_control.py
@@ -1,6 +1,10 @@
#!/usr/bin/env python
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -27,9 +31,9 @@
if (len(data) == 32):
uuid = data
else:
- print "ERROR: UUID is not formatted correctly: " + data
+ print("ERROR: UUID is not formatted correctly: " + data)
except Exception:
- print "ERROR: Unable to open uuid file: " + MACHINE_ID
+ print("ERROR: Unable to open uuid file: " + MACHINE_ID)
return uuid
@@ -86,7 +90,7 @@
@dbus.service.method(DBUS_NAME,
in_signature='', out_signature='')
def powerOn(self):
- print "Turn on power and boot"
+ print("Turn on power and boot")
self.Set(DBUS_NAME, "reboot", 0)
intf = self.getInterface('systemd')
f = getattr(intf, 'StartUnit')
@@ -96,7 +100,7 @@
@dbus.service.method(DBUS_NAME,
in_signature='', out_signature='')
def powerOff(self):
- print "Turn off power"
+ print("Turn off power")
intf = self.getInterface('systemd')
f = getattr(intf, 'StartUnit')
f.call_async('obmc-chassis-hard-poweroff@0.target', 'replace')
@@ -105,7 +109,7 @@
@dbus.service.method(DBUS_NAME,
in_signature='', out_signature='')
def softPowerOff(self):
- print "Soft off power"
+ print("Soft off power")
intf = self.getInterface('systemd')
f = getattr(intf, 'StartUnit')
f.call_async('obmc-host-shutdown@0.target', 'replace')
@@ -114,7 +118,7 @@
@dbus.service.method(DBUS_NAME,
in_signature='', out_signature='')
def reboot(self):
- print "Rebooting"
+ print("Rebooting")
if self.getPowerState() == POWER_OFF:
self.powerOn()
else:
@@ -125,7 +129,7 @@
@dbus.service.method(DBUS_NAME,
in_signature='', out_signature='')
def softReboot(self):
- print "Soft Rebooting"
+ print("Soft Rebooting")
if self.getPowerState() == POWER_OFF:
self.powerOn()
else:
@@ -158,14 +162,14 @@
self.softPowerOff()
def long_power_button_signal_handler(self):
- print "Long-press button, hard power off"
+ print("Long-press button, hard power off")
self.powerOff()
def softreset_button_signal_handler(self):
self.softReboot()
def host_watchdog_signal_handler(self):
- print "Watchdog Error, Going to quiesce"
+ print("Watchdog Error, Going to quiesce")
self.quiesce()
@@ -179,7 +183,7 @@
obj.unmask_signals()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Running ChassisControlService"
+ print("Running ChassisControlService")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/pydownloadmgr/download_manager.py b/pydownloadmgr/download_manager.py
index 7ba80ca..7fb600f 100644
--- a/pydownloadmgr/download_manager.py
+++ b/pydownloadmgr/download_manager.py
@@ -1,7 +1,11 @@
#!/usr/bin/env python
import os
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -30,7 +34,7 @@
@dbus.service.signal(DBUS_NAME, signature='ss')
def DownloadComplete(self, outfile, filename):
- print "Download Complete: "+outfile
+ print("Download Complete: "+outfile)
return outfile
@dbus.service.signal(DBUS_NAME, signature='s')
@@ -40,7 +44,7 @@
def TftpDownloadHandler(self, ip, filename, path=None):
try:
filename = str(filename)
- print "Downloading: "+filename+" from "+ip
+ print("Downloading: "+filename+" from "+ip)
outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
rc = subprocess.call(
["tftp", "-l", outfile, "-r", filename, "-g", ip])
@@ -50,7 +54,7 @@
self.DownloadError(filename)
except Exception as e:
- print "ERROR DownloadManager: "+str(e)
+ print("ERROR DownloadManager: "+str(e))
self.DownloadError(filename)
# TODO: this needs to be deprecated.
@@ -58,7 +62,7 @@
def DownloadHandler(self, url, filename, path=None):
try:
filename = str(filename)
- print "Downloading: "+filename+" from "+url
+ print("Downloading: "+filename+" from "+url)
outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename)
subprocess.call(
["tftp", "-l", outfile, "-r", filename, "-g", url])
@@ -67,7 +71,7 @@
intf.update(outfile)
except Exception as e:
- print "ERROR DownloadManager: "+str(e)
+ print("ERROR DownloadManager: "+str(e))
obj = bus.get_object("org.openbmc.control.Flash", path)
intf = dbus.Interface(obj, "org.openbmc.Flash")
intf.error("Download Error: "+filename)
@@ -80,7 +84,7 @@
mainloop = gobject.MainLoop()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Running Download Manager"
+ print("Running Download Manager")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/pyflashbmc/bmc_update.py b/pyflashbmc/bmc_update.py
index c934e1a..8c5936e 100644
--- a/pyflashbmc/bmc_update.py
+++ b/pyflashbmc/bmc_update.py
@@ -1,6 +1,10 @@
#!/usr/bin/env python
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -98,7 +102,7 @@
if (filename != self.Get(DBUS_NAME, "filename")):
return
- print "Download complete. Updating..."
+ print("Download complete. Updating...")
self.Set(DBUS_NAME, "status", "Download Complete")
copy_files = {}
@@ -120,13 +124,13 @@
for f in tar.getnames():
files[f] = True
tar.close()
- for f in copy_files.keys():
+ for f in list(copy_files.keys()):
if f not in files:
raise Exception(
"ERROR: File not found in update archive: "+f)
except Exception as e:
- print e
+ print(str(e))
self.Set(DBUS_NAME, "status", "Unpack Error")
return
@@ -136,7 +140,7 @@
tar.close()
if self.Get(DBUS_NAME, "clear_persistent_files"):
- print "Removing persistent files"
+ print("Removing persistent files")
try:
os.unlink(UPDATE_PATH+"/whitelist")
except OSError as e:
@@ -159,11 +163,11 @@
raise
if self.Get(DBUS_NAME, "preserve_network_settings"):
- print "Preserving network settings"
+ print("Preserving network settings")
save_fw_env()
except Exception as e:
- print e
+ print(str(e))
self.Set(DBUS_NAME, "status", "Unpack Error")
self.Verify()
@@ -327,7 +331,7 @@
obj.unmask_signals()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Running Bmc Flash Control"
+ print("Running Bmc Flash Control")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/pyinventorymgr/inventory_items.py b/pyinventorymgr/inventory_items.py
index 17b5eab..168e3d6 100644
--- a/pyinventorymgr/inventory_items.py
+++ b/pyinventorymgr/inventory_items.py
@@ -2,7 +2,11 @@
import os
import sys
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -88,7 +92,7 @@
try:
inv = json.load(f)
except ValueError:
- print "Invalid JSON detected in " + INVENTORY_FILE
+ print("Invalid JSON detected in " + INVENTORY_FILE)
else:
FRUS = inv
else:
@@ -98,7 +102,7 @@
except ImportError:
pass
- for f in FRUS.keys():
+ for f in list(FRUS.keys()):
import obmc.inventory
obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
obj = InventoryItem(bus, obj_path, FRUS[f])
@@ -112,7 +116,7 @@
obj_parent.unmask_signals()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Running Inventory Manager"
+ print("Running Inventory Manager")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/pysensormgr/sensor_manager2.py b/pysensormgr/sensor_manager2.py
index cfd8868..f6a7dbf 100644
--- a/pysensormgr/sensor_manager2.py
+++ b/pysensormgr/sensor_manager2.py
@@ -1,6 +1,10 @@
#!/usr/bin/env python
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -27,7 +31,7 @@
DBUS_NAME, in_signature='ss', out_signature='')
def register(self, object_name, obj_path):
if obj_path not in self.objects:
- print "Register: "+object_name+" : "+obj_path
+ print("Register: "+object_name+" : "+obj_path)
sensor = eval('obmc.sensors.'+object_name+'(bus,obj_path)')
self.add(obj_path, sensor)
@@ -35,14 +39,14 @@
DBUS_NAME, in_signature='s', out_signature='')
def delete(self, obj_path):
if obj_path in self.objects:
- print "Delete: "+obj_path
+ print("Delete: "+obj_path)
self.remove(obj_path)
def SensorChange(self, value, path=None):
if path in self.objects:
self.objects[path].setValue(value)
else:
- print "ERROR: Sensor not found: "+path
+ print("ERROR: Sensor not found: "+path)
if __name__ == '__main__':
@@ -53,7 +57,7 @@
# instantiate non-polling sensors
# these don't need to be in separate process
if has_system:
- for (id, the_sensor) in System.MISC_SENSORS.items():
+ for (id, the_sensor) in list(System.MISC_SENSORS.items()):
sensor_class = the_sensor['class']
obj_path = System.ID_LOOKUP['SENSOR'][id]
sensor_obj = getattr(obmc.sensors, sensor_class)(bus, obj_path)
@@ -65,7 +69,7 @@
root_sensor.unmask_signals()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Starting sensor manager"
+ print("Starting sensor manager")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/pysystemmgr/obmc/system/__init__.py b/pysystemmgr/obmc/system/__init__.py
index 03dc235..7bcea9e 100644
--- a/pysystemmgr/obmc/system/__init__.py
+++ b/pysystemmgr/obmc/system/__init__.py
@@ -18,8 +18,8 @@
def convertGpio(name):
- offset = int(filter(str.isdigit, name))
- port = filter(str.isalpha, name.upper())
+ offset = int(''.join(list(filter(str.isdigit, name))))
+ port = list(filter(str.isalpha, name.upper()))
a = ord(port[-1]) - ord('A')
if len(port) > 1:
a += 26
diff --git a/pysystemmgr/system_manager.py b/pysystemmgr/system_manager.py
index 77d36a3..cd13f7b 100644
--- a/pysystemmgr/system_manager.py
+++ b/pysystemmgr/system_manager.py
@@ -1,6 +1,10 @@
#!/usr/bin/env python
-import gobject
+# TODO: openbmc/openbmc#2994 remove python 2 support
+try: # python 2
+ import gobject
+except ImportError: # python 3
+ from gi.repository import GObject as gobject
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -21,7 +25,7 @@
object_path=obj_name)
self.bus = bus
- print "SystemManager Init Done"
+ print("SystemManager Init Done")
@dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
def gpioInit(self, name):
@@ -31,7 +35,7 @@
if name not in System.GPIO_CONFIG:
# TODO: Better error handling
msg = "ERROR: "+name+" not found in GPIO config table"
- print msg
+ print(msg)
raise Exception(msg)
else:
@@ -44,7 +48,7 @@
gpio_num = obmc.system.convertGpio(gpio['gpio_pin'])
else:
msg = "ERROR: SystemManager - GPIO lookup failed for "+name
- print msg
+ print(msg)
raise Exception(msg)
if (gpio_num != -1):
@@ -69,7 +73,7 @@
r = [power_good_in, latch_out, power_up_outs, reset_outs,
pci_reset_outs, fsi_data, fsi_clk, fsi_enable, cronus_sel,
optionals]
- print "Power GPIO config: " + str(r)
+ print("Power GPIO config: " + str(r))
return r
@@ -81,7 +85,7 @@
obj.unmask_signals()
name = dbus.service.BusName(DBUS_NAME, bus)
- print "Running SystemManager"
+ print("Running SystemManager")
mainloop.run()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4