commit:     773270f3647a0d08d6468eccb141496f26e0a601
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Sat Apr 18 20:15:18 2020 +0000
Commit:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
CommitDate: Wed Apr 22 00:22:24 2020 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=773270f3

catalyst: Consolidate mount code into an OrderedDict

Keeping source and target together in a single data structure makes
things tidier than having

        self.mountmap    (map of mount name to source mount)
        self.targets_map (map of mount name to target mount)
        self.mounts      (list of mounts in the order they're mounted)

This also has another nice benefit: the the order that the mounts take
place is stored in one location and isn't affected by the order in which
the code chooses to enable or disable them (dependent on configuration
options).

Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>

 catalyst/base/stagebase.py | 94 ++++++++++++++++++++------------------------
 catalyst/defaults.py       | 97 +++++++++++++++++++++++++++++++++-------------
 2 files changed, 112 insertions(+), 79 deletions(-)

diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index f8c1611e..cc997688 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -11,8 +11,7 @@ from snakeoil.osutils import pjoin
 from DeComp.compress import CompressMap
 
 from catalyst import log
-from catalyst.defaults import (SOURCE_MOUNT_DEFAULTS, TARGET_MOUNT_DEFAULTS,
-                               PORT_LOGDIR_CLEAN)
+from catalyst.defaults import (MOUNT_DEFAULTS, PORT_LOGDIR_CLEAN)
 from catalyst.support import (CatalystError, file_locate, normpath,
                               cmd, read_makeconf, ismount, file_check)
 from catalyst.base.targetbase import TargetBase
@@ -188,46 +187,39 @@ class StageBase(TargetBase, ClearBase, GenBase):
             file_locate(self.settings, ["portage_confdir"], expand=0)
 
         # Setup our mount points.
-        # initialize our target mounts.
-        self.target_mounts = TARGET_MOUNT_DEFAULTS.copy()
-
-        self.mounts = ["proc", "dev", "portdir", "distdir", "port_tmpdir"]
-        # initialize our source mounts
-        self.mountmap = SOURCE_MOUNT_DEFAULTS.copy()
-        # update these from settings
-        self.mountmap["portdir"] = self.settings["portdir"]
-        self.mountmap["distdir"] = self.settings["distdir"]
-        self.target_mounts["portdir"] = normpath(self.settings["repo_basedir"] 
+
-                                                 "/" + 
self.settings["repo_name"])
-        self.target_mounts["distdir"] = self.settings["target_distdir"]
-        self.target_mounts["packagedir"] = self.settings["target_pkgdir"]
+        self.mount = MOUNT_DEFAULTS.copy()
+
+        self.mount['portdir']['source'] = self.settings['portdir']
+        self.mount['portdir']['target'] = 
normpath(self.settings["repo_basedir"]
+                                                   + "/" +
+                                                   self.settings["repo_name"])
+        self.mount['distdir']['source'] = self.settings['distdir']
+        self.mount["distdir"]['target'] = self.settings["target_distdir"]
+
         if "snapcache" not in self.settings["options"]:
-            self.mounts.remove("portdir")
-            self.mountmap["portdir"] = None
+            self.mount['portdir']['enable'] = False
         else:
-            self.mountmap["portdir"] = normpath("/".join([
-                self.settings["snapshot_cache_path"],
-                self.settings["repo_name"],
+            self.mount['portdir']['source'] = normpath("/".join([
+                self.settings['snapshot_cache_path'],
+                self.settings['repo_name']
             ]))
-        self.mounts.append("devpts")
-        self.mounts.append("shm")
-        self.mounts.append("run")
 
         # Configure any user specified options (either in catalyst.conf or on
         # the command line).
         if "pkgcache" in self.settings["options"]:
             self.set_pkgcache_path()
+            self.mount['pkgdir']['enable'] = True
+            self.mount['pkgdir']['source'] = self.settings['pkgcache_path']
+            self.mount['pkgdir']['target'] = self.settings["target_pkgdir"]
             log.info('Location of the package cache is %s',
                      self.settings['pkgcache_path'])
-            self.mounts.append("packagedir")
-            self.mountmap["packagedir"] = self.settings["pkgcache_path"]
 
         if "kerncache" in self.settings["options"]:
             self.set_kerncache_path()
+            self.mount['kerncache']['enable'] = True
+            self.mount['kerncache']['source'] = self.settings["kerncache_path"]
             log.info('Location of the kerncache is %s',
                      self.settings['kerncache_path'])
-            self.mounts.append("kerncache")
-            self.mountmap["kerncache"] = self.settings["kerncache_path"]
 
         if "ccache" in self.settings["options"]:
             if "CCACHE_DIR" in os.environ:
@@ -238,20 +230,19 @@ class StageBase(TargetBase, ClearBase, GenBase):
             if not os.path.isdir(ccdir):
                 raise CatalystError(
                     "Compiler cache support can't be enabled (can't find " + 
ccdir+")")
-            self.mounts.append("ccache")
-            self.mountmap["ccache"] = ccdir
-            # for the chroot:
-            self.env["CCACHE_DIR"] = self.target_mounts["ccache"]
+            self.mount['ccache']['enable'] = True
+            self.mount['ccache']['source'] = ccdir
+            self.env["CCACHE_DIR"] = self.mount['ccache']['target']
 
         if "icecream" in self.settings["options"]:
-            self.mounts.append("icecream")
-            self.mountmap["icecream"] = self.settings["icecream"]
-            self.env["PATH"] = self.target_mounts["icecream"] + \
+            self.mount['icecream']['enable'] = True
+            self.mount['icecream']['source'] = self.settings['icecream']
+            self.env["PATH"] = self.mount['icecream']['target'] + \
                 ":" + self.env["PATH"]
 
         if "port_logdir" in self.settings:
-            self.mounts.append("port_logdir")
-            self.mountmap["port_logdir"] = self.settings["port_logdir"]
+            self.mount['port_logdir']['enable'] = True
+            self.mount['port_logdir']['source'] = self.settings['port_logdir']
             self.env["PORT_LOGDIR"] = self.settings["port_logdir"]
             self.env["PORT_LOGDIR_CLEAN"] = PORT_LOGDIR_CLEAN
 
@@ -697,10 +688,10 @@ class StageBase(TargetBase, ClearBase, GenBase):
         if not os.path.exists(self.settings["chroot_path"]):
             return
 
-        log.debug('self.mounts = %s', self.mounts)
-        for x in self.mounts:
-            target = normpath(
-                self.settings["chroot_path"] + self.target_mounts[x])
+        log.debug('self.mount = %s', self.mount)
+        for x in [x for x in self.mount if self.mount[x]['enable']]:
+            target = normpath(self.settings['chroot_path'] +
+                              self.mount[x]['target'])
             log.debug('mount_safety_check() x = %s %s', x, target)
             if not os.path.exists(target):
                 continue
@@ -971,17 +962,17 @@ class StageBase(TargetBase, ClearBase, GenBase):
                         env=self.env)
 
     def bind(self):
-        for x in self.mounts:
+        for x in [x for x in self.mount if self.mount[x]['enable']]:
             log.debug('bind(); x = %s', x)
-            target = normpath(
-                self.settings["chroot_path"] + self.target_mounts[x])
+            target = normpath(self.settings['chroot_path'] +
+                              self.mount[x]['target'])
             ensure_dirs(target, mode=0o755)
 
-            if not os.path.exists(self.mountmap[x]):
-                if self.mountmap[x] not in ("maybe_tmpfs", "tmpfs", "shmfs"):
-                    ensure_dirs(self.mountmap[x], mode=0o755)
+            if not os.path.exists(self.mount[x]['source']):
+                if self.mount[x]['source'] not in ("maybe_tmpfs", "tmpfs", 
"shmfs"):
+                    ensure_dirs(self.mount[x]['source'], mode=0o755)
 
-            src = self.mountmap[x]
+            src = self.mount[x]['source']
             log.debug('bind(); src = %s', src)
             if "snapcache" in self.settings["options"] and x == "portdir":
                 self.snapcache_lock.read_lock()
@@ -1008,11 +999,10 @@ class StageBase(TargetBase, ClearBase, GenBase):
     def unbind(self):
         ouch = 0
         mypath = self.settings["chroot_path"]
-        myrevmounts = self.mounts[:]
-        myrevmounts.reverse()
-        # Unmount in reverse order for nested bind-mounts
-        for x in myrevmounts:
-            target = normpath(mypath + self.target_mounts[x])
+
+        # Unmount in reverse order
+        for x in [x for x in reversed(self.mount) if self.mount[x]['enable']]:
+            target = normpath(mypath + self.mount[x]['target'])
             if not os.path.exists(target):
                 log.notice('%s does not exist. Skipping', target)
                 continue

diff --git a/catalyst/defaults.py b/catalyst/defaults.py
index f292c211..e09d08e8 100644
--- a/catalyst/defaults.py
+++ b/catalyst/defaults.py
@@ -1,4 +1,6 @@
 
+from collections import OrderedDict
+
 from DeComp.definitions import DECOMPRESSOR_SEARCH_ORDER
 from DeComp.definitions import COMPRESSOR_PROGRAM_OPTIONS, XATTRS_OPTIONS
 from DeComp.definitions import DECOMPRESSOR_PROGRAM_OPTIONS, 
LIST_XATTRS_OPTIONS
@@ -68,7 +70,7 @@ confdefaults = {
     "port_conf": "/etc/portage",
     "make_conf": "%(port_conf)s/make.conf",
     "options": set(),
-    "packagedir": PKGDIR[:],
+    "pkgdir": PKGDIR[:],
     "portdir": PORTDIR[:],
     "port_tmpdir": "/var/tmp/portage",
     "PythonDir": "./catalyst",
@@ -89,32 +91,73 @@ DEFAULT_CONFIG_FILE = '/etc/catalyst/catalyst.conf'
 PORT_LOGDIR_CLEAN = \
     'find "${PORT_LOGDIR}" -type f ! -name "summary.log*" -mtime +30 -delete'
 
-TARGET_MOUNT_DEFAULTS = {
-    "ccache": "/var/tmp/ccache",
-    "dev": "/dev",
-    "devpts": "/dev/pts",
-    "distdir": DISTDIR[:],
-    "icecream": "/usr/lib/icecc/bin",
-    "kerncache": "/tmp/kerncache",
-    "packagedir": PKGDIR[:],
-    "portdir": PORTDIR[:],
-    "port_tmpdir": "/var/tmp/portage",
-    "port_logdir": "/var/log/portage",
-    "proc": "/proc",
-    "shm": "/dev/shm",
-    "run": "/run",
-}
-
-SOURCE_MOUNT_DEFAULTS = {
-    "dev": "/dev",
-    "devpts": "/dev/pts",
-    "distdir": DISTDIR[:],
-    "portdir": PORTDIR[:],
-    "port_tmpdir": "maybe_tmpfs",
-    "proc": "/proc",
-    "shm": "shmfs",
-    "run": "tmpfs",
-}
+MOUNT_DEFAULTS = OrderedDict([
+    ('proc', {
+        'enable': True,
+        'source': '/proc',
+        'target': '/proc',
+    }),
+    ('dev', {
+        'enable': True,
+        'source': '/dev',
+        'target': '/dev',
+    }),
+    ('devpts', {
+        'enable': True,
+        'source': '/dev/pts',
+        'target': '/dev/pts',
+    }),
+    ('shm', {
+        'enable': True,
+        'source': 'shmfs',
+        'target': '/dev/shm',
+    }),
+    ('run', {
+        'enable': True,
+        'source': 'tmpfs',
+        'target': '/run',
+    }),
+    ('portdir', {
+        'enable': True,
+        'source': 'config',
+        'target': 'config',
+    }),
+    ('distdir', {
+        'enable': True,
+        'source': 'config',
+        'target': 'config',
+    }),
+    ('pkgdir', {
+        'enable': False,
+        'source': 'config',
+        'target': 'config',
+    }),
+    ('port_tmpdir', {
+        'enable': True,
+        'source': 'maybe_tmpfs',
+        'target': '/var/tmp/portage',
+    }),
+    ('kerncache', {
+        'enable': False,
+        'source': 'config',
+        'target': '/tmp/kerncache',
+    }),
+    ('port_logdir', {
+        'enable': False,
+        'source': 'config',
+        'target': '/var/log/portage',
+    }),
+    ('ccache', {
+        'enable': False,
+        'source': 'config',
+        'target': '/var/tmp/ccache',
+    }),
+    ('icecream', {
+        'enable': False,
+        'source': ...,
+        'target': '/usr/lib/icecc/bin',
+    }),
+])
 
 option_messages = {
     "autoresume": "Autoresuming support enabled.",

Reply via email to