summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pym/portage/__init__.py27
-rw-r--r--pym/portage/util.py61
2 files changed, 87 insertions, 1 deletions
diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py
index cedbd2b8e..54faefad5 100644
--- a/pym/portage/__init__.py
+++ b/pym/portage/__init__.py
@@ -6907,6 +6907,20 @@ def create_trees(config_root=None, target_root=None, trees=None):
binarytree, myroot, mysettings["PKGDIR"], settings=mysettings)
return trees
+class _LegacyGlobalProxy(portage.util.ObjectProxy):
+ """
+ Instances of these serve as proxies to global variables
+ that are initialized on demand.
+ """
+ def __init__(self, name):
+ portage.util.ObjectProxy.__init__(self)
+ object.__setattr__(self, '_name', name)
+
+ def _get_target(self):
+ init_legacy_globals()
+ name = object.__getattribute__(self, '_name')
+ return globals()[name]
+
# 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
@@ -6915,7 +6929,14 @@ def create_trees(config_root=None, target_root=None, trees=None):
# code that is aware of this flag to import portage without the unnecessary
# overhead (and other issues!) of initializing the legacy globals.
+_globals_initialized = False
+
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, flushmtimedb
@@ -6974,7 +6995,11 @@ def init_legacy_globals():
# use within Portage. External use of this variable is unsupported because
# it is experimental and it's behavior is likely to change.
if "PORTAGE_LEGACY_GLOBALS" not in os.environ:
- init_legacy_globals()
+ for k in ("db", "settings", "root", "portdb", "selinux_enabled",
+ "mtimedbfile", "mtimedb", "archlist", "features", "groups",
+ "pkglines", "thirdpartymirrors", "usedefaults", "profiledir",
+ "flushmtimedb"):
+ globals()[k] = _LegacyGlobalProxy(k)
# Clear the cache
dircache={}
diff --git a/pym/portage/util.py b/pym/portage/util.py
index 61ee3d88d..821513ae6 100644
--- a/pym/portage/util.py
+++ b/pym/portage/util.py
@@ -912,6 +912,67 @@ def ensure_dirs(dir_path, *args, **kwargs):
perms_modified = apply_permissions(dir_path, *args, **kwargs)
return created_dir or perms_modified
+class ObjectProxy(object):
+
+ """
+ Object that acts as a proxy to another object, forwarding
+ attribute accesses and method calls. This can be useful
+ for implementing lazy initialization.
+ """
+
+ def _get_target(self):
+ raise NotImplementedError(self)
+
+ def __getattribute__(self, attr):
+ result = object.__getattribute__(self, '_get_target')()
+ return getattr(result, attr)
+
+ def __setattr__(self, attr, value):
+ result = object.__getattribute__(self, '_get_target')()
+ setattr(result, attr, value)
+
+ def __call__(self, *args, **kwargs):
+ result = object.__getattribute__(self, '_get_target')()
+ return result(*args, **kwargs)
+
+ def __setitem__(self, key, value):
+ object.__getattribute__(self, '_get_target')()[key] = value
+
+ def __getitem__(self, key):
+ return object.__getattribute__(self, '_get_target')()[key]
+
+ def __delitem__(self, key):
+ del object.__getattribute__(self, '_get_target')()[key]
+
+ def __contains__(self, key):
+ return key in object.__getattribute__(self, '_get_target')()
+
+ def __iter__(self):
+ return iter(object.__getattribute__(self, '_get_target')())
+
+ def __len__(self):
+ return len(object.__getattribute__(self, '_get_target')())
+
+ def __repr__(self):
+ return repr(object.__getattribute__(self, '_get_target')())
+
+ def __str__(self):
+ return str(object.__getattribute__(self, '_get_target')())
+
+ def __hash__(self):
+ return hash(object.__getattribute__(self, '_get_target')())
+
+ def __eq__(self, other):
+ return object.__getattribute__(self, '_get_target')() == other
+
+ def __ne__(self, other):
+ return object.__getattribute__(self, '_get_target')() != other
+
+ def __nonzero__(self):
+ if object.__getattribute__(self, '_get_target')():
+ return True
+ return False
+
class LazyItemsDict(dict):
"""A mapping object that behaves like a standard dict except that it allows
for lazy initialization of values via callable objects. Lazy items can be