classes-systemd: general refactoring

Factor out some code so it can be used elsewhere.

Change-Id: Ifbb2d7553c2c21868b5bc5b515b6f03f2c0882c0
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/classes/obmc-phosphor-systemd.bbclass b/classes/obmc-phosphor-systemd.bbclass
index c1eb3c9..85089f8 100644
--- a/classes/obmc-phosphor-systemd.bbclass
+++ b/classes/obmc-phosphor-systemd.bbclass
@@ -104,25 +104,28 @@
                 'have a restart policy defined.' % unit.name)
 
 
-    def add_sd_unit(d, unit, pkg):
-        name = unit.name
-        unit_dir = d.getVar('systemd_system_unitdir', True)
-        set_append(d, 'SRC_URI', 'file://%s' % name)
-        set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name))
-        set_append(d, '_INSTALL_SD_UNITS', name)
-        set_append(d, '_MAKE_SUBS', '%s' % name)
+    def add_default_subs(d, file):
+        set_append(d, '_MAKE_SUBS', '%s' % file)
 
         for x in [
                 'base_bindir',
                 'bindir',
                 'sbindir',
                 'SYSTEMD_DEFAULT_TARGET' ]:
-            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % name,
+            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file,
                 '%s:%s' % (x, d.getVar(x, True)))
 
 
-    def add_sd_user(d, unit, pkg):
+    def add_sd_unit(d, unit, pkg):
         name = unit.name
+        unit_dir = d.getVar('systemd_system_unitdir', True)
+        set_append(d, 'SRC_URI', 'file://%s' % name)
+        set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name))
+        set_append(d, '_INSTALL_SD_UNITS', name)
+        add_default_subs(d, name)
+
+
+    def add_sd_user(d, file, pkg):
         opts = [
             '--system',
             '--home',
@@ -131,7 +134,7 @@
             '--shell /sbin/nologin',
             '--user-group']
 
-        var = 'SYSTEMD_USER_%s' % name
+        var = 'SYSTEMD_USER_%s' % file
         user = listvar_to_list(d, var)
         if len(user) is 0:
             var = 'SYSTEMD_USER_%s' % pkg
@@ -141,7 +144,7 @@
                 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
 
             user = user[0]
-            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % name,
+            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file,
                 'USER:%s' % user)
             if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
                 set_append(
@@ -168,28 +171,32 @@
         for unit in tmpl + svc:
             check_sd_unit(d, unit)
             add_sd_unit(d, unit, pkg)
-            add_sd_user(d, unit, pkg)
+            add_sd_user(d, unit.name, pkg)
 }
 
 
 python systemd_do_postinst() {
-    for f in listvar_to_list(d, '_MAKE_SUBS'):
-        subs = dict([ x.split(':') for x in
-            listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)])
-        if not subs:
-            continue
+    def make_subs(d):
+        for f in listvar_to_list(d, '_MAKE_SUBS'):
+            subs = dict([ x.split(':') for x in
+                listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)])
+            if not subs:
+                continue
 
-        path = d.getVar('D', True)
-        path += d.getVar('systemd_system_unitdir', True)
-        path += '/%s' % f
-        with open(path, 'r') as fd:
-            content = fd.read()
-        with open(path, 'w+') as fd:
-            try:
-                fd.write(content.format(**subs))
-            except KeyError as e:
-                bb.fatal('No substitution found for %s in '
-                    'file \'%s\'' % (e, f))
+            path = d.getVar('D', True)
+            path += d.getVar('systemd_system_unitdir', True)
+            path += '/%s' % f
+            with open(path, 'r') as fd:
+                content = fd.read()
+            with open(path, 'w+') as fd:
+                try:
+                    fd.write(content.format(**subs))
+                except KeyError as e:
+                    bb.fatal('No substitution found for %s in '
+                        'file \'%s\'' % (e, f))
+
+
+    make_subs(d)
 }