]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Collecting all unnecessary home dirs.
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 21 Mar 2017 16:24:37 +0000 (17:24 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 21 Mar 2017 16:24:37 +0000 (17:24 +0100)
pp_lib/test_home_app.py

index 830faa0c88dd5974b4606e99fc3068086231c3ed..b22c2e004cf058adedb922bd7f4bf83d850cd827 100644 (file)
@@ -18,6 +18,7 @@ import traceback
 import textwrap
 import pwd
 import copy
+import glob
 
 # Third party modules
 import six
@@ -31,7 +32,7 @@ from .common import pp, terminal_can_colors, to_bytes, to_bool
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.3.1'
+__version__ = '0.4.1'
 LOG = logging.getLogger(__name__)
 
 
@@ -81,6 +82,7 @@ class PpTestHomeApp(PpConfigApplication):
 
         self.exclude_dirs = []
         self.passwd_home_dirs = []
+        self.unnecessary_dirs = []
 
         description = textwrap.dedent('''\
             This scripts detects unnecessary home directories - without an
@@ -151,6 +153,7 @@ class PpTestHomeApp(PpConfigApplication):
 
         self.read_exclude_dirs()
         self.read_passwd_homes()
+        self.check_homes()
 
     # -------------------------------------------------------------------------
     def read_exclude_dirs(self):
@@ -244,6 +247,30 @@ class PpTestHomeApp(PpConfigApplication):
         if self.verbose > 2:
             LOG.debug("Home directories in passwd:\n{}".format(pp(self.passwd_home_dirs)))
 
+    # -------------------------------------------------------------------------
+    def check_homes(self):
+
+        LOG.info("Checking for unnecessary home directories ...")
+
+        glob_pattern = os.path.join(self.home_root_real, '*')
+        all_home_entries = glob.glob(glob_pattern)
+
+        for path in all_home_entries:
+            if not os.path.isdir(path):
+                continue
+            home_rel = os.sep + os.path.relpath(path, self.chroot_homedir)
+            if self.verbose > 2:
+                LOG.debug("Checking {p!r} ({h!r}) ...".format(
+                    p=path, h=home_rel))
+            if home_rel in self.passwd_home_dirs:
+                continue
+            if home_rel in self.exclude_dirs:
+                continue
+            LOG.debug("Marking {!r} as unnecessary.".format(home_rel))
+            self.unnecessary_dirs.append(home_rel)
+
+        self.unnecessary_dirs.sort(key=str.lower)
+
 # =============================================================================
 
 if __name__ == "__main__":