1 |
Author: zmedico |
2 |
Date: 2009-02-20 06:15:34 +0000 (Fri, 20 Feb 2009) |
3 |
New Revision: 12661 |
4 |
|
5 |
Modified: |
6 |
main/trunk/pym/portage/__init__.py |
7 |
main/trunk/pym/portage/dbapi/bintree.py |
8 |
main/trunk/pym/portage/util.py |
9 |
Log: |
10 |
Add a new portage.utils.lazy_import() function which behaves similar to the |
11 |
snakeoil.demandload.demandload() function. |
12 |
|
13 |
|
14 |
Modified: main/trunk/pym/portage/__init__.py |
15 |
=================================================================== |
16 |
--- main/trunk/pym/portage/__init__.py 2009-02-20 03:01:56 UTC (rev 12660) |
17 |
+++ main/trunk/pym/portage/__init__.py 2009-02-20 06:15:34 UTC (rev 12661) |
18 |
@@ -73,18 +73,31 @@ |
19 |
|
20 |
try: |
21 |
from portage.cache.cache_errors import CacheError |
22 |
- import portage.cvstree |
23 |
- import portage.xpak |
24 |
- import portage.getbinpkg |
25 |
- import portage.dep |
26 |
- from portage.dep import dep_getcpv, dep_getkey, get_operator, \ |
27 |
- isjustname, isspecific, isvalidatom, \ |
28 |
- match_from_list, match_to_list, best_match_to_list |
29 |
+ import portage.util as util |
30 |
+ util.lazy_import(globals(), |
31 |
+ 'portage.checksum', |
32 |
+ 'portage.checksum:perform_checksum,perform_md5,prelink_capable', |
33 |
+ 'portage.cvstree', |
34 |
+ 'portage.dep', |
35 |
+ 'portage.dep:best_match_to_list,dep_getcpv,dep_getkey,' + \ |
36 |
+ 'get_operator,isjustname,isspecific,isvalidatom,' + \ |
37 |
+ 'match_from_list,match_to_list', |
38 |
+ 'portage.eclass_cache', |
39 |
+ 'portage.getbinpkg', |
40 |
+ 'portage.locks', |
41 |
+ 'portage.locks:lockdir,lockfile,unlockdir,unlockfile', |
42 |
+ 'portage.output', |
43 |
+ 'portage.output:bold,colorize', |
44 |
+ 'portage.process', |
45 |
+ 'portage.process:atexit_register,run_exitfuncs', |
46 |
+ 'portage.update:dep_transform,fixdbentries,grab_updates,' + \ |
47 |
+ 'parse_updates,update_config_files,update_dbentries,' + \ |
48 |
+ 'update_dbentry', |
49 |
+ 'portage.versions:best,catpkgsplit,catsplit,endversion_keys,' + \ |
50 |
+ 'suffix_value@endversion,pkgcmp,pkgsplit,vercmp,ververify', |
51 |
+ 'portage.xpak', |
52 |
+ ) |
53 |
|
54 |
- # XXX: This needs to get cleaned up. |
55 |
- import portage.output |
56 |
- from portage.output import bold, colorize, green, red, yellow |
57 |
- |
58 |
import portage.const |
59 |
from portage.const import VDB_PATH, PRIVATE_PATH, CACHE_PATH, DEPCACHE_PATH, \ |
60 |
USER_CONFIG_PATH, MODULES_FILE_PATH, CUSTOM_PROFILE_PATH, PORTAGE_BASE_PATH, \ |
61 |
@@ -99,32 +112,14 @@ |
62 |
portage_uid, portage_gid, userpriv_groups |
63 |
from portage.manifest import Manifest |
64 |
|
65 |
- import portage.util |
66 |
from portage.util import atomic_ofstream, apply_secpass_permissions, apply_recursive_permissions, \ |
67 |
dump_traceback, getconfig, grabdict, grabdict_package, grabfile, grabfile_package, \ |
68 |
map_dictlist_vals, new_protect_filename, normalize_path, \ |
69 |
pickle_read, pickle_write, stack_dictlist, stack_dicts, stack_lists, \ |
70 |
unique_array, varexpand, writedict, writemsg, writemsg_stdout, write_atomic |
71 |
import portage.exception |
72 |
- import portage.locks |
73 |
- import portage.process |
74 |
- from portage.process import atexit_register, run_exitfuncs |
75 |
- from portage.locks import unlockfile,unlockdir,lockfile,lockdir |
76 |
- import portage.checksum |
77 |
- from portage.checksum import perform_md5,perform_checksum,prelink_capable |
78 |
- import portage.eclass_cache |
79 |
from portage.localization import _ |
80 |
- from portage.update import dep_transform, fixdbentries, grab_updates, \ |
81 |
- parse_updates, update_config_files, update_dbentries, update_dbentry |
82 |
|
83 |
- # Need these functions directly in portage namespace to not break every external tool in existence |
84 |
- from portage.versions import best, catpkgsplit, catsplit, pkgcmp, \ |
85 |
- pkgsplit, vercmp, ververify |
86 |
- |
87 |
- # endversion and endversion_keys are for backward compatibility only. |
88 |
- from portage.versions import endversion_keys |
89 |
- from portage.versions import suffix_value as endversion |
90 |
- |
91 |
except ImportError, e: |
92 |
sys.stderr.write("\n\n") |
93 |
sys.stderr.write("!!! Failed to complete portage imports. There are internal modules for\n") |
94 |
|
95 |
Modified: main/trunk/pym/portage/dbapi/bintree.py |
96 |
=================================================================== |
97 |
--- main/trunk/pym/portage/dbapi/bintree.py 2009-02-20 03:01:56 UTC (rev 12660) |
98 |
+++ main/trunk/pym/portage/dbapi/bintree.py 2009-02-20 06:15:34 UTC (rev 12661) |
99 |
@@ -16,7 +16,7 @@ |
100 |
|
101 |
from portage import dep_expand, listdir, _check_distfile, _movefile |
102 |
|
103 |
-import portage.xpak, portage.getbinpkg |
104 |
+import portage |
105 |
|
106 |
import os, errno, stat |
107 |
import re |
108 |
|
109 |
Modified: main/trunk/pym/portage/util.py |
110 |
=================================================================== |
111 |
--- main/trunk/pym/portage/util.py 2009-02-20 03:01:56 UTC (rev 12660) |
112 |
+++ main/trunk/pym/portage/util.py 2009-02-20 06:15:34 UTC (rev 12661) |
113 |
@@ -10,6 +10,7 @@ |
114 |
import stat |
115 |
import string |
116 |
import sys |
117 |
+import types |
118 |
|
119 |
from portage.exception import PortageException, FileNotFound, \ |
120 |
OperationNotPermitted, PermissionDenied, ReadOnlyFileSystem |
121 |
@@ -341,6 +342,8 @@ |
122 |
for implementing lazy initialization. |
123 |
""" |
124 |
|
125 |
+ __slots__ = () |
126 |
+ |
127 |
def _get_target(self): |
128 |
raise NotImplementedError(self) |
129 |
|
130 |
@@ -392,6 +395,104 @@ |
131 |
def __nonzero__(self): |
132 |
return bool(object.__getattribute__(self, '_get_target')()) |
133 |
|
134 |
+class _LazyImport(ObjectProxy): |
135 |
+ |
136 |
+ __slots__ = ('_scope', '_alias', '_name', '_target') |
137 |
+ |
138 |
+ def __init__(self, scope, alias, name): |
139 |
+ ObjectProxy.__init__(self) |
140 |
+ object.__setattr__(self, '_scope', scope) |
141 |
+ object.__setattr__(self, '_alias', alias) |
142 |
+ object.__setattr__(self, '_name', name) |
143 |
+ |
144 |
+ def _get_target(self): |
145 |
+ try: |
146 |
+ return object.__getattribute__(self, '_target') |
147 |
+ except AttributeError: |
148 |
+ pass |
149 |
+ name = object.__getattribute__(self, '_name') |
150 |
+ __import__(name) |
151 |
+ target = sys.modules[name] |
152 |
+ object.__setattr__(self, '_target', target) |
153 |
+ object.__getattribute__(self, '_scope')[ |
154 |
+ object.__getattribute__(self, '_alias')] = target |
155 |
+ return target |
156 |
+ |
157 |
+class _LazyImportFrom(_LazyImport): |
158 |
+ |
159 |
+ __slots__ = () |
160 |
+ |
161 |
+ def _get_target(self): |
162 |
+ try: |
163 |
+ return object.__getattribute__(self, '_target') |
164 |
+ except AttributeError: |
165 |
+ pass |
166 |
+ name = object.__getattribute__(self, '_name') |
167 |
+ components = name.split('.') |
168 |
+ parent_name = '.'.join(components[:-1]) |
169 |
+ __import__(parent_name) |
170 |
+ target = getattr(sys.modules[parent_name], components[-1]) |
171 |
+ object.__setattr__(self, '_target', target) |
172 |
+ object.__getattribute__(self, '_scope')[ |
173 |
+ object.__getattribute__(self, '_alias')] = target |
174 |
+ return target |
175 |
+ |
176 |
+def lazy_import(scope, *args): |
177 |
+ """ |
178 |
+ Create a proxy in the given scope in order to performa a lazy import. |
179 |
+ |
180 |
+ Syntax Result |
181 |
+ foo import foo |
182 |
+ foo:bar,baz from foo import bar, baz |
183 |
+ foo:bar@baz from foo import bar as baz |
184 |
+ |
185 |
+ @param scope: the scope in which to place the import, typically globals() |
186 |
+ @type myfilename: dict |
187 |
+ @param args: module names to import |
188 |
+ @type args: strings |
189 |
+ """ |
190 |
+ |
191 |
+ for s in args: |
192 |
+ parts = s.split(':', 1) |
193 |
+ if len(parts) == 1: |
194 |
+ name = s |
195 |
+ |
196 |
+ if not name or not isinstance(name, basestring): |
197 |
+ raise ValueError(name) |
198 |
+ |
199 |
+ components = name.split('.') |
200 |
+ parent_scope = scope |
201 |
+ for i in xrange(len(components)): |
202 |
+ alias = components[i] |
203 |
+ mod = parent_scope.get(alias) |
204 |
+ if isinstance(mod, types.ModuleType): |
205 |
+ parent_scope = mod.__dict__ |
206 |
+ continue |
207 |
+ if i < len(components) - 1: |
208 |
+ parent_name = ".".join(components[:i+1]) |
209 |
+ __import__(parent_name) |
210 |
+ mod = sys.modules.get(parent_name) |
211 |
+ if not isinstance(mod, types.ModuleType): |
212 |
+ # raise an exception |
213 |
+ __import__(name) |
214 |
+ parent_scope[alias] = mod |
215 |
+ parent_scope = mod.__dict__ |
216 |
+ continue |
217 |
+ parent_scope[alias] = _LazyImport(parent_scope, alias, name) |
218 |
+ |
219 |
+ else: |
220 |
+ name, fromlist = parts |
221 |
+ fromlist = fromlist.split(',') |
222 |
+ for s in fromlist: |
223 |
+ alias = s.split('@', 1) |
224 |
+ if len(alias) == 1: |
225 |
+ alias = alias[0] |
226 |
+ orig = alias |
227 |
+ else: |
228 |
+ orig, alias = alias |
229 |
+ scope[alias] = _LazyImportFrom(scope, alias, |
230 |
+ name + '.' + orig) |
231 |
+ |
232 |
class _tolerant_shlex(shlex.shlex): |
233 |
def sourcehook(self, newfile): |
234 |
try: |