meta-phosphor: Reuse dreport utility function

install_dreport_user_script function is intended
to be reused in meta-openpower layer.

To enable this, the function is being moved
to a bbclass which would be inherited in the
openpower-debug-collector recipe in the
meta-openpower layer.

dreport.conf file is a variable, hence making it
an argument to the function, so that other layer
can give a custom dreport.conf as input.

Tested:
Verified that plugin scripts are getting installed
from openpower-debug-collector repository and
BMC Dump collection is successful.

Change-Id: I8d13bc7e381cd1b957d5770926fb712165a07185
Signed-off-by: Gopichand Paturi <gopichandpaturi@gmail.com>
diff --git a/meta-ibm/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend b/meta-ibm/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
index df135f4..68fbf60 100644
--- a/meta-ibm/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
+++ b/meta-ibm/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
@@ -23,7 +23,7 @@
     op_plugins = os.listdir(source_path)
     for op_plugin in op_plugins:
         op_plugin_name = os.path.join(source_path, op_plugin)
-        install_dreport_user_script(op_plugin_name, d)
+        install_dreport_user_script("dreport.conf", op_plugin_name, d)
 }
 
 #Install dump header script from dreport/ibm.d to dreport/include.d
diff --git a/meta-openpower/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend b/meta-openpower/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
index 3f505c3..c8bccd1 100644
--- a/meta-openpower/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
+++ b/meta-openpower/recipes-phosphor/dump/phosphor-debug-collector_%.bbappend
@@ -11,7 +11,7 @@
     op_plugins = os.listdir(source_path)
     for op_plugin in op_plugins:
         op_plugin_name = os.path.join(source_path, op_plugin)
-        install_dreport_user_script(op_plugin_name, d)
+        install_dreport_user_script("dreport.conf", op_plugin_name, d)
 }
 
 DEBUG_COLLECTOR_INSTALL_POSTFUNCS ?= ""
diff --git a/meta-phosphor/classes/phosphor-debug-collector.bbclass b/meta-phosphor/classes/phosphor-debug-collector.bbclass
index d5d49a2..ca1a003 100644
--- a/meta-phosphor/classes/phosphor-debug-collector.bbclass
+++ b/meta-phosphor/classes/phosphor-debug-collector.bbclass
@@ -1,5 +1,67 @@
-bmc_dump_path="/var/lib/phosphor-debug-collector/dumps"
+bmc_dump_path = "/var/lib/phosphor-debug-collector/dumps"
 dreport_plugin_dir = "${datadir}/dreport.d/plugins.d"
 dreport_include_dir = "${datadir}/dreport.d/include.d"
 dreport_conf_dir = "${datadir}/dreport.d/conf.d"
 dreport_dir = "${datadir}/dreport.d/"
