Dont import calibre.gui2 to build forms

This commit is contained in:
Kovid Goyal 2021-11-21 22:18:20 +05:30
parent 53ad3db66e
commit e99ab7e002
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 83 additions and 68 deletions

View File

@ -66,7 +66,7 @@ class GUI(Command):
os.chdir(cwd)
def build_forms(self, summary=False):
from calibre.gui2 import build_forms
from calibre.build_forms import build_forms
build_forms(self.SRC, info=self.info, summary=summary)
def clean(self):

View File

@ -13,9 +13,13 @@ class To6(Command):
description = 'Toggle between the Qt6 and master branches building everything needed'
def ccall(self, *a):
self.info(*a)
subprocess.check_call(a)
def run(self, opts):
subprocess.check_call(['git', 'switch', '-'])
subprocess.check_call([sys.executable, 'setup.py', 'build', '--clean'])
subprocess.check_call([sys.executable, 'setup.py', 'build'])
subprocess.check_call([sys.executable, 'setup.py', 'gui', '--clean'])
subprocess.check_call([sys.executable, 'setup.py', 'gui'])
self.ccall('git', 'switch', '-')
self.ccall(sys.executable, 'setup.py', 'build', '--clean')
self.ccall(sys.executable, 'setup.py', 'build')
self.ccall(sys.executable, 'setup.py', 'gui', '--clean')
self.ccall(sys.executable, 'setup.py', 'gui', '--summary')

View File

@ -0,0 +1,72 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
import os
import importlib
def form_to_compiled_form(form):
return form.rpartition('.')[0]+'_ui.py'
def find_forms(srcdir):
base = os.path.join(srcdir, 'calibre', 'gui2')
forms = []
for root, _, files in os.walk(base):
for name in files:
if name.endswith('.ui'):
forms.append(os.path.abspath(os.path.join(root, name)))
return forms
def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
import re
from qt.core import QT_VERSION_STR
qt_major = QT_VERSION_STR.split('.')[0]
m = importlib.import_module(f'PyQt{qt_major}.uic')
from polyglot.io import PolyglotStringIO
forms = find_forms(srcdir)
if info is None:
info = print
pat = re.compile(r'''(['"]):/images/([^'"]+)\1''')
def sub(match):
ans = 'I(%s%s%s)'%(match.group(1), match.group(2), match.group(1))
return ans
num = 0
transdef_pat = re.compile(r'^\s+_translate\s+=\s+QtCore.QCoreApplication.translate$', flags=re.M)
transpat = re.compile(r'_translate\s*\(.+?,\s+"(.+?)(?<!\\)"\)', re.DOTALL)
# Ensure that people running from source have all their forms rebuilt for
# the qt5 migration
force_compile = False
if check_for_migration:
from calibre.gui2 import gprefs
force_compile = not gprefs.get(f'migrated_forms_to_qt{qt_major}', False)
for form in forms:
compiled_form = form_to_compiled_form(form)
if force_compile or not os.path.exists(compiled_form) or os.stat(form).st_mtime > os.stat(compiled_form).st_mtime:
if not summary:
info('\tCompiling form', form)
buf = PolyglotStringIO()
m.compileUi(form, buf)
dat = buf.getvalue()
dat = dat.replace('import images_rc', '')
dat = transdef_pat.sub('', dat)
dat = transpat.sub(r'_("\1")', dat)
dat = dat.replace('_("MMM yyyy")', '"MMM yyyy"')
dat = dat.replace('_("d MMM yyyy")', '"d MMM yyyy"')
dat = pat.sub(sub, dat)
if not isinstance(dat, bytes):
dat = dat.encode('utf-8')
open(compiled_form, 'wb').write(dat)
num += 1
if num:
info('Compiled %d forms' % num)
if force_compile:
gprefs.set(f'migrated_forms_to_qt{qt_major}', True)

View File

@ -1422,69 +1422,8 @@ def elided_text(text, font=None, width=300, pos='middle'):
return str(text)
def find_forms(srcdir):
base = os.path.join(srcdir, 'calibre', 'gui2')
forms = []
for root, _, files in os.walk(base):
for name in files:
if name.endswith('.ui'):
forms.append(os.path.abspath(os.path.join(root, name)))
return forms
def form_to_compiled_form(form):
return form.rpartition('.')[0]+'_ui.py'
def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
import re
from PyQt5.uic import compileUi
from polyglot.io import PolyglotStringIO
forms = find_forms(srcdir)
if info is None:
from calibre import prints
info = prints
pat = re.compile(r'''(['"]):/images/([^'"]+)\1''')
def sub(match):
ans = 'I(%s%s%s)'%(match.group(1), match.group(2), match.group(1))
return ans
num = 0
transdef_pat = re.compile(r'^\s+_translate\s+=\s+QtCore.QCoreApplication.translate$', flags=re.M)
transpat = re.compile(r'_translate\s*\(.+?,\s+"(.+?)(?<!\\)"\)', re.DOTALL)
# Ensure that people running from source have all their forms rebuilt for
# the qt5 migration
force_compile = check_for_migration and not gprefs.get('migrated_forms_to_qt5', False)
for form in forms:
compiled_form = form_to_compiled_form(form)
if force_compile or not os.path.exists(compiled_form) or os.stat(form).st_mtime > os.stat(compiled_form).st_mtime:
if not summary:
info('\tCompiling form', form)
buf = PolyglotStringIO()
compileUi(form, buf)
dat = buf.getvalue()
dat = dat.replace('import images_rc', '')
dat = transdef_pat.sub('', dat)
dat = transpat.sub(r'_("\1")', dat)
dat = dat.replace('_("MMM yyyy")', '"MMM yyyy"')
dat = dat.replace('_("d MMM yyyy")', '"d MMM yyyy"')
dat = pat.sub(sub, dat)
if not isinstance(dat, bytes):
dat = dat.encode('utf-8')
open(compiled_form, 'wb').write(dat)
num += 1
if num:
info('Compiled %d forms' % num)
if force_compile:
gprefs.set('migrated_forms_to_qt5', True)
if is_running_from_develop:
from calibre.build_forms import build_forms
build_forms(os.environ['CALIBRE_DEVELOP_FROM'], check_for_migration=True)