pytools: Remove dependency to obmc.system

The system manager (obmc.system) is being deprecated and
the GPIOs has moved out of D-Bus (see openbmc/openbmc#3332).
The pytools still depend on the convertGpio function provided
by system manager, so copy that function into the tools that
use it to remove the dependency.

Tested: obmcutil power functions still work, and gpioutil
-l and -p work the same.

Change-Id: Ia09f95312438040908eb15f93ff6d525665f5ab3
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/pytools/obmcutil b/pytools/obmcutil
index 0a0d8d1..0aa40a9 100644
--- a/pytools/obmcutil
+++ b/pytools/obmcutil
@@ -10,10 +10,11 @@
 import os
 import signal
 import time
+from glob import glob
+from os.path import join
 from subprocess import Popen
 
 import obmc_system_config
-import obmc.system
 
 descriptors = {
     'power': {
@@ -83,6 +84,32 @@
 
 GPIO_DEFS_FILE = '/etc/default/obmc/gpio/gpio_defs.json'
 
+
+def find_gpio_base(path="/sys/class/gpio/"):
+    pattern = "gpiochip*"
+    for gc in glob(join(path, pattern)):
+        with open(join(gc, "label")) as f:
+            label = f.readline().strip()
+        if label == "1e780000.gpio":
+            with open(join(gc, "base")) as f:
+                return int(f.readline().strip())
+    # trigger a file not found exception
+    open(join(path, "gpiochip"))
+
+
+GPIO_BASE = find_gpio_base()
+
+
+def convertGpio(name):
+    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
+    base = a * 8 + GPIO_BASE
+    return base + offset
+
+
 def run_set_property(dbus_bus, dbus_iface, descriptor, args):
     mainloop = gobject.MainLoop()
 
@@ -186,7 +213,7 @@
     return True
 
 def gpio_set_value(gpio_name, active_low, asserted):
-    gpio_id = obmc.system.convertGpio(gpio_name)
+    gpio_id = convertGpio(gpio_name)
     gpio_value_path = "/sys/class/gpio/gpio{}/value".format(gpio_id)
 
     with open(gpio_value_path, 'w') as gpio: