classes-systemd: General refactoring

Take the more idiomatic approach of iterating on
SYSTEMD_PACKAGES to find service files.

The OBMC_SYSTEMD_SERVICE variable isn't meant to be used directly by
recipes.  Rename it to something that seems less public.

Change-Id: If031937d4716b9c7e6ac1be506457e41109b69c1
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
index f882bfc..3df5ee4 100644
--- a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
+++ b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
@@ -8,41 +8,41 @@
 #
 # Alternatively this class can just be inherited and
 # ${PN}.service will be added to the main package.
+
+inherit obmc-phosphor-utils
 inherit systemd
 
+_INSTALL_SD_UNITS=""
+
 
 python() {
+    def add_sd_unit(d, unit, pkg):
+        searchpaths = d.getVar('FILESPATH', True)
+        path = bb.utils.which(searchpaths, '%s' % unit)
+        if not os.path.isfile(path):
+            bb.fatal('Did not find unit file "%s"' % unit)
+        set_append(d, 'SRC_URI', 'file://%s' % unit)
+        set_append(d, 'FILES_%s' % pkg, '%s/%s' \
+            % (d.getVar('systemd_system_unitdir', True), unit))
+        set_append(d, '_INSTALL_SD_UNITS', '%s' % unit)
+
     pn = d.getVar('PN', True)
-    searchpaths = d.getVar('FILESPATH', True)
+    if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
+        d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
 
-    services = d.getVar('SYSTEMD_SERVICE_' + pn, True)
-
-    if services:
-        services = services.split()
-    else:
-        services = [pn + '.service']
-
-    for s in services:
-        file = s
-        path = bb.utils.which(searchpaths, file)
-        if os.path.isfile(path):
-            d.appendVar('SRC_URI', ' file://' + file)
-            d.appendVar("FILES_%s" %(pn), " %s/%s" \
-                % (d.getVar('systemd_system_unitdir', True), file))
-            d.appendVar('OBMC_SYSTEMD_SERVICES', ' ' + file)
-            if file not in (d.getVar('SYSTEMD_SERVICE_' + pn, True) or "").split():
-                d.appendVar('SYSTEMD_SERVICE_' + pn, ' ' + file)
-        else:
-            bb.error("Could not find service file: %s" % file)
+    for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
+        for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
+            add_sd_unit(d, unit, pkg)
 }
 
+
 do_install_append() {
         # install systemd service/socket/template files
-        if [ "${OBMC_SYSTEMD_SERVICES}" ]; then
+        [ -z "${_INSTALL_SD_UNITS}" ] || \
                 install -d ${D}${systemd_system_unitdir}
-        fi
-        for s in ${OBMC_SYSTEMD_SERVICES}; do
-                install -m 0644 ${WORKDIR}/$s ${D}${systemd_system_unitdir}
+        for s in ${_INSTALL_SD_UNITS}; do
+                install -m 0644 ${WORKDIR}/$s \
+                        ${D}${systemd_system_unitdir}/$s
                 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
                         -e 's,@BINDIR@,${bindir},g' \
                         -e 's,@SBINDIR@,${sbindir},g' \
diff --git a/meta-phosphor/classes/obmc-phosphor-utils.bbclass b/meta-phosphor/classes/obmc-phosphor-utils.bbclass
index 44ca7ad..036af9b 100644
--- a/meta-phosphor/classes/obmc-phosphor-utils.bbclass
+++ b/meta-phosphor/classes/obmc-phosphor-utils.bbclass
@@ -15,3 +15,15 @@
     return value if df_enabled(feature, value, d) \
         and mf_enabled(feature, value, d) \
             else ""
+
+
+def set_append(d, var, val, sep=' '):
+    values = (d.getVar(var, True) or '').split(sep)
+    if filter(bool, values):
+        d.appendVar(var, '%s%s' %(sep, val))
+    else:
+        d.setVar(var, val)
+
+
+def listvar_to_list(d, list_var, sep=' '):
+    return filter(bool, (d.getVar(list_var, True) or '').split(sep))