1 |
Author: zmedico |
2 |
Date: 2010-02-28 06:01:47 +0000 (Sun, 28 Feb 2010) |
3 |
New Revision: 15492 |
4 |
|
5 |
Added: |
6 |
main/trunk/pym/portage/_legacy_globals.py |
7 |
Modified: |
8 |
main/trunk/pym/portage/__init__.py |
9 |
Log: |
10 |
Move legacy globals code into a _legacy_globals module that's imported on |
11 |
demand. |
12 |
|
13 |
|
14 |
Modified: main/trunk/pym/portage/__init__.py |
15 |
=================================================================== |
16 |
--- main/trunk/pym/portage/__init__.py 2010-02-28 04:28:21 UTC (rev 15491) |
17 |
+++ main/trunk/pym/portage/__init__.py 2010-02-28 06:01:47 UTC (rev 15492) |
18 |
@@ -564,10 +564,6 @@ |
19 |
return trees |
20 |
|
21 |
class _LegacyGlobalProxy(proxy.objectproxy.ObjectProxy): |
22 |
- """ |
23 |
- Instances of these serve as proxies to global variables |
24 |
- that are initialized on demand. |
25 |
- """ |
26 |
|
27 |
__slots__ = ('_name',) |
28 |
|
29 |
@@ -576,53 +572,19 @@ |
30 |
object.__setattr__(self, '_name', name) |
31 |
|
32 |
def _get_target(self): |
33 |
- init_legacy_globals() |
34 |
name = object.__getattribute__(self, '_name') |
35 |
- return globals()[name] |
36 |
+ from portage._legacy_globals import _get_legacy_global |
37 |
+ return _get_legacy_global(name) |
38 |
|
39 |
-class _PortdbProxy(proxy.objectproxy.ObjectProxy): |
40 |
- """ |
41 |
- The portdb is initialized separately from the rest |
42 |
- of the variables, since sometimes the other variables |
43 |
- are needed while the portdb is not. |
44 |
- """ |
45 |
- |
46 |
- __slots__ = () |
47 |
- |
48 |
- def _get_target(self): |
49 |
- init_legacy_globals() |
50 |
- global db, portdb, root, _portdb_initialized |
51 |
- if not _portdb_initialized: |
52 |
- portdb = db[root]["porttree"].dbapi |
53 |
- _portdb_initialized = True |
54 |
- return portdb |
55 |
- |
56 |
-class _MtimedbProxy(proxy.objectproxy.ObjectProxy): |
57 |
- """ |
58 |
- The mtimedb is independent from the portdb and other globals. |
59 |
- """ |
60 |
- |
61 |
- __slots__ = ('_name',) |
62 |
- |
63 |
- def __init__(self, name): |
64 |
- proxy.objectproxy.ObjectProxy.__init__(self) |
65 |
- object.__setattr__(self, '_name', name) |
66 |
- |
67 |
- def _get_target(self): |
68 |
- global mtimedb, mtimedbfile, _mtimedb_initialized |
69 |
- if not _mtimedb_initialized: |
70 |
- mtimedbfile = os.path.join(os.path.sep, |
71 |
- CACHE_PATH, "mtimedb") |
72 |
- mtimedb = MtimeDB(mtimedbfile) |
73 |
- _mtimedb_initialized = True |
74 |
- name = object.__getattribute__(self, '_name') |
75 |
- return globals()[name] |
76 |
- |
77 |
_legacy_global_var_names = ("archlist", "db", "features", |
78 |
"groups", "mtimedb", "mtimedbfile", "pkglines", |
79 |
"portdb", "profiledir", "root", "selinux_enabled", |
80 |
"settings", "thirdpartymirrors", "usedefaults") |
81 |
|
82 |
+for k in _legacy_global_var_names: |
83 |
+ globals()[k] = _LegacyGlobalProxy(k) |
84 |
+del k |
85 |
+ |
86 |
def _disable_legacy_globals(): |
87 |
""" |
88 |
This deletes the ObjectProxy instances that are used |
89 |
@@ -633,80 +595,3 @@ |
90 |
global _legacy_global_var_names |
91 |
for k in _legacy_global_var_names: |
92 |
globals().pop(k, None) |
93 |
- |
94 |
-# Initialization of legacy globals. No functions/classes below this point |
95 |
-# please! When the above functions and classes become independent of the |
96 |
-# below global variables, it will be possible to make the below code |
97 |
-# conditional on a backward compatibility flag (backward compatibility could |
98 |
-# be disabled via an environment variable, for example). This will enable new |
99 |
-# code that is aware of this flag to import portage without the unnecessary |
100 |
-# overhead (and other issues!) of initializing the legacy globals. |
101 |
- |
102 |
-def init_legacy_globals(): |
103 |
- global _globals_initialized |
104 |
- if _globals_initialized: |
105 |
- return |
106 |
- _globals_initialized = True |
107 |
- |
108 |
- global db, settings, root, portdb, selinux_enabled, mtimedbfile, mtimedb, \ |
109 |
- archlist, features, groups, pkglines, thirdpartymirrors, usedefaults, \ |
110 |
- profiledir |
111 |
- |
112 |
- # Portage needs to ensure a sane umask for the files it creates. |
113 |
- os.umask(0o22) |
114 |
- |
115 |
- kwargs = {} |
116 |
- for k, envvar in (("config_root", "PORTAGE_CONFIGROOT"), ("target_root", "ROOT")): |
117 |
- kwargs[k] = os.environ.get(envvar, "/") |
118 |
- |
119 |
- global _initializing_globals |
120 |
- _initializing_globals = True |
121 |
- db = create_trees(**kwargs) |
122 |
- del _initializing_globals |
123 |
- |
124 |
- settings = db["/"]["vartree"].settings |
125 |
- |
126 |
- for myroot in db: |
127 |
- if myroot != "/": |
128 |
- settings = db[myroot]["vartree"].settings |
129 |
- break |
130 |
- |
131 |
- root = settings["ROOT"] |
132 |
- output._init(config_root=settings['PORTAGE_CONFIGROOT']) |
133 |
- |
134 |
- # ======================================================================== |
135 |
- # COMPATIBILITY |
136 |
- # These attributes should not be used |
137 |
- # within Portage under any circumstances. |
138 |
- # ======================================================================== |
139 |
- archlist = settings.archlist() |
140 |
- features = settings.features |
141 |
- groups = settings["ACCEPT_KEYWORDS"].split() |
142 |
- pkglines = settings.packages |
143 |
- selinux_enabled = settings.selinux_enabled() |
144 |
- thirdpartymirrors = settings.thirdpartymirrors() |
145 |
- usedefaults = settings.use_defs |
146 |
- profiledir = os.path.join(settings["PORTAGE_CONFIGROOT"], PROFILE_PATH) |
147 |
- if not os.path.isdir(profiledir): |
148 |
- profiledir = None |
149 |
- # ======================================================================== |
150 |
- # COMPATIBILITY |
151 |
- # These attributes should not be used |
152 |
- # within Portage under any circumstances. |
153 |
- # ======================================================================== |
154 |
- |
155 |
-if True: |
156 |
- |
157 |
- _mtimedb_initialized = False |
158 |
- mtimedb = _MtimedbProxy("mtimedb") |
159 |
- mtimedbfile = _MtimedbProxy("mtimedbfile") |
160 |
- |
161 |
- _portdb_initialized = False |
162 |
- portdb = _PortdbProxy() |
163 |
- |
164 |
- _globals_initialized = False |
165 |
- |
166 |
- for k in ("db", "settings", "root", "selinux_enabled", |
167 |
- "archlist", "features", "groups", |
168 |
- "pkglines", "thirdpartymirrors", "usedefaults", "profiledir"): |
169 |
- globals()[k] = _LegacyGlobalProxy(k) |
170 |
|
171 |
Added: main/trunk/pym/portage/_legacy_globals.py |
172 |
=================================================================== |
173 |
--- main/trunk/pym/portage/_legacy_globals.py (rev 0) |
174 |
+++ main/trunk/pym/portage/_legacy_globals.py 2010-02-28 06:01:47 UTC (rev 15492) |
175 |
@@ -0,0 +1,87 @@ |
176 |
+# Copyright 2010 Gentoo Foundation |
177 |
+# Distributed under the terms of the GNU General Public License v2 |
178 |
+# $Id$ |
179 |
+ |
180 |
+import portage |
181 |
+from portage import os |
182 |
+from portage.const import CACHE_PATH, PROFILE_PATH |
183 |
+ |
184 |
+_legacy_globals = {} |
185 |
+ |
186 |
+def _get_legacy_global(name): |
187 |
+ global _legacy_globals |
188 |
+ target = _legacy_globals.get(name, _legacy_globals) |
189 |
+ if target is not _legacy_globals: |
190 |
+ return target |
191 |
+ |
192 |
+ if name == 'portdb': |
193 |
+ portage.portdb = portage.db[portage.root]["porttree"].dbapi |
194 |
+ _legacy_globals[name] = portage.portdb |
195 |
+ return _legacy_globals[name] |
196 |
+ elif name in ('mtimedb', 'mtimedbfile'): |
197 |
+ portage.mtimedbfile = os.path.join(portage.root, |
198 |
+ CACHE_PATH, "mtimedb") |
199 |
+ _legacy_globals['mtimedbfile'] = portage.mtimedbfile |
200 |
+ portage.mtimedb = portage.MtimeDB(portage.mtimedbfile) |
201 |
+ _legacy_globals['mtimedb'] = portage.mtimedb |
202 |
+ return _legacy_globals[name] |
203 |
+ |
204 |
+ # Portage needs to ensure a sane umask for the files it creates. |
205 |
+ os.umask(0o22) |
206 |
+ |
207 |
+ kwargs = {} |
208 |
+ for k, envvar in (("config_root", "PORTAGE_CONFIGROOT"), ("target_root", "ROOT")): |
209 |
+ kwargs[k] = os.environ.get(envvar, "/") |
210 |
+ |
211 |
+ portage._initializing_globals = True |
212 |
+ portage.db = portage.create_trees(**kwargs) |
213 |
+ _legacy_globals['db'] = portage.db |
214 |
+ del portage._initializing_globals |
215 |
+ |
216 |
+ settings = portage.db["/"]["vartree"].settings |
217 |
+ |
218 |
+ for root in portage.db: |
219 |
+ if root != "/": |
220 |
+ settings = portage.db[root]["vartree"].settings |
221 |
+ break |
222 |
+ |
223 |
+ portage.output._init(config_root=settings['PORTAGE_CONFIGROOT']) |
224 |
+ |
225 |
+ portage.settings = settings |
226 |
+ _legacy_globals['settings'] = settings |
227 |
+ |
228 |
+ portage.root = root |
229 |
+ _legacy_globals['root'] = root |
230 |
+ |
231 |
+ # COMPATIBILITY |
232 |
+ # These attributes should not be used within |
233 |
+ # Portage under any circumstances. |
234 |
+ |
235 |
+ portage.archlist = settings.archlist() |
236 |
+ _legacy_globals['archlist'] = portage.archlist |
237 |
+ |
238 |
+ portage.features = settings.features |
239 |
+ _legacy_globals['features'] = portage.features |
240 |
+ |
241 |
+ portage.groups = settings["ACCEPT_KEYWORDS"].split() |
242 |
+ _legacy_globals['groups'] = portage.groups |
243 |
+ |
244 |
+ portage.pkglines = settings.packages |
245 |
+ _legacy_globals['pkglines'] = portage.pkglines |
246 |
+ |
247 |
+ portage.selinux_enabled = settings.selinux_enabled() |
248 |
+ _legacy_globals['selinux_enabled'] = portage.selinux_enabled |
249 |
+ |
250 |
+ portage.thirdpartymirrors = settings.thirdpartymirrors() |
251 |
+ _legacy_globals['thirdpartymirrors'] = portage.thirdpartymirrors |
252 |
+ |
253 |
+ portage.usedefaults = settings.use_defs |
254 |
+ _legacy_globals['usedefaults'] = portage.usedefaults |
255 |
+ |
256 |
+ profiledir = os.path.join(settings["PORTAGE_CONFIGROOT"], PROFILE_PATH) |
257 |
+ if not os.path.isdir(profiledir): |
258 |
+ profiledir = None |
259 |
+ portage.profiledir = profiledir |
260 |
+ _legacy_globals['profiledir'] = portage.profiledir |
261 |
+ |
262 |
+ return _legacy_globals[name] |
263 |
|
264 |
|
265 |
Property changes on: main/trunk/pym/portage/_legacy_globals.py |
266 |
___________________________________________________________________ |
267 |
Added: svn:keywords |
268 |
+ Id |