| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai |
| 3 |
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 4 |
|
| 5 |
import builtins |
| 6 |
import os |
| 7 |
import sys |
| 8 |
from importlib import import_module |
| 9 |
from importlib.machinery import EXTENSION_SUFFIXES |
| 10 |
from importlib.util import spec_from_file_location |
| 11 |
|
| 12 |
import _sitebuiltins |
| 13 |
|
| 14 |
pyd_items = None |
| 15 |
extension_suffixes = sorted(EXTENSION_SUFFIXES, key=len, reverse=True) |
| 16 |
USER_SITE = None |
| 17 |
|
| 18 |
|
| 19 |
def remove_extension_suffix(name): |
| 20 |
for q in extension_suffixes: |
| 21 |
if name.endswith(q): |
| 22 |
return name[:-len(q)] |
| 23 |
|
| 24 |
|
| 25 |
class PydImporter: |
| 26 |
|
| 27 |
def find_spec(self, fullname, path, target=None): |
| 28 |
global pyd_items |
| 29 |
if pyd_items is None: |
| 30 |
pyd_items = {} |
| 31 |
dlls_dir = os.path.join(sys.app_dir, 'app', 'bin') |
| 32 |
for x in os.listdir(dlls_dir): |
| 33 |
lx = x.lower() |
| 34 |
if lx.endswith('.pyd'): |
| 35 |
pyd_items[remove_extension_suffix(lx)] = os.path.abspath(os.path.join(dlls_dir, x)) |
| 36 |
q = fullname.lower() |
| 37 |
path = pyd_items.get(q) |
| 38 |
if path is not None: |
| 39 |
return spec_from_file_location(fullname, path) |
| 40 |
|
| 41 |
def invalidate_caches(self): |
| 42 |
global pyd_items |
| 43 |
pyd_items = None |
| 44 |
|
| 45 |
|
| 46 |
def run_entry_point(): |
| 47 |
bname, mod, func = sys.calibre_basename, sys.calibre_module, sys.calibre_function |
| 48 |
sys.argv[0] = bname + '.exe' |
| 49 |
pmod = import_module(mod) |
| 50 |
return getattr(pmod, func)() |
| 51 |
|
| 52 |
|
| 53 |
def set_helper(): |
| 54 |
builtins.help = _sitebuiltins._Helper() |
| 55 |
|
| 56 |
|
| 57 |
def set_quit(): |
| 58 |
eof = 'Ctrl-Z plus Return' |
| 59 |
builtins.quit = _sitebuiltins.Quitter('quit', eof) |
| 60 |
builtins.exit = _sitebuiltins.Quitter('exit', eof) |
| 61 |
|
| 62 |
|
| 63 |
def main(): |
| 64 |
sys.meta_path.insert(0, PydImporter()) |
| 65 |
os.add_dll_directory(os.path.abspath(os.path.join(sys.app_dir, 'app', 'bin'))) |
| 66 |
|
| 67 |
import linecache |
| 68 |
|
| 69 |
def fake_getline(filename, lineno, module_globals=None): |
| 70 |
return '' |
| 71 |
|
| 72 |
linecache.orig_getline = linecache.getline |
| 73 |
linecache.getline = fake_getline |
| 74 |
|
| 75 |
set_helper() |
| 76 |
set_quit() |
| 77 |
|
| 78 |
return run_entry_point() |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == '__main__': |
| 82 |
try: |
| 83 |
main() |
| 84 |
except Exception: |
| 85 |
if sys.gui_app and sys.excepthook == sys.__excepthook__: |
| 86 |
import traceback |
| 87 |
|
| 88 |
import calibre_os_module |
| 89 |
calibre_os_module.gui_error_message( |
| 90 |
f"Unhandled exception running {sys.calibre_basename}", |
| 91 |
traceback.format_exc()) |
| 92 |
raise |