diff mbox series

[1/2] systemd.bbclass: Clean up empty parent directories

Message ID 20240820203533.4166641-1-pkj@axis.com
State Accepted, archived
Commit 73159c0bbc636a08934f47690885d75fd37b701a
Headers show
Series [1/2] systemd.bbclass: Clean up empty parent directories | expand

Commit Message

Peter Kjellerstedt Aug. 20, 2024, 8:35 p.m. UTC
Previously, rm_systemd_unitdir() would remove one parent directory of
${systemd_unitdir} if it was empty after removing ${systemd_unitdir}.
rm_sysvinit_initddir() would not remove any parent directory. Thus, if
the only directory created in /etc was /etc/init.d, an empty /etc would
remain after the cleanup and would be packaged.

Simplify rm_systemd_unitdir() and rm_sysvinit_initddir() by rewriting
them in shell, and use rmdir -p to remove all empty parent directories.

Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
---
 meta/classes-recipe/systemd.bbclass | 46 +++++++++++++----------------
 1 file changed, 21 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/systemd.bbclass b/meta/classes-recipe/systemd.bbclass
index 0f7e3b5a08..987b3fc0c7 100644
--- a/meta/classes-recipe/systemd.bbclass
+++ b/meta/classes-recipe/systemd.bbclass
@@ -208,33 +208,29 @@  python systemd_populate_packages() {
 
 PACKAGESPLITFUNCS =+ "systemd_populate_packages"
 
-python rm_systemd_unitdir (){
-    import shutil
-    if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
-        systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
-        if os.path.exists(systemd_unitdir):
-            shutil.rmtree(systemd_unitdir)
-        systemd_libdir = os.path.dirname(systemd_unitdir)
-        if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
-            os.rmdir(systemd_libdir)
+rm_systemd_unitdir() {
+	rm -rf ${D}${systemd_unitdir}
+	rmdir -p $(dirname ${D}${systemd_unitdir}) 2>/dev/null || :
+	# Make sure ${D} still exists since rmdir -p may have removed it in
+	# case ${systemd_unitdir} was the only directory present.
+	[ -d ${D} ] || mkdir -p ${D}
 }
 
-python rm_sysvinit_initddir (){
-    import shutil
-    sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
-
-    if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
-        not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
-        os.path.exists(sysv_initddir):
-        systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
+rm_sysvinit_initddir() {
+	local sysv_initddir=${INIT_D_DIR}
+	: ${sysv_initddir:=${sysconfdir}/init.d}
 
-        # If systemd_system_unitdir contains anything, delete sysv_initddir
-        if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
-            shutil.rmtree(sysv_initddir)
+	# If systemd_system_unitdir contains anything, delete sysv_initddir
+        if find ${D}${systemd_system_unitdir} -mindepth 1 -maxdepth 1 2>/dev/null | read; then
+		rm -rf ${D}$sysv_initddir
+		rmdir -p $(dirname ${D}$sysv_initddir) 2>/dev/null || :
+	fi
 }
 
-do_install[postfuncs] += "${RMINITDIR} "
-RMINITDIR:class-target = " rm_sysvinit_initddir rm_systemd_unitdir "
-RMINITDIR:class-nativesdk = " rm_sysvinit_initddir rm_systemd_unitdir "
-RMINITDIR = ""
-
+do_install[postfuncs] += "${RMINITDIR}"
+RMINITDIR = " \
+    ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '', 'rm_systemd_unitdir', d)} \
+    ${@'rm_sysvinit_initddir' if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
+                                 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) else ''} \
+"
+RMINITDIR:class-native = ""