+
+# Make the links for a single user plugin script
+# Create user directories based on the dump type value in the config section
+# Create softlinks for the base scripts in the user directories
+def install_dreport_user_script(dreport_conf, script_path, d):
+    import re
+    import configparser
+
+    #Set variables
+    config = ("config:")
+    section = "DumpType"
+
+    #Read the user types from the dreport_conf file
+    configure = configparser.ConfigParser()
+    conf_dir  = d.getVar('D', True) + d.getVar('dreport_conf_dir', True)
+    confsource = os.path.join(conf_dir, dreport_conf)
+    configure.read(confsource)
+
+    #Extract the script name, and open the user script file
+    dreport_dir = d.getVar('D', True) + d.getVar('dreport_dir', True)
+    script = os.path.basename(script_path)
+    file = open(script_path, "r")
+
+    #softlink to the script
+    srclink = os.path.join(d.getVar('dreport_plugin_dir', True), script)
+
+    for line in file:
+       if not config in line:
+           continue
+
+       revalue = re.search('[0-9]+.[0-9]+', line)
+       if not revalue:
+           bb.warn("Invalid format for config value =%s" % line)
+           continue
+
+       #Regex search to identify which directories get softlinks to the script
+       parse_value = revalue.group(0)
+       config_values = re.split(r'\W+', parse_value, 1)
+       if(len(config_values) != 2):
+           bb.warn("Invalid config value=%s" % parse_value)
+           break;
+       priority = config_values[1]
+       types = [int(d) for d in str(config_values[0])]
+
+       #For every dump type identified from 'types',create softlink to script
+       for type in types:
+           if not configure.has_option(section, str(type)):
+               bb.warn("Invalid dump type id =%s" % (str(type)))
+               continue
+
+           #create directories based on the usertype
+           typestr = configure.get(section, str(type))
+           destdir = os.path.join(dreport_dir, ("pl_" + typestr + ".d"))
+           if not os.path.exists(destdir):
+               os.makedirs(destdir)
+
+           #Create softlinks to the user script in the directories
+           linkname = "E" + priority + script
+           destlink = os.path.join(destdir, linkname)
+           os.symlink(srclink, destlink)
+
+    file.close()
diff --git a/meta-phosphor/recipes-phosphor/dump/phosphor-debug-collector_git.bb b/meta-phosphor/recipes-phosphor/dump/phosphor-debug-collector_git.bb
index c28aa3b..c16dd87 100644
--- a/meta-phosphor/recipes-phosphor/dump/phosphor-debug-collector_git.bb
+++ b/meta-phosphor/recipes-phosphor/dump/phosphor-debug-collector_git.bb
@@ -126,49 +126,7 @@
     install -m 0755 ${S}/tools/dreport.d/include.d/* \
                 ${D}${dreport_include_dir}/
 }
-# Make the links for a single user plugin script
-# Create user directories based on the dump type value in the config section
-# Create softlinks for the base scripts in the user directories
-def install_dreport_user_script(script_path, d):
-    import re
-    import configparser
-    #Read the user types from the dreport.conf file
-    configure = configparser.ConfigParser()
-    conf_dir  = d.getVar('D', True) + d.getVar('dreport_conf_dir', True)
-    confsource = os.path.join(conf_dir, "dreport.conf")
-    configure.read(confsource)
-    config = ("config:")
-    section = "DumpType"
-    dreport_dir = d.getVar('D', True) + d.getVar('dreport_dir', True)
-    script = os.path.basename(script_path)
-    srclink = os.path.join(d.getVar('dreport_plugin_dir', True), script)
-    file = open(script_path, "r")
-    for line in file:
-        if not config in line:
-            continue
-        revalue = re.search('[0-9]+.[0-9]+', line)
-        if not revalue:
-            bb.warn("Invalid format for config value =%s" % line)
-            continue
-        parse_value = revalue.group(0)
-        config_values = re.split(r'\W+', parse_value, 1)
-        if(len(config_values) != 2):
-            bb.warn("Invalid config value=%s" % parse_value)
-            break;
-        priority = config_values[1]
-        types = [int(d) for d in str(config_values[0])]
-        for type in types:
-            if not configure.has_option(section, str(type)):
-                bb.warn("Invalid dump type id =%s" % (str(type)))
-                continue
-            typestr = configure.get(section, str(type))
-            destdir = os.path.join(dreport_dir, ("pl_" + typestr + ".d"))
-            if not os.path.exists(destdir):
-                os.makedirs(destdir)
-            linkname = "E" + priority + script
-            destlink = os.path.join(destdir, linkname)
-            os.symlink(srclink, destlink)
-    file.close()
+
 #Make the links for all the plugins
 python install_dreport_user_scripts() {
     source = d.getVar('S', True)
@@ -176,5 +134,5 @@
     scripts = os.listdir(source_path)
     for script in scripts:
         srcname = os.path.join(source_path, script)
-        install_dreport_user_script(srcname, d)
+        install_dreport_user_script("dreport.conf", srcname, d)
 }