-
Notifications
You must be signed in to change notification settings - Fork 0
/
iu.py
748 lines (674 loc) · 25.4 KB
/
iu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#
# Copyright (C) 2005, Giovanni Bajo
#
# Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# In addition to the permissions in the GNU General Public License, the
# authors give you unlimited permission to link or embed the compiled
# version of this file into combinations with other programs, and to
# distribute those combinations without any restriction coming from the
# use of this file. (The General Public License restrictions do apply in
# other respects; for example, they cover modification of the file, and
# distribution when not linked into a combine executable.)
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
#
# **NOTE** This module is used during bootstrap. Import *ONLY* builtin modules.
#
import sys
import imp
import marshal
try:
py_version = sys.version_info
except AttributeError:
py_version = (1,5)
try:
# zipimport is supported starting with Python 2.3
import zipimport
except ImportError:
zipimport = None
try:
STRINGTYPE = basestring
except NameError:
STRINGTYPE = type("")
def debug(msg):
if 0:
sys.stderr.write(msg+"\n")
#=======================Owners==========================#
# An Owner does imports from a particular piece of turf
# That is, there's an Owner for each thing on sys.path
# There are owners for directories and .pyz files.
# There could be owners for zip files, or even URLs.
# A shadowpath (a dictionary mapping the names in
# sys.path to their owners) is used so that sys.path
# (or a package's __path__) is still a bunch of strings,
class OwnerError(IOError):
def __str__(self):
return "<OwnerError %s>" % self.message
class Owner:
def __init__(self, path):
self.path = path
def __str__(self):
return self.path
def getmod(self, nm):
return None
class DirOwner(Owner):
def __init__(self, path):
if path == '':
path = _os_getcwd()
if not pathisdir(path):
raise OwnerError("%s is not a directory" % path)
Owner.__init__(self, path)
def getmod(self, nm, getsuffixes=imp.get_suffixes,
loadco=marshal.loads, newmod=imp.new_module):
pth = _os_path_join(self.path, nm)
possibles = [(pth, 0, None)]
if pathisdir(pth) and caseOk(pth):
possibles.insert(0, (_os_path_join(pth, '__init__'), 1, pth))
py = pyc = None
for pth, ispkg, pkgpth in possibles:
for ext, mode, typ in getsuffixes():
attempt = pth+ext
try:
st = _os_stat(attempt)
except OSError, e:
assert e.errno == 2 #[Errno 2] No such file or directory
else:
# Check case
if not caseOk(attempt):
continue
if typ == imp.C_EXTENSION:
fp = open(attempt, 'rb')
mod = imp.load_module(nm, fp, attempt, (ext, mode, typ))
mod.__file__ = attempt
return mod
elif typ == imp.PY_SOURCE:
py = (attempt, st)
else:
pyc = (attempt, st)
if py or pyc:
break
if py is None and pyc is None:
return None
while 1:
if pyc is None or py and pyc[1][8] < py[1][8]:
try:
co = compile(open(py[0], 'r').read()+'\n', py[0], 'exec')
break
except SyntaxError, e:
print "Invalid syntax in %s" % py[0]
print e.args
raise
elif pyc:
stuff = open(pyc[0], 'rb').read()
try:
co = loadco(stuff[8:])
break
except (ValueError, EOFError):
pyc = None
else:
return None
mod = newmod(nm)
mod.__file__ = co.co_filename
if ispkg:
mod.__path__ = [pkgpth]
subimporter = PathImportDirector(mod.__path__)
mod.__importsub__ = subimporter.getmod
mod.__co__ = co
return mod
ZipOwner = None
if zipimport:
class ZipOwner(Owner):
def __init__(self, path):
try:
self.__zip = zipimport.zipimporter(path)
except zipimport.ZipImportError, e:
raise OwnerError('%s: %s' % (str(e), path))
Owner.__init__(self, path)
def getmod(self, nm, newmod=imp.new_module):
# We cannot simply use zipimport.load_module here
# because it both loads (= create module object)
# and imports (= execute bytecode). Instead, our
# getmod() functions are supposed to only load the modules.
# Note that imp.load_module() does the right thing, instead.
debug('zipimport try: %s within %s' % (nm, self.__zip))
try:
co = self.__zip.get_code(nm)
mod = newmod(nm)
mod.__file__ = co.co_filename
if self.__zip.is_package(nm):
mod.__path__ = [_os_path_join(self.path, nm)]
subimporter = PathImportDirector(mod.__path__)
mod.__importsub__ = subimporter.getmod
if self.path.endswith(".egg"):
# Fixup some additional special attribute so that
# pkg_resources works correctly.
# TODO: couldn't we fix these attributes always,
# for all zip files?
mod.__file__ = _os_path_join(
_os_path_join(self.path, nm), "__init__.py")
mod.__loader__ = self.__zip
mod.__co__ = co
return mod
except zipimport.ZipImportError:
debug('zipimport not found %s' % nm)
return None
# _mountzlib.py will insert archive.PYZOwner in front later
_globalownertypes = filter(None, [
ZipOwner,
DirOwner,
Owner,
])
#===================Import Directors====================================#
# ImportDirectors live on the metapath
# There's one for builtins, one for frozen modules, and one for sys.path
# Windows gets one for modules gotten from the Registry
# Mac would have them for PY_RESOURCE modules etc.
# A generalization of Owner - their concept of "turf" is broader
class ImportDirector(Owner):
pass
class BuiltinImportDirector(ImportDirector):
def __init__(self):
self.path = 'Builtins'
def getmod(self, nm, isbuiltin=imp.is_builtin):
if isbuiltin(nm):
mod = imp.load_module(nm, None, nm, ('','',imp.C_BUILTIN))
return mod
return None
class FrozenImportDirector(ImportDirector):
def __init__(self):
self.path = 'FrozenModules'
def getmod(self, nm, isfrozen=imp.is_frozen):
if isfrozen(nm):
mod = imp.load_module(nm, None, nm, ('','',imp.PY_FROZEN))
if hasattr(mod, '__path__'):
mod.__importsub__ = lambda name, pname=nm, owner=self: owner.getmod(pname+'.'+name)
return mod
return None
class RegistryImportDirector(ImportDirector):
# for Windows only
def __init__(self):
self.path = "WindowsRegistry"
self.map = {}
try:
import win32api
## import win32con
except ImportError:
pass
else:
HKEY_CURRENT_USER = -2147483647
HKEY_LOCAL_MACHINE = -2147483646
KEY_ALL_ACCESS = 983103
KEY_READ = 131097
subkey = r"Software\Python\PythonCore\%s\Modules" % sys.winver
for root in (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE):
try:
#hkey = win32api.RegOpenKeyEx(root, subkey, 0, KEY_ALL_ACCESS)
hkey = win32api.RegOpenKeyEx(root, subkey, 0, KEY_READ)
except Exception:
# If the key does not exist, simply try the next one.
pass
else:
numsubkeys, numvalues, lastmodified = win32api.RegQueryInfoKey(hkey)
for i in range(numsubkeys):
subkeyname = win32api.RegEnumKey(hkey, i)
#hskey = win32api.RegOpenKeyEx(hkey, subkeyname, 0, KEY_ALL_ACCESS)
hskey = win32api.RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ)
val = win32api.RegQueryValueEx(hskey, '')
desc = getDescr(val[0])
self.map[subkeyname] = (val[0], desc)
hskey.Close()
hkey.Close()
break
def getmod(self, nm):
stuff = self.map.get(nm)
if stuff:
fnm, desc = stuff
fp = open(fnm, 'rb')
mod = imp.load_module(nm, fp, fnm, desc)
mod.__file__ = fnm
return mod
return None
class PathImportDirector(ImportDirector):
def __init__(self, pathlist=None, importers=None, ownertypes=None):
self.path = pathlist
if ownertypes == None:
self.ownertypes = _globalownertypes
else:
self.ownertypes = ownertypes
if importers:
self.shadowpath = importers
else:
self.shadowpath = {}
self.inMakeOwner = 0
self.building = {}
def __str__(self):
return str(self.path or sys.path)
def getmod(self, nm):
mod = None
for thing in (self.path or sys.path):
if isinstance(thing, STRINGTYPE):
owner = self.shadowpath.get(thing, -1)
if owner == -1:
owner = self.shadowpath[thing] = self.makeOwner(thing)
if owner:
mod = owner.getmod(nm)
else:
mod = thing.getmod(nm)
if mod:
break
return mod
def makeOwner(self, path):
if self.building.get(path):
return None
self.building[path] = 1
owner = None
for klass in self.ownertypes:
try:
# this may cause an import, which may cause recursion
# hence the protection
owner = klass(path)
except OwnerError, e:
pass
else:
break
del self.building[path]
return owner
def getDescr(fnm):
ext = getpathext(fnm)
for (suffix, mode, typ) in imp.get_suffixes():
if suffix == ext:
return (suffix, mode, typ)
#=================ImportManager============================#
# The one-and-only ImportManager
# ie, the builtin import
UNTRIED = -1
class ImportManagerException(Exception):
def __init__(self, args):
self.args = args
def __repr__(self):
return "<%s: %s>" % (self.__name__, self.args)
class ImportManager:
# really the equivalent of builtin import
def __init__(self):
self.metapath = [
BuiltinImportDirector(),
FrozenImportDirector(),
RegistryImportDirector(),
PathImportDirector()
]
self.threaded = 0
self.rlock = None
self.locker = None
self.setThreaded()
def setThreaded(self):
thread = sys.modules.get('thread', None)
if thread and not self.threaded:
#debug("iu setting threaded")
self.threaded = 1
self.rlock = thread.allocate_lock()
self._get_ident = thread.get_ident
def install(self):
import __builtin__
__builtin__.__import__ = self.importHook
__builtin__.reload = self.reloadHook
def importHook(self, name, globals=None, locals=None, fromlist=None, level=-1):
__globals_name = None
if globals:
__globals_name = globals.get('__name__')
# first see if we could be importing a relative name
debug("importHook(%s, %s, locals, %s, %s)" % (name, __globals_name, fromlist, level))
_sys_modules_get = sys.modules.get
_self_doimport = self.doimport
threaded = self.threaded
# break the name being imported up so we get:
# a.b.c -> [a, b, c]
nmparts = namesplit(name)
if not globals:
contexts = [None]
if level > 0:
raise RuntimeError("Relative import requires 'globals'")
elif level == 0:
# absolute import, do not try relative
contexts = [None]
else: # level != 0
importernm = globals.get('__name__', '')
ispkg = hasattr(_sys_modules_get(importernm), '__path__')
debug('importernm %s' % importernm)
if level < 0:
# behaviour up to Python 2.4 (and default in Python 2.5)
# add the package to searched contexts
contexts = [None]
else:
# relative import, do not try absolute
if not importernm:
raise RuntimeError("Relative import requires package")
# level=1 => current package
# level=2 => previous package => drop 1 level
if level > 1:
importernm = _string_split(importernm, '.')[:-level+1]
importernm = _string_join('.', importernm)
contexts = [None]
if importernm:
if ispkg:
# If you use the "from __init__ import" syntax, the package
# name will have a __init__ in it. We want to strip it.
if importernm[-len(".__init__"):] == ".__init__":
importernm = importernm[:-len(".__init__")]
contexts.insert(0,importernm)
else:
pkgnm = packagename(importernm)
if pkgnm:
contexts.insert(0, pkgnm)
# so contexts is [pkgnm, None], [pkgnm] or just [None]
for context in contexts:
ctx = context
i = 0
for i in range(len(nmparts)):
nm = nmparts[i]
debug(" importHook trying %s in %s" % (nm, ctx))
if ctx:
fqname = ctx + '.' + nm
else:
fqname = nm
if threaded:
self._acquire()
try:
mod = _sys_modules_get(fqname, UNTRIED)
if mod is UNTRIED:
debug('trying %s %s %s' % (nm, ctx, fqname))
mod = _self_doimport(nm, ctx, fqname)
finally:
if threaded:
self._release()
if mod:
ctx = fqname
else:
break
else:
# no break, point i beyond end
i = i + 1
if i:
break
if i<len(nmparts):
if ctx and hasattr(sys.modules[ctx], nmparts[i]):
debug("importHook done with %s %s %s (case 1)" % (name, __globals_name, fromlist))
return sys.modules[nmparts[0]]
del sys.modules[fqname]
raise ImportError, "No module named %s" % fqname
if not fromlist:
debug("importHook done with %s %s %s (case 2)" % (name, __globals_name, fromlist))
if context:
return sys.modules[context+'.'+nmparts[0]]
return sys.modules[nmparts[0]]
bottommod = sys.modules[ctx]
if hasattr(bottommod, '__path__'):
fromlist = list(fromlist)
i = 0
while i < len(fromlist):
nm = fromlist[i]
if nm == '*':
fromlist[i:i+1] = list(getattr(bottommod, '__all__', []))
if i >= len(fromlist):
break
nm = fromlist[i]
i = i + 1
if not hasattr(bottommod, nm):
if threaded:
self._acquire()
try:
mod = self.doimport(nm, ctx, ctx+'.'+nm)
finally:
if threaded:
self._release()
debug("importHook done with %s %s %s (case 3)" % (name, __globals_name, fromlist))
return bottommod
def doimport(self, nm, parentnm, fqname, reload=0):
# Not that nm is NEVER a dotted name at this point
debug("doimport(%s, %s, %s)" % (nm, parentnm, fqname))
if parentnm:
parent = sys.modules[parentnm]
if hasattr(parent, '__path__'):
importfunc = getattr(parent, '__importsub__', None)
if not importfunc:
subimporter = PathImportDirector(parent.__path__)
importfunc = parent.__importsub__ = subimporter.getmod
debug("using parent's importfunc: %s" % importfunc)
mod = importfunc(nm)
if mod and not reload:
setattr(parent, nm, mod)
else:
debug("..parent not a package")
return None
else:
parent = None
# now we're dealing with an absolute import
for director in self.metapath:
mod = director.getmod(nm)
if mod:
break
if mod:
mod.__name__ = fqname
if reload:
sys.modules[fqname].__dict__.update(mod.__dict__)
else:
sys.modules[fqname] = mod
if hasattr(mod, '__co__'):
co = mod.__co__
del mod.__co__
try:
if reload:
exec co in sys.modules[fqname].__dict__
else:
exec co in mod.__dict__
except:
# In Python 2.4 and above, sys.modules is left clean
# after a broken import. We need to do the same to
# achieve perfect compatibility (see ticket #32).
if py_version >= (2,4,0):
# FIXME: how can we recover from a broken reload()?
# Should we save the mod dict and restore it in case
# of failure?
if not reload:
# Some modules (eg: dbhash.py) cleanup
# sys.modules themselves. We should then
# be lenient and avoid errors.
sys.modules.pop(fqname, None)
if hasattr(parent, nm):
delattr(parent, nm)
raise
if fqname == 'thread' and not self.threaded:
#debug("thread detected!")
self.setThreaded()
else:
sys.modules[fqname] = None
debug("..found %s when looking for %s" % (mod, fqname))
return mod
def reloadHook(self, mod):
fqnm = mod.__name__
nm = namesplit(fqnm)[-1]
parentnm = packagename(fqnm)
newmod = self.doimport(nm, parentnm, fqnm, reload=1)
#mod.__dict__.update(newmod.__dict__)
return newmod
def _acquire(self):
if self.rlock.locked():
if self.locker == self._get_ident():
self.lockcount = self.lockcount + 1
#debug("_acquire incrementing lockcount to %s" % self.lockcount)
return
self.rlock.acquire()
self.locker = self._get_ident()
self.lockcount = 0
#debug("_acquire first time!")
def _release(self):
if self.lockcount:
self.lockcount = self.lockcount - 1
#debug("_release decrementing lockcount to %s" % self.lockcount)
else:
self.locker = None
self.rlock.release()
#debug("_release releasing lock!")
#========= some helper functions =============================#
def packagename(s):
for i in range(len(s)-1, -1, -1):
if s[i] == '.':
break
else:
return ''
return s[:i]
def namesplit(s):
rslt = []
i = j = 0
for j in range(len(s)):
if s[j] == '.':
rslt.append(s[i:j])
i = j+1
if i < len(s):
rslt.append(s[i:])
return rslt
def getpathext(fnm):
for i in range(len(fnm)-1, -1, -1):
if fnm[i] == '.':
return fnm[i:]
return ''
def pathisdir(pathname):
"Local replacement for os.path.isdir()."
try:
s = _os_stat(pathname)
except OSError:
return None
return (s[0] & 0170000) == 0040000
_os_stat = _os_path_join = _os_getcwd = _os_path_dirname = _os_environ = _os_listdir = _os_path_basename = None
def _os_bootstrap():
"Set up 'os' module replacement functions for use during import bootstrap."
global _os_stat, _os_path_join, _os_path_dirname, _os_getcwd, _os_environ, _os_listdir, _os_path_basename
names = sys.builtin_module_names
join = dirname = environ = listdir = basename = None
mindirlen = 0
if 'posix' in names:
from posix import stat, getcwd, environ, listdir
sep = '/'
mindirlen = 1
elif 'nt' in names:
from nt import stat, getcwd, environ, listdir
sep = '\\'
mindirlen = 3
elif 'dos' in names:
from dos import stat, getcwd, environ, listdir
sep = '\\'
mindirlen = 3
elif 'os2' in names:
from os2 import stat, getcwd, environ, listdir
sep = '\\'
elif 'mac' in names:
from mac import stat, getcwd, environ, listdir
def join(a, b):
if a == '':
return b
path = s
if ':' not in a:
a = ':' + a
if a[-1:] != ':':
a = a + ':'
return a + b
else:
raise ImportError, 'no os specific module found'
if join is None:
def join(a, b, sep=sep):
if a == '':
return b
lastchar = a[-1:]
if lastchar == '/' or lastchar == sep:
return a + b
return a + sep + b
if dirname is None:
def dirname(a, sep=sep, mindirlen=mindirlen):
for i in range(len(a)-1, -1, -1):
c = a[i]
if c == '/' or c == sep:
if i < mindirlen:
return a[:i+1]
return a[:i]
return ''
if basename is None:
def basename(p):
i = p.rfind(sep)
if i == -1:
return p
else:
return p[i + len(sep):]
def _listdir(dir, cache={}):
# since this function is only used by caseOk, it's fine to cache the
# results and avoid reading the whole contents of a directory each time
# we just want to check the case of a filename.
if not dir in cache:
cache[dir] = listdir(dir)
return cache[dir]
_os_stat = stat
_os_getcwd = getcwd
_os_path_join = join
_os_path_dirname = dirname
_os_environ = environ
_os_listdir = _listdir
_os_path_basename = basename
_string_replace = _string_join = _string_split = None
def _string_bootstrap():
"""
Set up 'string' module replacement functions for use during import bootstrap.
During bootstrap, we can use only builtin modules since import does not work
yet. For Python 2.0+, we can use string methods so this is not a problem.
For Python 1.5, we would need the string module, so we need replacements.
"""
global _string_replace, _string_join, _string_split
def join(sep, words):
res = ''
for w in words:
res = res + (sep + w)
return res[len(sep):]
def split(s, sep, maxsplit=0):
res = []
nsep = len(sep)
if nsep == 0:
return [s]
ns = len(s)
if maxsplit <= 0: maxsplit = ns
i = j = 0
count = 0
while j+nsep <= ns:
if s[j:j+nsep] == sep:
count = count + 1
res.append(s[i:j])
i = j = j + nsep
if count >= maxsplit: break
else:
j = j + 1
res.append(s[i:])
return res
def replace(str, old, new):
return _string_join(new, _string_split(str, old))
_string_join = getattr(STRINGTYPE, "join", join)
_string_split = getattr(STRINGTYPE, "split", split)
_string_replace = getattr(STRINGTYPE, "replace", replace)
_os_bootstrap()
if not _os_environ.has_key('PYTHONCASEOK') and sys.version_info >= (2, 1):
def caseOk(filename):
files = _os_listdir(_os_path_dirname(filename))
return _os_path_basename(filename) in files
else:
def caseOk(filename):
return True
_string_bootstrap()