def compile_py_modules(self): print('\nCompiling Python modules') base = join(self.resources_dir, 'Python') pydir = join(base, f'lib/python{py_ver}') src = join(pydir, 'lib-dynload') dest = join(self.frameworks_dir, 'plugins') print('Extracting extension modules from:', src, 'to', dest) self.ext_map.update(extract_extension_modules(src, dest)) os.rmdir(src) src = join(base, 'site-packages') print('Extracting extension modules from:', src, 'to', dest) self.ext_map.update(extract_extension_modules(src, dest)) for x in os.listdir(src): os.rename(join(src, x), join(pydir, x)) os.rmdir(src) py_compile(pydir) freeze_python( pydir, dest, self.inc_dir, self.ext_map, develop_mode_env_var='CALIBRE_DEVELOP_FROM', path_to_user_env_vars='~/Library/Preferences/calibre/macos-env.txt' ) shutil.rmtree(pydir)
def copy_python(env): print('Copying python...') srcdir = j(PREFIX, 'lib/python' + py_ver) for x in os.listdir(srcdir): y = j(srcdir, x) ext = os.path.splitext(x)[1] if os.path.isdir(y) and x not in ('test', 'hotshot', 'distutils', 'tkinter', 'turtledemo', 'site-packages', 'idlelib', 'lib2to3', 'dist-packages'): shutil.copytree(y, j(env.py_dir, x), ignore=ignore_in_lib) if os.path.isfile(y) and ext in ('.py', '.so'): shutil.copy2(y, env.py_dir) srcdir = j(srcdir, 'site-packages') site_packages_dir = j(env.py_dir, 'site-packages') import_site_packages(srcdir, site_packages_dir) pdir = os.path.join(env.lib_dir, 'kitty-extensions') os.makedirs(pdir, exist_ok=True) kitty_dir = os.path.join(env.lib_dir, 'kitty') bases = ('kitty', 'kittens', 'kitty_tests') for x in bases: dest = os.path.join(env.py_dir, x) os.rename(os.path.join(kitty_dir, x), dest) if x == 'kitty': shutil.rmtree(os.path.join(dest, 'launcher')) os.rename(os.path.join(kitty_dir, '__main__.py'), os.path.join(env.py_dir, 'kitty_main.py')) shutil.rmtree(os.path.join(kitty_dir, '__pycache__')) print('Extracting extension modules from', env.py_dir, 'to', pdir) ext_map = extract_extension_modules(env.py_dir, pdir) shutil.copy(os.path.join(os.path.dirname(self_dir), 'site.py'), os.path.join(env.py_dir, 'site.py')) for x in bases: iv['sanitize_source_folder'](os.path.join(env.py_dir, x)) py_compile(env.py_dir) freeze_python(env.py_dir, pdir, env.obj_dir, ext_map, develop_mode_env_var='KITTY_DEVELOP_FROM', remove_pyc_files=True)
def copy_python(env, ext_dir): print('Copying python...') srcdir = j(PREFIX, 'lib/python' + py_ver) for x in os.listdir(srcdir): y = j(srcdir, x) ext = os.path.splitext(x)[1] if os.path.isdir(y) and x not in ('test', 'hotshot', 'site-packages', 'idlelib', 'dist-packages'): shutil.copytree(y, j(env.py_dir, x), ignore=ignore_in_lib) if os.path.isfile(y) and ext in ('.py', '.so'): shutil.copy2(y, env.py_dir) srcdir = j(srcdir, 'site-packages') dest = j(env.py_dir, 'site-packages') import_site_packages(srcdir, dest) for x in os.listdir(env.SRC): c = j(env.SRC, x) if os.path.exists(j(c, '__init__.py')): shutil.copytree(c, j(dest, x), ignore=partial(ignore_in_lib, ignored_dirs={})) elif os.path.isfile(c): shutil.copy2(c, j(dest, x)) shutil.copytree(j(env.src_root, 'resources'), j(env.base, 'resources')) for pak in glob.glob(j(QT_PREFIX, 'resources', '*.pak')): shutil.copy2(pak, j(env.base, 'resources')) os.mkdir(j(env.base, 'translations')) shutil.copytree(j(QT_PREFIX, 'translations', 'qtwebengine_locales'), j(env.base, 'translations', 'qtwebengine_locales')) sitepy = j(self_dir, 'site.py') shutil.copy2(sitepy, j(env.py_dir, 'site.py')) pdir = j(env.lib_dir, 'calibre-extensions') if not os.path.exists(pdir): os.mkdir(pdir) fix_pycryptodome(j(env.py_dir, 'site-packages')) for x in os.listdir(j(env.py_dir, 'site-packages')): os.rename(j(env.py_dir, 'site-packages', x), j(env.py_dir, x)) os.rmdir(j(env.py_dir, 'site-packages')) print('Extracting extension modules from', ext_dir, 'to', pdir) ext_map = extract_extension_modules(ext_dir, pdir) shutil.rmtree(j(env.py_dir, 'calibre', 'plugins')) print('Extracting extension modules from', env.py_dir, 'to', pdir) ext_map.update(extract_extension_modules(env.py_dir, pdir)) py_compile(env.py_dir) freeze_python(env.py_dir, pdir, env.obj_dir, ext_map, develop_mode_env_var='CALIBRE_DEVELOP_FROM') shutil.rmtree(env.py_dir)
def copy_python(env, ext_dir): print('Copying python...') srcdir = j(PREFIX, 'lib/python' + py_ver) for x in os.listdir(srcdir): y = j(srcdir, x) ext = os.path.splitext(x)[1] if os.path.isdir(y) and x not in ('test', 'hotshot', 'site-packages', 'idlelib', 'lib2to3', 'dist-packages'): shutil.copytree(y, j(env.py_dir, x), ignore=ignore_in_lib) if os.path.isfile(y) and ext in ('.py', '.so'): shutil.copy2(y, env.py_dir) srcdir = j(srcdir, 'site-packages') dest = j(env.py_dir, 'site-packages') import_site_packages(srcdir, dest) shutil.rmtree(j(dest, 'PyQt5/uic/port_v3')) filter_pyqt = {x + '.so' for x in PYQT_MODULES} | {'sip.so'} pyqt = j(dest, 'PyQt5') for x in os.listdir(pyqt): if x.endswith('.so') and x not in filter_pyqt: os.remove(j(pyqt, x)) for x in os.listdir(env.SRC): c = j(env.SRC, x) if os.path.exists(j(c, '__init__.py')): shutil.copytree(c, j(dest, x), ignore=partial(ignore_in_lib, ignored_dirs={})) elif os.path.isfile(c): shutil.copy2(c, j(dest, x)) pdir = j(dest, 'calibre', 'plugins') if not os.path.exists(pdir): os.mkdir(pdir) for x in glob.glob(j(ext_dir, '*.so')): shutil.copy2(x, j(pdir, os.path.basename(x))) shutil.copytree(j(env.src_root, 'resources'), j(env.base, 'resources')) for pak in glob.glob(j(QT_PREFIX, 'resources', '*.pak')): shutil.copy2(pak, j(env.base, 'resources')) os.mkdir(j(env.base, 'translations')) shutil.copytree(j(QT_PREFIX, 'translations', 'qtwebengine_locales'), j(env.base, 'translations', 'qtwebengine_locales')) sitepy = j(self_dir, 'site.py') shutil.copy2(sitepy, j(env.py_dir, 'site.py')) py_compile(env.py_dir)
def copy_python(env): print('Copying python...') srcdir = j(PREFIX, 'lib/python' + py_ver) for x in os.listdir(srcdir): y = j(srcdir, x) ext = os.path.splitext(x)[1] if os.path.isdir(y) and x not in ('test', 'hotshot', 'distutils', 'tkinter', 'turtledemo', 'site-packages', 'idlelib', 'lib2to3', 'dist-packages'): shutil.copytree(y, j(env.py_dir, x), ignore=ignore_in_lib) if os.path.isfile(y) and ext in ('.py', '.so'): shutil.copy2(y, env.py_dir) srcdir = j(srcdir, 'site-packages') dest = j(env.py_dir, 'site-packages') import_site_packages(srcdir, dest) py_compile(env.py_dir) py_compile(os.path.join(env.base, 'lib', 'kitty'))
def copy_python(env, ext_dir): print('Copying python...') srcdir = j(PREFIX, 'lib/python' + py_ver) for x in os.listdir(srcdir): y = j(srcdir, x) ext = os.path.splitext(x)[1] if os.path.isdir(y) and x not in ('test', 'hotshot', 'distutils', 'site-packages', 'idlelib', 'lib2to3', 'dist-packages'): shutil.copytree(y, j(env.py_dir, x), ignore=ignore_in_lib) if os.path.isfile(y) and ext in ('.py', '.so'): shutil.copy2(y, env.py_dir) srcdir = j(srcdir, 'site-packages') dest = j(env.py_dir, 'site-packages') import_site_packages(srcdir, dest) shutil.rmtree(j(dest, 'PyQt5/uic/port_v3')) filter_pyqt = {x + '.so' for x in PYQT_MODULES} | {'sip.so'} pyqt = j(dest, 'PyQt5') for x in os.listdir(pyqt): if x.endswith('.so') and x not in filter_pyqt: os.remove(j(pyqt, x)) for x in os.listdir(env.SRC): c = j(env.SRC, x) if os.path.exists(j(c, '__init__.py')): shutil.copytree(c, j(dest, x), ignore=partial(ignore_in_lib, ignored_dirs={})) elif os.path.isfile(c): shutil.copy2(c, j(dest, x)) pdir = j(dest, 'calibre', 'plugins') if not os.path.exists(pdir): os.mkdir(pdir) for x in glob.glob(j(ext_dir, '*.so')): shutil.copy2(x, j(pdir, os.path.basename(x))) shutil.copytree(j(env.src_root, 'resources'), j(env.base, 'resources')) sitepy = j(self_dir, 'site.py') shutil.copy2(sitepy, j(env.py_dir, 'site.py')) py_compile(env.py_dir)
def freeze(env, ext_dir): shutil.copy2(j(env.src_root, 'LICENSE'), env.base) printf('Adding resources...') tgt = j(env.app_base, 'resources') if os.path.exists(tgt): shutil.rmtree(tgt) shutil.copytree(j(env.src_root, 'resources'), tgt) printf('\tAdding misc binary deps') def copybin(x): shutil.copy2(x, env.dll_dir) try: shutil.copy2(x + '.manifest', env.dll_dir) except EnvironmentError as err: if err.errno != errno.ENOENT: raise bindir = os.path.join(PREFIX, 'bin') for x in ('pdftohtml', 'pdfinfo', 'pdftoppm', 'jpegtran-calibre', 'cjpeg-calibre', 'optipng-calibre', 'JXRDecApp-calibre'): copybin(os.path.join(bindir, x + '.exe')) for f in glob.glob(os.path.join(bindir, '*.dll')): if re.search(r'(easylzma|icutest)', f.lower()) is None: copybin(f) copybin( os.path.join(env.python_base, 'python%s.dll' % env.py_ver.replace('.', ''))) for x in glob.glob(os.path.join(env.python_base, 'DLLs', '*')): # python pyd modules copybin(x) for f in walk(os.path.join(env.python_base, 'Lib')): if f.lower().endswith('.dll') and 'scintilla' not in f.lower(): copybin(f) add_plugins(env, ext_dir) printf('Adding Qt...') for x in QT_DLLS: copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) copybin(os.path.join(QT_PREFIX, 'bin', 'QtWebEngineProcess.exe')) for x in 'libGLESv2 libEGL'.split(): copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) plugdir = j(QT_PREFIX, 'plugins') tdir = j(env.app_base, 'plugins') for d in QT_PLUGINS: imfd = os.path.join(plugdir, d) tg = os.path.join(tdir, d) if os.path.exists(tg): shutil.rmtree(tg) shutil.copytree(imfd, tg) for f in walk(tdir): if not f.lower().endswith('.dll'): os.remove(f) for data_file in os.listdir(j(QT_PREFIX, 'resources')): shutil.copy2(j(QT_PREFIX, 'resources', data_file), j(env.app_base, 'resources')) shutil.copytree(j(QT_PREFIX, 'translations'), j(env.app_base, 'translations')) printf('Adding python...') def ignore_lib(root, items): ans = [] for x in items: ext = os.path.splitext(x)[1].lower() if ext in ('.dll', '.chm', '.htm', '.txt'): ans.append(x) return ans shutil.copytree(r'%s\Lib' % env.python_base, env.lib_dir, ignore=ignore_lib) install_site_py(env) # Fix win32com sp_dir = j(env.lib_dir, 'site-packages') comext = j(sp_dir, 'win32comext') shutil.copytree(j(comext, 'shell'), j(sp_dir, 'win32com', 'shell')) shutil.rmtree(comext) for pat in ('PyQt5\\uic\\port_v3', ): x = glob.glob(j(env.lib_dir, 'site-packages', pat))[0] shutil.rmtree(x) pyqt = j(env.lib_dir, 'site-packages', 'PyQt5') for x in {x for x in os.listdir(pyqt) if x.endswith('.pyd')}: if x.partition('.')[0] not in PYQT_MODULES and x != 'sip.pyd': os.remove(j(pyqt, x)) with open(j(pyqt, '__init__.py'), 'r+b') as f: raw = f.read() nraw = raw.replace( b'def find_qt():', b'def find_qt():\n return # disabled for calibre') if nraw == raw: raise Exception( 'Failed to patch PyQt to disable dll directory manipulation') f.seek(0), f.truncate(), f.write(nraw) printf('Adding calibre sources...') for x in glob.glob(j(CALIBRE_DIR, 'src', '*')): if os.path.isdir(x): if os.path.exists(os.path.join(x, '__init__.py')): shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns( '*.pyc', '*.pyo')) else: shutil.copy(x, j(sp_dir, b(x))) for x in (r'calibre\manual', r'calibre\plugins', 'pythonwin'): deld = j(sp_dir, x) if os.path.exists(deld): shutil.rmtree(deld) for x in os.walk(j(sp_dir, 'calibre')): for f in x[-1]: if not f.endswith('.py'): os.remove(j(x[0], f)) extract_pyd_modules(env, sp_dir) printf('Byte-compiling all python modules...') for x in ('test', 'lib2to3'): x = j(env.lib_dir, x) if os.path.exists(x): shutil.rmtree(x) py_compile(env.lib_dir.replace(os.sep, '/'))
def compile_py_modules(self): self.remove_bytecode(join(self.resources_dir, 'Python')) py_compile(join(self.resources_dir, 'Python'))
def freeze(env, ext_dir, incdir): shutil.copy2(j(env.src_root, 'LICENSE'), env.base) printf('Adding resources...') tgt = j(env.app_base, 'resources') if os.path.exists(tgt): shutil.rmtree(tgt) shutil.copytree(j(env.src_root, 'resources'), tgt) printf('\tAdding misc binary deps') def copybin(x): shutil.copy2(x, env.dll_dir) with contextlib.suppress(FileNotFoundError): shutil.copy2(x + '.manifest', env.dll_dir) bindir = os.path.join(PREFIX, 'bin') for x in ('pdftohtml', 'pdfinfo', 'pdftoppm', 'jpegtran-calibre', 'cjpeg-calibre', 'optipng-calibre', 'JXRDecApp-calibre'): copybin(os.path.join(bindir, x + '.exe')) for f in glob.glob(os.path.join(bindir, '*.dll')): if re.search(r'(easylzma|icutest)', f.lower()) is None: copybin(f) copybin( os.path.join(env.python_base, 'python%s.dll' % env.py_ver.replace('.', ''))) copybin(os.path.join(env.python_base, 'python%s.dll' % env.py_ver[0])) for x in glob.glob(os.path.join(env.python_base, 'DLLs', '*.dll')): # dlls needed by python copybin(x) for f in walk(os.path.join(env.python_base, 'Lib')): q = f.lower() if q.endswith( '.dll') and 'scintilla' not in q and 'pyqtbuild' not in q: copybin(f) ext_map = extract_extension_modules(ext_dir, env.dll_dir) ext_map.update( extract_extension_modules(j(env.python_base, 'DLLs'), env.dll_dir, move=False)) printf('Adding Qt...') for x in QT_DLLS: copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) copybin(os.path.join(QT_PREFIX, 'bin', 'QtWebEngineProcess.exe')) for x in 'libGLESv2 libEGL'.split(): copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) plugdir = j(QT_PREFIX, 'plugins') tdir = j(env.app_base, 'plugins') for d in QT_PLUGINS: imfd = os.path.join(plugdir, d) tg = os.path.join(tdir, d) if os.path.exists(tg): shutil.rmtree(tg) shutil.copytree(imfd, tg) for f in walk(tdir): if not f.lower().endswith('.dll'): os.remove(f) for data_file in os.listdir(j(QT_PREFIX, 'resources')): shutil.copy2(j(QT_PREFIX, 'resources', data_file), j(env.app_base, 'resources')) shutil.copytree(j(QT_PREFIX, 'translations'), j(env.app_base, 'translations')) printf('Adding python...') def ignore_lib(root, items): ans = [] for x in items: ext = os.path.splitext(x)[1].lower() if ext in ('.dll', '.chm', '.htm', '.txt'): ans.append(x) return ans shutil.copytree(r'%s\Lib' % env.python_base, env.lib_dir, ignore=ignore_lib) install_site_py(env) sp_dir = j(env.lib_dir, 'site-packages') printf('Adding calibre sources...') for x in glob.glob(j(CALIBRE_DIR, 'src', '*')): if os.path.isdir(x): if os.path.exists(os.path.join(x, '__init__.py')): shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns( '*.pyc', '*.pyo')) else: shutil.copy(x, j(sp_dir, b(x))) ext_map.update(cleanup_site_packages(sp_dir)) for x in os.listdir(sp_dir): os.rename(j(sp_dir, x), j(env.lib_dir, x)) os.rmdir(sp_dir) printf('Extracting extension modules from', env.lib_dir, 'to', env.dll_dir) ext_map.update(extract_extension_modules(env.lib_dir, env.dll_dir)) printf('Byte-compiling all python modules...') py_compile(env.lib_dir.replace(os.sep, '/')) # from bypy.utils import run_shell # run_shell(cwd=env.lib_dir) freeze_python(env.lib_dir, env.dll_dir, incdir, ext_map, develop_mode_env_var='CALIBRE_DEVELOP_FROM') shutil.rmtree(env.lib_dir)
def compile_py_modules(self): print('\nCompiling Python modules') base = join(self.resources_dir, 'Python') py_compile(base)
def compile_py_modules(self): print('\nCompiling Python modules') self.remove_bytecode(join(self.resources_dir, 'Python')) py_compile(join(self.resources_dir, 'Python')) self.remove_bytecode(join(self.resources_dir, 'kitty')) py_compile(join(self.resources_dir, 'kitty'))
def freeze(env, ext_dir): shutil.copy2(j(env.src_root, 'LICENSE'), env.base) printf('Adding resources...') tgt = j(env.app_base, 'resources') if os.path.exists(tgt): shutil.rmtree(tgt) shutil.copytree(j(env.src_root, 'resources'), tgt) printf('\tAdding misc binary deps') def copybin(x): shutil.copy2(x, env.dll_dir) try: shutil.copy2(x + '.manifest', env.dll_dir) except EnvironmentError as err: if err.errno != errno.ENOENT: raise bindir = os.path.join(PREFIX, 'bin') for x in ('pdftohtml', 'pdfinfo', 'pdftoppm', 'jpegtran-calibre', 'cjpeg-calibre', 'optipng-calibre', 'JXRDecApp-calibre'): copybin(os.path.join(bindir, x + '.exe')) for f in glob.glob(os.path.join(bindir, '*.dll')): if re.search(r'(easylzma|icutest)', f.lower()) is None: copybin(f) copybin(os.path.join(env.python_base, 'python%s.dll' % env.py_ver.replace('.', ''))) copybin(os.path.join(env.python_base, 'python%s.dll' % env.py_ver[0])) for x in glob.glob(os.path.join(env.python_base, 'DLLs', '*')): # python pyd modules and dlls copybin(x) for f in walk(os.path.join(env.python_base, 'Lib')): q = f.lower() if q.endswith('.dll') and 'scintilla' not in q and 'pyqtbuild' not in q: copybin(f) add_plugins(env, ext_dir) printf('Adding Qt...') for x in QT_DLLS: copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) copybin(os.path.join(QT_PREFIX, 'bin', 'QtWebEngineProcess.exe')) for x in 'libGLESv2 libEGL'.split(): copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll')) plugdir = j(QT_PREFIX, 'plugins') tdir = j(env.app_base, 'plugins') for d in QT_PLUGINS: imfd = os.path.join(plugdir, d) tg = os.path.join(tdir, d) if os.path.exists(tg): shutil.rmtree(tg) shutil.copytree(imfd, tg) for f in walk(tdir): if not f.lower().endswith('.dll'): os.remove(f) for data_file in os.listdir(j(QT_PREFIX, 'resources')): shutil.copy2(j(QT_PREFIX, 'resources', data_file), j(env.app_base, 'resources')) shutil.copytree(j(QT_PREFIX, 'translations'), j(env.app_base, 'translations')) printf('Adding python...') def ignore_lib(root, items): ans = [] for x in items: ext = os.path.splitext(x)[1].lower() if ext in ('.dll', '.chm', '.htm', '.txt'): ans.append(x) return ans shutil.copytree(r'%s\Lib' % env.python_base, env.lib_dir, ignore=ignore_lib) install_site_py(env) # Fix win32com sp_dir = j(env.lib_dir, 'site-packages') comext = j(sp_dir, 'win32comext') shutil.copytree(j(comext, 'shell'), j(sp_dir, 'win32com', 'shell')) shutil.rmtree(comext) # Fix pycryptodome with open(j(sp_dir, 'Crypto', 'Util', '_file_system.py'), 'w') as fspy: fspy.write(''' import os, sys def pycryptodome_filename(dir_comps, filename): base = os.path.join(sys.app_dir, 'app', 'bin') path = os.path.join(base, '.'.join(dir_comps + [filename])) return path ''') printf('Adding calibre sources...') for x in glob.glob(j(CALIBRE_DIR, 'src', '*')): if os.path.isdir(x): if os.path.exists(os.path.join(x, '__init__.py')): shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns('*.pyc', '*.pyo')) else: shutil.copy(x, j(sp_dir, b(x))) for x in (r'calibre\manual', r'calibre\plugins', 'pythonwin'): deld = j(sp_dir, x) if os.path.exists(deld): shutil.rmtree(deld) for x in os.walk(j(sp_dir, 'calibre')): for f in x[-1]: if not f.endswith('.py'): os.remove(j(x[0], f)) extract_pyd_modules(env, sp_dir) printf('Byte-compiling all python modules...') for x in ('test', 'lib2to3'): x = j(env.lib_dir, x) if os.path.exists(x): shutil.rmtree(x) py_compile(env.lib_dir.replace(os.sep, '/'))