public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] portage r10798 - main/trunk/pym/portage
@ 2008-06-26  5:28 Zac Medico (zmedico)
  0 siblings, 0 replies; only message in thread
From: Zac Medico (zmedico) @ 2008-06-26  5:28 UTC (permalink / raw
  To: gentoo-commits

Author: zmedico
Date: 2008-06-26 05:28:04 +0000 (Thu, 26 Jun 2008)
New Revision: 10798

Modified:
   main/trunk/pym/portage/__init__.py
   main/trunk/pym/portage/util.py
Log:
Implement lazy initialization of global "portdb", "settings" and other
variables that pollute the portage module. This works by initializing
the global variables with dummy "proxy" objects that serve as a means
to trigger lazy initialization. As soon as the first attribute access
or method call occurs on one of the proxy objects, it causes all the
proxy objects to be replaced with the real ones.

It's possible for an unsupported attribute access or method call on a
proxy object to trigger an error, leading to breakage. However, hopefully
these such corner cases will negligible (only time will tell).


Modified: main/trunk/pym/portage/__init__.py
===================================================================
--- main/trunk/pym/portage/__init__.py	2008-06-26 00:49:10 UTC (rev 10797)
+++ main/trunk/pym/portage/__init__.py	2008-06-26 05:28:04 UTC (rev 10798)
@@ -6907,6 +6907,20 @@
 			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 @@
 # 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 @@
 # 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={}

Modified: main/trunk/pym/portage/util.py
===================================================================
--- main/trunk/pym/portage/util.py	2008-06-26 00:49:10 UTC (rev 10797)
+++ main/trunk/pym/portage/util.py	2008-06-26 05:28:04 UTC (rev 10798)
@@ -912,6 +912,67 @@
 	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

-- 
gentoo-commits@lists.gentoo.org mailing list



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-06-26  5:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-26  5:28 [gentoo-commits] portage r10798 - main/trunk/pym/portage Zac Medico (zmedico)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox