Move legacy globals code into a _legacy_globals module that's imported on
authorZac Medico <zmedico@gentoo.org>
Tue, 2 Mar 2010 21:09:07 +0000 (21:09 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 2 Mar 2010 21:09:07 +0000 (21:09 -0000)
demand. (trunk r15492)

svn path=/main/branches/2.1.7/; revision=15693

pym/portage/__init__.py
pym/portage/_legacy_globals.py [new file with mode: 0644]

index ccfacebc7d081ea2d7e89a06bb2d1be02ad9b781..9d0f929b22864fb4d861345c02d2fb2bc76563c9 100644 (file)
@@ -564,43 +564,6 @@ def create_trees(config_root=None, target_root=None, trees=None):
        return trees
 
 class _LegacyGlobalProxy(proxy.objectproxy.ObjectProxy):
-       """
-       Instances of these serve as proxies to global variables
-       that are initialized on demand.
-       """
-
-       __slots__ = ('_name',)
-
-       def __init__(self, name):
-               proxy.objectproxy.ObjectProxy.__init__(self)
-               object.__setattr__(self, '_name', name)
-
-       def _get_target(self):
-               init_legacy_globals()
-               name = object.__getattribute__(self, '_name')
-               return globals()[name]
-
-class _PortdbProxy(proxy.objectproxy.ObjectProxy):
-       """
-       The portdb is initialized separately from the rest
-       of the variables, since sometimes the other variables
-       are needed while the portdb is not.
-       """
-
-       __slots__ = ()
-
-       def _get_target(self):
-               init_legacy_globals()
-               global db, portdb, root, _portdb_initialized
-               if not _portdb_initialized:
-                       portdb = db[root]["porttree"].dbapi
-                       _portdb_initialized = True
-               return portdb
-
-class _MtimedbProxy(proxy.objectproxy.ObjectProxy):
-       """
-       The mtimedb is independent from the portdb and other globals.
-       """
 
        __slots__ = ('_name',)
 
@@ -609,20 +572,19 @@ class _MtimedbProxy(proxy.objectproxy.ObjectProxy):
                object.__setattr__(self, '_name', name)
 
        def _get_target(self):
-               global mtimedb, mtimedbfile, _mtimedb_initialized
-               if not _mtimedb_initialized:
-                       mtimedbfile = os.path.join(os.path.sep,
-                               CACHE_PATH, "mtimedb")
-                       mtimedb = MtimeDB(mtimedbfile)
-                       _mtimedb_initialized = True
                name = object.__getattribute__(self, '_name')
-               return globals()[name]
+               from portage._legacy_globals import _get_legacy_global
+               return _get_legacy_global(name)
 
 _legacy_global_var_names = ("archlist", "db", "features",
        "groups", "mtimedb", "mtimedbfile", "pkglines",
        "portdb", "profiledir", "root", "selinux_enabled",
        "settings", "thirdpartymirrors", "usedefaults")
 
+for k in _legacy_global_var_names:
+       globals()[k] = _LegacyGlobalProxy(k)
+del k
+
 def _disable_legacy_globals():
        """
        This deletes the ObjectProxy instances that are used
@@ -633,80 +595,3 @@ def _disable_legacy_globals():
        global _legacy_global_var_names
        for k in _legacy_global_var_names:
                globals().pop(k, None)
-
-# Initialization of legacy globals.  No functions/classes below this point
-# please!  When the above functions and classes become independent of the
-# below global variables, it will be possible to make the below code
-# conditional on a backward compatibility flag (backward compatibility could
-# be disabled via an environment variable, for example).  This will enable new
-# code that is aware of this flag to import portage without the unnecessary
-# overhead (and other issues!) of initializing the legacy globals.
-
-def init_legacy_globals():
-       global _globals_initialized
-       if _globals_initialized:
-               return
-       _globals_initialized = True
-
-       global db, settings, root, portdb, selinux_enabled, mtimedbfile, mtimedb, \
-       archlist, features, groups, pkglines, thirdpartymirrors, usedefaults, \
-       profiledir
-
-       # Portage needs to ensure a sane umask for the files it creates.
-       os.umask(0o22)
-
-       kwargs = {}
-       for k, envvar in (("config_root", "PORTAGE_CONFIGROOT"), ("target_root", "ROOT")):
-               kwargs[k] = os.environ.get(envvar, "/")
-
-       global _initializing_globals
-       _initializing_globals = True
-       db = create_trees(**kwargs)
-       del _initializing_globals
-
-       settings = db["/"]["vartree"].settings
-
-       for myroot in db:
-               if myroot != "/":
-                       settings = db[myroot]["vartree"].settings
-                       break
-
-       root = settings["ROOT"]
-       output._init(config_root=settings['PORTAGE_CONFIGROOT'])
-
-       # ========================================================================
-       # COMPATIBILITY
-       # These attributes should not be used
-       # within Portage under any circumstances.
-       # ========================================================================
-       archlist    = settings.archlist()
-       features    = settings.features
-       groups      = settings["ACCEPT_KEYWORDS"].split()
-       pkglines    = settings.packages
-       selinux_enabled   = settings.selinux_enabled()
-       thirdpartymirrors = settings.thirdpartymirrors()
-       usedefaults       = settings.use_defs
-       profiledir  = os.path.join(settings["PORTAGE_CONFIGROOT"], PROFILE_PATH)
-       if not os.path.isdir(profiledir):
-               profiledir = None
-       # ========================================================================
-       # COMPATIBILITY
-       # These attributes should not be used
-       # within Portage under any circumstances.
-       # ========================================================================
-
-if True:
-
-       _mtimedb_initialized = False
-       mtimedb     = _MtimedbProxy("mtimedb")
-       mtimedbfile = _MtimedbProxy("mtimedbfile")
-
-       _portdb_initialized  = False
-       portdb = _PortdbProxy()
-
-       _globals_initialized = False
-
-       for k in ("db", "settings", "root", "selinux_enabled",
-               "archlist", "features", "groups",
-               "pkglines", "thirdpartymirrors", "usedefaults", "profiledir"):
-               globals()[k] = _LegacyGlobalProxy(k)
diff --git a/pym/portage/_legacy_globals.py b/pym/portage/_legacy_globals.py
new file mode 100644 (file)
index 0000000..432d8d8
--- /dev/null
@@ -0,0 +1,87 @@
+# Copyright 2010 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+import portage
+from portage import os
+from portage.const import CACHE_PATH, PROFILE_PATH
+
+_legacy_globals = {}
+
+def _get_legacy_global(name):
+       global _legacy_globals
+       target = _legacy_globals.get(name, _legacy_globals)
+       if target is not _legacy_globals:
+               return target
+
+       if name == 'portdb':
+               portage.portdb = portage.db[portage.root]["porttree"].dbapi
+               _legacy_globals[name] = portage.portdb
+               return _legacy_globals[name]
+       elif name in ('mtimedb', 'mtimedbfile'):
+               portage.mtimedbfile = os.path.join(portage.root,
+                       CACHE_PATH, "mtimedb")
+               _legacy_globals['mtimedbfile'] = portage.mtimedbfile
+               portage.mtimedb = portage.MtimeDB(portage.mtimedbfile)
+               _legacy_globals['mtimedb'] = portage.mtimedb
+               return _legacy_globals[name]
+
+       # Portage needs to ensure a sane umask for the files it creates.
+       os.umask(0o22)
+
+       kwargs = {}
+       for k, envvar in (("config_root", "PORTAGE_CONFIGROOT"), ("target_root", "ROOT")):
+               kwargs[k] = os.environ.get(envvar, "/")
+
+       portage._initializing_globals = True
+       portage.db = portage.create_trees(**kwargs)
+       _legacy_globals['db'] = portage.db
+       del portage._initializing_globals
+
+       settings = portage.db["/"]["vartree"].settings
+
+       for root in portage.db:
+               if root != "/":
+                       settings = portage.db[root]["vartree"].settings
+                       break
+
+       portage.output._init(config_root=settings['PORTAGE_CONFIGROOT'])
+
+       portage.settings = settings
+       _legacy_globals['settings'] = settings
+
+       portage.root = root
+       _legacy_globals['root'] = root
+
+       # COMPATIBILITY
+       # These attributes should not be used within
+       # Portage under any circumstances.
+
+       portage.archlist = settings.archlist()
+       _legacy_globals['archlist'] = portage.archlist
+
+       portage.features = settings.features
+       _legacy_globals['features'] = portage.features
+
+       portage.groups = settings["ACCEPT_KEYWORDS"].split()
+       _legacy_globals['groups'] = portage.groups
+
+       portage.pkglines = settings.packages
+       _legacy_globals['pkglines'] = portage.pkglines
+
+       portage.selinux_enabled = settings.selinux_enabled()
+       _legacy_globals['selinux_enabled'] = portage.selinux_enabled
+
+       portage.thirdpartymirrors = settings.thirdpartymirrors()
+       _legacy_globals['thirdpartymirrors'] = portage.thirdpartymirrors
+
+       portage.usedefaults = settings.use_defs
+       _legacy_globals['usedefaults'] = portage.usedefaults
+
+       profiledir = os.path.join(settings["PORTAGE_CONFIGROOT"], PROFILE_PATH)
+       if not os.path.isdir(profiledir):
+               profiledir = None
+       portage.profiledir = profiledir
+       _legacy_globals['profiledir'] = portage.profiledir
+
+       return _legacy_globals[name]