julenuri

Calibre site.py

Nov 19th, 2020 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
  3.  
  4. __license__   = 'GPL v3'
  5. __copyright__ = '2009, Kovid Goyal <[email protected]>'
  6. __docformat__ = 'restructuredtext en'
  7.  
  8. import sys
  9. import os
  10. import zipimport
  11. import _memimporter
  12.  
  13. DEBUG_ZIPIMPORT = False
  14.  
  15. class ZipExtensionImporter(zipimport.zipimporter):
  16.     '''
  17.    Taken, with thanks, from the py2exe source code
  18.    '''
  19.  
  20.     def __init__(self, *args, **kwargs):
  21.         zipimport.zipimporter.__init__(self, *args, **kwargs)
  22.         # We know there are no dlls in the zip file, so dont set findproc
  23.         # (performance optimization)
  24.         #_memimporter.set_find_proc(self.locate_dll_image)
  25.  
  26.     def find_module(self, fullname, path=None):
  27.         result = zipimport.zipimporter.find_module(self, fullname, path)
  28.         if result:
  29.             return result
  30.         fullname = fullname.replace(".", "\\")
  31.         if (fullname + '.pyd') in self._files:
  32.             return self
  33.         return None
  34.  
  35.     def locate_dll_image(self, name):
  36.         # A callback function for_memimporter.import_module.  Tries to
  37.         # locate additional dlls.  Returns the image as Python string,
  38.         # or None if not found.
  39.         if name in self._files:
  40.             return self.get_data(name)
  41.         return None
  42.  
  43.     def load_module(self, fullname):
  44.         if sys.modules.has_key(fullname):
  45.             mod = sys.modules[fullname]
  46.             if DEBUG_ZIPIMPORT:
  47.                 sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
  48.             return mod
  49.         try:
  50.             return zipimport.zipimporter.load_module(self, fullname)
  51.         except zipimport.ZipImportError:
  52.             pass
  53.         initname = "init" + fullname.split(".")[-1] # name of initfunction
  54.         filename = fullname.replace(".", "\\")
  55.         path = filename + '.pyd'
  56.         if path in self._files:
  57.             if DEBUG_ZIPIMPORT:
  58.                 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
  59.             code = self.get_data(path)
  60.             mod = _memimporter.import_module(code, initname, fullname, path)
  61.             mod.__file__ = "%s\\%s" % (self.archive, path)
  62.             mod.__loader__ = self
  63.             if DEBUG_ZIPIMPORT:
  64.                 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
  65.             return mod
  66.         raise zipimport.ZipImportError, "can't find module %s" % fullname
  67.  
  68.     def __repr__(self):
  69.         return "<%s object %r>" % (self.__class__.__name__, self.archive)
  70.  
  71.  
  72. def abs__file__():
  73.     """Set all module __file__ attribute to an absolute path"""
  74.     for m in sys.modules.values():
  75.         if hasattr(m, '__loader__'):
  76.             continue   # don't mess with a PEP 302-supplied __file__
  77.         try:
  78.             m.__file__ = os.path.abspath(m.__file__)
  79.         except AttributeError:
  80.             continue
  81.  
  82. def aliasmbcs():
  83.     import locale, codecs
  84.     enc = locale.getdefaultlocale()[1]
  85.     if enc.startswith('cp'):            # "cp***" ?
  86.         try:
  87.             codecs.lookup(enc)
  88.         except LookupError:
  89.             import encodings
  90.             encodings._cache[enc] = encodings._unknown
  91.             encodings.aliases.aliases[enc] = 'mbcs'
  92.  
  93. def add_calibre_vars():
  94.     sys.resources_location = os.path.join(sys.app_dir, 'resources')
  95.     sys.extensions_location = os.path.join(sys.app_dir, 'plugins2')
  96.  
  97.     dv = os.environ.get('CALIBRE_DEVELOP_FROM', None)
  98.     if dv and os.path.exists(dv):
  99.         sys.path.insert(0, os.path.abspath(dv))
  100.  
  101. def makepath(*paths):
  102.     dir = os.path.abspath(os.path.join(*paths))
  103.     return dir, os.path.normcase(dir)
  104.  
  105. def run_entry_point():
  106.     bname, mod, func = sys.calibre_basename, sys.calibre_module, sys.calibre_function
  107.     sys.argv[0] = bname+'.exe'
  108.     pmod = __import__(mod, fromlist=[1], level=0)
  109.     return getattr(pmod, func)()
  110.  
  111. def main():
  112.     sys.frozen = 'windows_exe'
  113.     sys.setdefaultencoding('utf-8')
  114.     aliasmbcs()
  115.  
  116.     sys.path_hooks.insert(0, ZipExtensionImporter)
  117.     sys.path_importer_cache.clear()
  118.  
  119.     import linecache
  120.     def fake_getline(filename, lineno, module_globals=None):
  121.         return ''
  122.     linecache.orig_getline = linecache.getline
  123.     linecache.getline = fake_getline
  124.  
  125.     abs__file__()
  126.  
  127.     add_calibre_vars()
  128.  
  129.     # Needed for pywintypes to be able to load its DLL
  130.     sys.path.append(os.path.join(sys.app_dir, 'DLLs'))
  131.  
  132.     return run_entry_point()
  133.  
  134.  
Add Comment
Please, Sign In to add comment