home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_630 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  16.9 KB  |  706 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import generators
  5. import dis
  6. import imp
  7. import marshal
  8. import os
  9. import sys
  10. import types
  11. import struct
  12. if hasattr(sys.__stdout__, 'newlines'):
  13.     READ_MODE = 'U'
  14. else:
  15.     READ_MODE = 'r'
  16. LOAD_CONST = chr(dis.opname.index('LOAD_CONST'))
  17. IMPORT_NAME = chr(dis.opname.index('IMPORT_NAME'))
  18. STORE_NAME = chr(dis.opname.index('STORE_NAME'))
  19. STORE_GLOBAL = chr(dis.opname.index('STORE_GLOBAL'))
  20. STORE_OPS = [
  21.     STORE_NAME,
  22.     STORE_GLOBAL]
  23. HAVE_ARGUMENT = chr(dis.HAVE_ARGUMENT)
  24. packagePathMap = { }
  25.  
  26. def AddPackagePath(packagename, path):
  27.     paths = packagePathMap.get(packagename, [])
  28.     paths.append(path)
  29.     packagePathMap[packagename] = paths
  30.  
  31. replacePackageMap = { }
  32.  
  33. def ReplacePackage(oldname, newname):
  34.     replacePackageMap[oldname] = newname
  35.  
  36.  
  37. class Module:
  38.     
  39.     def __init__(self, name, file = None, path = None):
  40.         self.__name__ = name
  41.         self.__file__ = file
  42.         self.__path__ = path
  43.         self.__code__ = None
  44.         self.globalnames = { }
  45.         self.starimports = { }
  46.  
  47.     
  48.     def __repr__(self):
  49.         s = 'Module(%r' % (self.__name__,)
  50.         if self.__file__ is not None:
  51.             s = s + ', %r' % (self.__file__,)
  52.         
  53.         if self.__path__ is not None:
  54.             s = s + ', %r' % (self.__path__,)
  55.         
  56.         s = s + ')'
  57.         return s
  58.  
  59.  
  60.  
  61. class ModuleFinder:
  62.     
  63.     def __init__(self, path = None, debug = 0, excludes = [], replace_paths = []):
  64.         if path is None:
  65.             path = sys.path
  66.         
  67.         self.path = path
  68.         self.modules = { }
  69.         self.badmodules = { }
  70.         self.debug = debug
  71.         self.indent = 0
  72.         self.excludes = excludes
  73.         self.replace_paths = replace_paths
  74.         self.processed_paths = []
  75.  
  76.     
  77.     def msg(self, level, str, *args):
  78.         if level <= self.debug:
  79.             for i in range(self.indent):
  80.                 print '   ',
  81.             
  82.             print str,
  83.             for arg in args:
  84.                 print repr(arg),
  85.             
  86.             print 
  87.         
  88.  
  89.     
  90.     def msgin(self, *args):
  91.         level = args[0]
  92.         if level <= self.debug:
  93.             self.indent = self.indent + 1
  94.             self.msg(*args)
  95.         
  96.  
  97.     
  98.     def msgout(self, *args):
  99.         level = args[0]
  100.         if level <= self.debug:
  101.             self.indent = self.indent - 1
  102.             self.msg(*args)
  103.         
  104.  
  105.     
  106.     def run_script(self, pathname):
  107.         self.msg(2, 'run_script', pathname)
  108.         fp = open(pathname, READ_MODE)
  109.         stuff = ('', 'r', imp.PY_SOURCE)
  110.         self.load_module('__main__', fp, pathname, stuff)
  111.  
  112.     
  113.     def load_file(self, pathname):
  114.         (dir, name) = os.path.split(pathname)
  115.         (name, ext) = os.path.splitext(name)
  116.         fp = open(pathname, READ_MODE)
  117.         stuff = (ext, 'r', imp.PY_SOURCE)
  118.         self.load_module(name, fp, pathname, stuff)
  119.  
  120.     
  121.     def import_hook(self, name, caller = None, fromlist = None, level = -1):
  122.         self.msg(3, 'import_hook', name, caller, fromlist, level)
  123.         parent = self.determine_parent(caller, level = level)
  124.         (q, tail) = self.find_head_package(parent, name)
  125.         m = self.load_tail(q, tail)
  126.         if not fromlist:
  127.             return q
  128.         if m.__path__:
  129.             self.ensure_fromlist(m, fromlist)
  130.         
  131.  
  132.     
  133.     def determine_parent(self, caller, level = -1):
  134.         self.msgin(4, 'determine_parent', caller, level)
  135.         if not caller or level == 0:
  136.             self.msgout(4, 'determine_parent -> None')
  137.             return None
  138.         pname = caller.__name__
  139.         if level >= 1:
  140.             if caller.__path__:
  141.                 level -= 1
  142.             
  143.             if level == 0:
  144.                 parent = self.modules[pname]
  145.                 self.msgout(4, 'determine_parent ->', parent)
  146.                 return parent
  147.             if pname.count('.') < level:
  148.                 raise ImportError, 'relative importpath too deep'
  149.             pname.count('.') < level
  150.             pname = '.'.join(pname.split('.')[:-level])
  151.             parent = self.modules[pname]
  152.             self.msgout(4, 'determine_parent ->', parent)
  153.             return parent
  154.         if caller.__path__:
  155.             parent = self.modules[pname]
  156.             self.msgout(4, 'determine_parent ->', parent)
  157.             return parent
  158.         if '.' in pname:
  159.             i = pname.rfind('.')
  160.             pname = pname[:i]
  161.             parent = self.modules[pname]
  162.             self.msgout(4, 'determine_parent ->', parent)
  163.             return parent
  164.         self.msgout(4, 'determine_parent -> None')
  165.  
  166.     
  167.     def find_head_package(self, parent, name):
  168.         self.msgin(4, 'find_head_package', parent, name)
  169.         if '.' in name:
  170.             i = name.find('.')
  171.             head = name[:i]
  172.             tail = name[i + 1:]
  173.         else:
  174.             head = name
  175.             tail = ''
  176.         if parent:
  177.             qname = '%s.%s' % (parent.__name__, head)
  178.         else:
  179.             qname = head
  180.         q = self.import_module(head, qname, parent)
  181.         if q:
  182.             self.msgout(4, 'find_head_package ->', (q, tail))
  183.             return (q, tail)
  184.         self.msgout(4, 'raise ImportError: No module named', qname)
  185.         raise ImportError, 'No module named ' + qname
  186.  
  187.     
  188.     def load_tail(self, q, tail):
  189.         self.msgin(4, 'load_tail', q, tail)
  190.         m = q
  191.         while tail:
  192.             i = tail.find('.')
  193.             if i < 0:
  194.                 i = len(tail)
  195.             
  196.             head = tail[:i]
  197.             tail = tail[i + 1:]
  198.             mname = '%s.%s' % (m.__name__, head)
  199.             m = self.import_module(head, mname, m)
  200.             if not m:
  201.                 self.msgout(4, 'raise ImportError: No module named', mname)
  202.                 raise ImportError, 'No module named ' + mname
  203.             m
  204.         self.msgout(4, 'load_tail ->', m)
  205.         return m
  206.  
  207.     
  208.     def ensure_fromlist(self, m, fromlist, recursive = 0):
  209.         self.msg(4, 'ensure_fromlist', m, fromlist, recursive)
  210.         for sub in fromlist:
  211.             if sub == '*':
  212.                 if not recursive:
  213.                     all = self.find_all_submodules(m)
  214.                     if all:
  215.                         self.ensure_fromlist(m, all, 1)
  216.                     
  217.                 
  218.             recursive
  219.             if not hasattr(m, sub):
  220.                 subname = '%s.%s' % (m.__name__, sub)
  221.                 submod = self.import_module(sub, subname, m)
  222.                 if not submod:
  223.                     raise ImportError, 'No module named ' + subname
  224.                 submod
  225.                 continue
  226.         
  227.  
  228.     
  229.     def find_all_submodules(self, m):
  230.         if not m.__path__:
  231.             return None
  232.         modules = { }
  233.         suffixes = []
  234.         for triple in imp.get_suffixes():
  235.             suffixes.append(triple[0])
  236.         
  237.         for dir in m.__path__:
  238.             
  239.             try:
  240.                 names = os.listdir(dir)
  241.             except os.error:
  242.                 m.__path__
  243.                 m.__path__
  244.                 self.msg(2, "can't list directory", dir)
  245.                 continue
  246.             except:
  247.                 m.__path__
  248.  
  249.             for name in names:
  250.                 mod = None
  251.                 for suff in suffixes:
  252.                     n = len(suff)
  253.                     if name[-n:] == suff:
  254.                         mod = name[:-n]
  255.                         break
  256.                         continue
  257.                     m.__path__
  258.                 
  259.                 if mod and mod != '__init__':
  260.                     modules[mod] = mod
  261.                     continue
  262.             
  263.         
  264.         return modules.keys()
  265.  
  266.     
  267.     def import_module(self, partname, fqname, parent):
  268.         self.msgin(3, 'import_module', partname, fqname, parent)
  269.         
  270.         try:
  271.             m = self.modules[fqname]
  272.         except KeyError:
  273.             pass
  274.  
  275.         self.msgout(3, 'import_module ->', m)
  276.         return m
  277.         if fqname in self.badmodules:
  278.             self.msgout(3, 'import_module -> None')
  279.             return None
  280.         if parent and parent.__path__ is None:
  281.             self.msgout(3, 'import_module -> None')
  282.             return None
  283.         
  284.         try:
  285.             if parent:
  286.                 pass
  287.             (fp, pathname, stuff) = self.find_module(partname, parent.__path__, parent)
  288.         except ImportError:
  289.             parent.__path__ is None
  290.             parent.__path__ is None
  291.             fqname in self.badmodules
  292.             self.msgout(3, 'import_module ->', None)
  293.             return None
  294.  
  295.         
  296.         try:
  297.             m = self.load_module(fqname, fp, pathname, stuff)
  298.         finally:
  299.             pass
  300.  
  301.         self.msgout(3, 'import_module ->', m)
  302.         return m
  303.  
  304.     
  305.     def load_module(self, fqname, fp, pathname, file_info):
  306.         (suffix, mode, type) = file_info
  307.         if fp:
  308.             pass
  309.         self.msgin(2, 'load_module', fqname, 'fp', pathname)
  310.         if type == imp.PKG_DIRECTORY:
  311.             m = self.load_package(fqname, pathname)
  312.             self.msgout(2, 'load_module ->', m)
  313.             return m
  314.         if type == imp.PY_SOURCE:
  315.             co = compile(fp.read() + '\n', pathname, 'exec')
  316.         elif type == imp.PY_COMPILED:
  317.             if fp.read(4) != imp.get_magic():
  318.                 self.msgout(2, 'raise ImportError: Bad magic number', pathname)
  319.                 raise ImportError, 'Bad magic number in %s' % pathname
  320.             fp.read(4) != imp.get_magic()
  321.             fp.read(4)
  322.             co = marshal.load(fp)
  323.         else:
  324.             co = None
  325.         m = self.add_module(fqname)
  326.         m.__file__ = pathname
  327.         if co:
  328.             if self.replace_paths:
  329.                 co = self.replace_paths_in_code(co)
  330.             
  331.             m.__code__ = co
  332.             self.scan_code(co, m)
  333.         
  334.         self.msgout(2, 'load_module ->', m)
  335.         return m
  336.  
  337.     
  338.     def _add_badmodule(self, name, caller):
  339.         if name not in self.badmodules:
  340.             self.badmodules[name] = { }
  341.         
  342.         if caller:
  343.             self.badmodules[name][caller.__name__] = 1
  344.         else:
  345.             self.badmodules[name]['-'] = 1
  346.  
  347.     
  348.     def _safe_import_hook(self, name, caller, fromlist, level = -1):
  349.         if name in self.badmodules:
  350.             self._add_badmodule(name, caller)
  351.             return None
  352.         
  353.         try:
  354.             self.import_hook(name, caller, level = level)
  355.         except ImportError:
  356.             name in self.badmodules
  357.             msg = name in self.badmodules
  358.             self.msg(2, 'ImportError:', str(msg))
  359.             self._add_badmodule(name, caller)
  360.         except:
  361.             name in self.badmodules
  362.  
  363.         if fromlist:
  364.             for sub in fromlist:
  365.                 if sub in self.badmodules:
  366.                     self._add_badmodule(sub, caller)
  367.                     continue
  368.                 
  369.                 
  370.                 try:
  371.                     self.import_hook(name, caller, [
  372.                         sub], level = level)
  373.                 continue
  374.                 except ImportError:
  375.                     msg = None
  376.                     self.msg(2, 'ImportError:', str(msg))
  377.                     fullname = name + '.' + sub
  378.                     self._add_badmodule(fullname, caller)
  379.                     continue
  380.                 
  381.  
  382.             
  383.         
  384.  
  385.     
  386.     def scan_opcodes(self, co, unpack = struct.unpack):
  387.         code = co.co_code
  388.         names = co.co_names
  389.         consts = co.co_consts
  390.         while code:
  391.             c = code[0]
  392.             if c in STORE_OPS:
  393.                 (oparg,) = unpack('<H', code[1:3])
  394.                 yield ('store', (names[oparg],))
  395.                 code = code[3:]
  396.                 continue
  397.             
  398.             if c == LOAD_CONST and code[3] == IMPORT_NAME:
  399.                 (oparg_1, oparg_2) = unpack('<xHxH', code[:6])
  400.                 yield ('import', (consts[oparg_1], names[oparg_2]))
  401.                 code = code[6:]
  402.                 continue
  403.             
  404.             if c >= HAVE_ARGUMENT:
  405.                 code = code[3:]
  406.                 continue
  407.             code = code[1:]
  408.  
  409.     
  410.     def scan_opcodes_25(self, co, unpack = struct.unpack):
  411.         code = co.co_code
  412.         names = co.co_names
  413.         consts = co.co_consts
  414.         LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
  415.         while code:
  416.             c = code[0]
  417.             if c in STORE_OPS:
  418.                 (oparg,) = unpack('<H', code[1:3])
  419.                 yield ('store', (names[oparg],))
  420.                 code = code[3:]
  421.                 continue
  422.             
  423.             if code[:9:3] == LOAD_LOAD_AND_IMPORT:
  424.                 (oparg_1, oparg_2, oparg_3) = unpack('<xHxHxH', code[:9])
  425.                 level = consts[oparg_1]
  426.                 if level == -1:
  427.                     yield ('import', (consts[oparg_2], names[oparg_3]))
  428.                 elif level == 0:
  429.                     yield ('absolute_import', (consts[oparg_2], names[oparg_3]))
  430.                 else:
  431.                     yield ('relative_import', (level, consts[oparg_2], names[oparg_3]))
  432.                 code = code[9:]
  433.                 continue
  434.             
  435.             if c >= HAVE_ARGUMENT:
  436.                 code = code[3:]
  437.                 continue
  438.             code = code[1:]
  439.  
  440.     
  441.     def scan_code(self, co, m):
  442.         code = co.co_code
  443.         if sys.version_info >= (2, 5):
  444.             scanner = self.scan_opcodes_25
  445.         else:
  446.             scanner = self.scan_opcodes
  447.         for what, args in scanner(co):
  448.             if what == 'store':
  449.                 (name,) = args
  450.                 m.globalnames[name] = 1
  451.                 continue
  452.             if what in ('import', 'absolute_import'):
  453.                 (fromlist, name) = args
  454.                 have_star = 0
  455.                 if what == 'absolute_import':
  456.                     level = 0
  457.                 else:
  458.                     level = -1
  459.                 self._safe_import_hook(name, m, fromlist, level = level)
  460.                 if have_star:
  461.                     mm = None
  462.                     if m.__path__:
  463.                         mm = self.modules.get(m.__name__ + '.' + name)
  464.                     
  465.                     if mm is None:
  466.                         mm = self.modules.get(name)
  467.                     
  468.                     if mm is not None:
  469.                         m.globalnames.update(mm.globalnames)
  470.                         m.starimports.update(mm.starimports)
  471.                         if mm.__code__ is None:
  472.                             m.starimports[name] = 1
  473.                         
  474.                     else:
  475.                         m.starimports[name] = 1
  476.                 
  477.             have_star
  478.             if what == 'relative_import':
  479.                 (level, fromlist, name) = args
  480.                 if name:
  481.                     self._safe_import_hook(name, m, fromlist, level = level)
  482.                 else:
  483.                     parent = self.determine_parent(m, level = level)
  484.                     self._safe_import_hook(parent.__name__, None, fromlist, level = 0)
  485.             name
  486.             raise RuntimeError(what)
  487.         
  488.         for c in co.co_consts:
  489.             if isinstance(c, type(co)):
  490.                 self.scan_code(c, m)
  491.                 continue
  492.         
  493.  
  494.     
  495.     def load_package(self, fqname, pathname):
  496.         self.msgin(2, 'load_package', fqname, pathname)
  497.         newname = replacePackageMap.get(fqname)
  498.         if newname:
  499.             fqname = newname
  500.         
  501.         m = self.add_module(fqname)
  502.         m.__file__ = pathname
  503.         m.__path__ = [
  504.             pathname]
  505.         m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
  506.         (fp, buf, stuff) = self.find_module('__init__', m.__path__)
  507.         self.load_module(fqname, fp, buf, stuff)
  508.         self.msgout(2, 'load_package ->', m)
  509.         return m
  510.  
  511.     
  512.     def add_module(self, fqname):
  513.         if fqname in self.modules:
  514.             return self.modules[fqname]
  515.         return m
  516.  
  517.     
  518.     def find_module(self, name, path, parent = None):
  519.         if parent is not None:
  520.             fullname = parent.__name__ + '.' + name
  521.         else:
  522.             fullname = name
  523.         if fullname in self.excludes:
  524.             self.msgout(3, 'find_module -> Excluded', fullname)
  525.             raise ImportError, name
  526.         fullname in self.excludes
  527.         if path is None:
  528.             if name in sys.builtin_module_names:
  529.                 return (None, None, ('', '', imp.C_BUILTIN))
  530.             path = self.path
  531.         
  532.         return imp.find_module(name, path)
  533.  
  534.     
  535.     def report(self):
  536.         print 
  537.         print '  %-25s %s' % ('Name', 'File')
  538.         print '  %-25s %s' % ('----', '----')
  539.         keys = self.modules.keys()
  540.         keys.sort()
  541.         for key in keys:
  542.             m = self.modules[key]
  543.             if m.__path__:
  544.                 print 'P',
  545.             else:
  546.                 print 'm',
  547.             print '%-25s' % key,
  548.             if not m.__file__:
  549.                 pass
  550.             print ''
  551.         
  552.         (missing, maybe) = self.any_missing_maybe()
  553.         if missing:
  554.             print 
  555.             print 'Missing modules:'
  556.             for name in missing:
  557.                 mods = self.badmodules[name].keys()
  558.                 mods.sort()
  559.                 print '?', name, 'imported from', ', '.join(mods)
  560.             
  561.         
  562.         if maybe:
  563.             print 
  564.             print 'Submodules thay appear to be missing, but could also be', 'global names in the parent package:'
  565.             for name in maybe:
  566.                 mods = self.badmodules[name].keys()
  567.                 mods.sort()
  568.                 print '?', name, 'imported from', ', '.join(mods)
  569.             
  570.         
  571.  
  572.     
  573.     def any_missing(self):
  574.         (missing, maybe) = self.any_missing_maybe()
  575.         return missing + maybe
  576.  
  577.     
  578.     def any_missing_maybe(self):
  579.         missing = []
  580.         maybe = []
  581.         for name in self.badmodules:
  582.             if name in self.excludes:
  583.                 continue
  584.             
  585.             i = name.rfind('.')
  586.             if i < 0:
  587.                 missing.append(name)
  588.                 continue
  589.             
  590.             subname = name[i + 1:]
  591.             pkgname = name[:i]
  592.             pkg = self.modules.get(pkgname)
  593.             if pkg is not None:
  594.                 if pkgname in self.badmodules[name]:
  595.                     missing.append(name)
  596.                 elif subname in pkg.globalnames:
  597.                     pass
  598.                 elif pkg.starimports:
  599.                     maybe.append(name)
  600.                 else:
  601.                     missing.append(name)
  602.             pkgname in self.badmodules[name]
  603.             missing.append(name)
  604.         
  605.         missing.sort()
  606.         maybe.sort()
  607.         return (missing, maybe)
  608.  
  609.     
  610.     def replace_paths_in_code(self, co):
  611.         new_filename = original_filename = os.path.normpath(co.co_filename)
  612.         for f, r in self.replace_paths:
  613.             if original_filename.startswith(f):
  614.                 new_filename = r + original_filename[len(f):]
  615.                 break
  616.                 continue
  617.         
  618.         if self.debug and original_filename not in self.processed_paths:
  619.             if new_filename != original_filename:
  620.                 self.msgout(2, 'co_filename %r changed to %r' % (original_filename, new_filename))
  621.             else:
  622.                 self.msgout(2, 'co_filename %r remains unchanged' % (original_filename,))
  623.             self.processed_paths.append(original_filename)
  624.         
  625.         consts = list(co.co_consts)
  626.         for i in range(len(consts)):
  627.             if isinstance(consts[i], type(co)):
  628.                 consts[i] = self.replace_paths_in_code(consts[i])
  629.                 continue
  630.         
  631.         return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code, tuple(consts), co.co_names, co.co_varnames, new_filename, co.co_name, co.co_firstlineno, co.co_lnotab, co.co_freevars, co.co_cellvars)
  632.  
  633.  
  634.  
  635. def test():
  636.     import getopt
  637.     
  638.     try:
  639.         (opts, args) = getopt.getopt(sys.argv[1:], 'dmp:qx:')
  640.     except getopt.error:
  641.         msg = None
  642.         print msg
  643.         return None
  644.  
  645.     debug = 1
  646.     domods = 0
  647.     addpath = []
  648.     exclude = []
  649.     for o, a in opts:
  650.         if o == '-d':
  651.             debug = debug + 1
  652.         
  653.         if o == '-m':
  654.             domods = 1
  655.         
  656.         if o == '-p':
  657.             addpath = addpath + a.split(os.pathsep)
  658.         
  659.         if o == '-q':
  660.             debug = 0
  661.         
  662.         if o == '-x':
  663.             exclude.append(a)
  664.             continue
  665.     
  666.     if not args:
  667.         script = 'hello.py'
  668.     else:
  669.         script = args[0]
  670.     path = sys.path[:]
  671.     path[0] = os.path.dirname(script)
  672.     path = addpath + path
  673.     if debug > 1:
  674.         print 'path:'
  675.         for item in path:
  676.             print '   ', repr(item)
  677.         
  678.     
  679.     mf = ModuleFinder(path, debug, exclude)
  680.     for arg in args[1:]:
  681.         if arg == '-m':
  682.             domods = 1
  683.             continue
  684.         
  685.         if domods:
  686.             if arg[-2:] == '.*':
  687.                 mf.import_hook(arg[:-2], None, [
  688.                     '*'])
  689.             else:
  690.                 mf.import_hook(arg)
  691.         arg[-2:] == '.*'
  692.         mf.load_file(arg)
  693.     
  694.     mf.run_script(script)
  695.     mf.report()
  696.     return mf
  697.  
  698. if __name__ == '__main__':
  699.     
  700.     try:
  701.         mf = test()
  702.     except KeyboardInterrupt:
  703.         print '\n[interrupt]'
  704.  
  705.  
  706.