Gentoo Archives: gentoo-commits

From: "Zac Medico (zmedico)" <zmedico@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] portage r10798 - main/trunk/pym/portage
Date: Thu, 26 Jun 2008 05:28:12
Message-Id: E1KBk1d-00074s-SV@stork.gentoo.org
1 Author: zmedico
2 Date: 2008-06-26 05:28:04 +0000 (Thu, 26 Jun 2008)
3 New Revision: 10798
4
5 Modified:
6 main/trunk/pym/portage/__init__.py
7 main/trunk/pym/portage/util.py
8 Log:
9 Implement lazy initialization of global "portdb", "settings" and other
10 variables that pollute the portage module. This works by initializing
11 the global variables with dummy "proxy" objects that serve as a means
12 to trigger lazy initialization. As soon as the first attribute access
13 or method call occurs on one of the proxy objects, it causes all the
14 proxy objects to be replaced with the real ones.
15
16 It's possible for an unsupported attribute access or method call on a
17 proxy object to trigger an error, leading to breakage. However, hopefully
18 these such corner cases will negligible (only time will tell).
19
20
21 Modified: main/trunk/pym/portage/__init__.py
22 ===================================================================
23 --- main/trunk/pym/portage/__init__.py 2008-06-26 00:49:10 UTC (rev 10797)
24 +++ main/trunk/pym/portage/__init__.py 2008-06-26 05:28:04 UTC (rev 10798)
25 @@ -6907,6 +6907,20 @@
26 binarytree, myroot, mysettings["PKGDIR"], settings=mysettings)
27 return trees
28
29 +class _LegacyGlobalProxy(portage.util.ObjectProxy):
30 + """
31 + Instances of these serve as proxies to global variables
32 + that are initialized on demand.
33 + """
34 + def __init__(self, name):
35 + portage.util.ObjectProxy.__init__(self)
36 + object.__setattr__(self, '_name', name)
37 +
38 + def _get_target(self):
39 + init_legacy_globals()
40 + name = object.__getattribute__(self, '_name')
41 + return globals()[name]
42 +
43 # Initialization of legacy globals. No functions/classes below this point
44 # please! When the above functions and classes become independent of the
45 # below global variables, it will be possible to make the below code
46 @@ -6915,7 +6929,14 @@
47 # code that is aware of this flag to import portage without the unnecessary
48 # overhead (and other issues!) of initializing the legacy globals.
49
50 +_globals_initialized = False
51 +
52 def init_legacy_globals():
53 + global _globals_initialized
54 + if _globals_initialized:
55 + return
56 + _globals_initialized = True
57 +
58 global db, settings, root, portdb, selinux_enabled, mtimedbfile, mtimedb, \
59 archlist, features, groups, pkglines, thirdpartymirrors, usedefaults, \
60 profiledir, flushmtimedb
61 @@ -6974,7 +6995,11 @@
62 # use within Portage. External use of this variable is unsupported because
63 # it is experimental and it's behavior is likely to change.
64 if "PORTAGE_LEGACY_GLOBALS" not in os.environ:
65 - init_legacy_globals()
66 + for k in ("db", "settings", "root", "portdb", "selinux_enabled",
67 + "mtimedbfile", "mtimedb", "archlist", "features", "groups",
68 + "pkglines", "thirdpartymirrors", "usedefaults", "profiledir",
69 + "flushmtimedb"):
70 + globals()[k] = _LegacyGlobalProxy(k)
71
72 # Clear the cache
73 dircache={}
74
75 Modified: main/trunk/pym/portage/util.py
76 ===================================================================
77 --- main/trunk/pym/portage/util.py 2008-06-26 00:49:10 UTC (rev 10797)
78 +++ main/trunk/pym/portage/util.py 2008-06-26 05:28:04 UTC (rev 10798)
79 @@ -912,6 +912,67 @@
80 perms_modified = apply_permissions(dir_path, *args, **kwargs)
81 return created_dir or perms_modified
82
83 +class ObjectProxy(object):
84 +
85 + """
86 + Object that acts as a proxy to another object, forwarding
87 + attribute accesses and method calls. This can be useful
88 + for implementing lazy initialization.
89 + """
90 +
91 + def _get_target(self):
92 + raise NotImplementedError(self)
93 +
94 + def __getattribute__(self, attr):
95 + result = object.__getattribute__(self, '_get_target')()
96 + return getattr(result, attr)
97 +
98 + def __setattr__(self, attr, value):
99 + result = object.__getattribute__(self, '_get_target')()
100 + setattr(result, attr, value)
101 +
102 + def __call__(self, *args, **kwargs):
103 + result = object.__getattribute__(self, '_get_target')()
104 + return result(*args, **kwargs)
105 +
106 + def __setitem__(self, key, value):
107 + object.__getattribute__(self, '_get_target')()[key] = value
108 +
109 + def __getitem__(self, key):
110 + return object.__getattribute__(self, '_get_target')()[key]
111 +
112 + def __delitem__(self, key):
113 + del object.__getattribute__(self, '_get_target')()[key]
114 +
115 + def __contains__(self, key):
116 + return key in object.__getattribute__(self, '_get_target')()
117 +
118 + def __iter__(self):
119 + return iter(object.__getattribute__(self, '_get_target')())
120 +
121 + def __len__(self):
122 + return len(object.__getattribute__(self, '_get_target')())
123 +
124 + def __repr__(self):
125 + return repr(object.__getattribute__(self, '_get_target')())
126 +
127 + def __str__(self):
128 + return str(object.__getattribute__(self, '_get_target')())
129 +
130 + def __hash__(self):
131 + return hash(object.__getattribute__(self, '_get_target')())
132 +
133 + def __eq__(self, other):
134 + return object.__getattribute__(self, '_get_target')() == other
135 +
136 + def __ne__(self, other):
137 + return object.__getattribute__(self, '_get_target')() != other
138 +
139 + def __nonzero__(self):
140 + if object.__getattribute__(self, '_get_target')():
141 + return True
142 + return False
143 +
144 class LazyItemsDict(dict):
145 """A mapping object that behaves like a standard dict except that it allows
146 for lazy initialization of values via callable objects. Lazy items can be
147
148 --
149 gentoo-commits@l.g.o mailing list