Merge hardcoded sensor yamls with generated sensor.yaml

This change will enable adding sensors which is not part
of the MRW to the MRW bases systems.

Change-Id: I0d7d2ff94a668ae77bb8c906e85ef6280dc49830
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/meta-phosphor/common/recipes-phosphor/ipmi/phosphor-ipmi-host.bb b/meta-phosphor/common/recipes-phosphor/ipmi/phosphor-ipmi-host.bb
index f2fa39e..45cb2ee 100644
--- a/meta-phosphor/common/recipes-phosphor/ipmi/phosphor-ipmi-host.bb
+++ b/meta-phosphor/common/recipes-phosphor/ipmi/phosphor-ipmi-host.bb
@@ -52,6 +52,8 @@
 
 S = "${WORKDIR}/git"
 
+SRC_URI += "file://merge_yamls.py"
+
 HOSTIPMI_PROVIDER_LIBRARY += "libapphandler.so"
 HOSTIPMI_PROVIDER_LIBRARY += "libsysintfcmds.so"
 
@@ -70,3 +72,34 @@
 SOFT_TGTFMT = "obmc-host-shutdown@{0}.target"
 SOFT_FMT = "../${SOFT_SVC}:${SOFT_TGTFMT}.requires/${SOFT_SVC}"
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'SOFT_FMT', 'OBMC_HOST_INSTANCES')}"
+
+#Collect all hardcoded sensor yamls from different recipes and
+#merge all of them with sensor.yaml.
+python do_merge_sensors () {
+    import subprocess
+
+    # TODO: Perform the merge in a temporary directory?
+    workdir = d.getVar('WORKDIR', True)
+    nativedir = d.getVar('STAGING_DIR_NATIVE', True)
+    sensorsdir = d.getVar('sensor_datadir', True)
+    sensorsdir = sensorsdir[1:]
+    sensorsdir = os.path.join(nativedir, sensorsdir)
+    cmd = []
+    cmd.append(os.path.join(workdir, 'merge_yamls.py'))
+    cmd.append(os.path.join(sensorsdir, 'sensor.yaml'))
+
+    if os.stat(os.path.join(sensorsdir, 'sensor.yaml')).st_size == 0:
+        return
+    fetch = bb.fetch2.Fetch([], d)
+    override_urls = filter(lambda f: f.endswith('.hardcoded.yaml'), fetch.urls)
+    for url in override_urls:
+        bb.debug(2, 'Overriding with source: ' + url)
+        local_base = os.path.basename(fetch.localpath(url))
+        filename = os.path.join(workdir, local_base)
+        cmd.append(filename)
+
+    # Invoke the script and don't catch any resulting exception.
+    subprocess.check_call(cmd)
+}
+# python-pyyaml-native is installed by do_configure, so put this task after
+addtask merge_sensors after do_configure before do_compile