| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
|
| 4 |
__license__ = 'GPL v3' |
| 5 |
__copyright__ = '2009, Kovid Goyal <[email protected]>' |
| 6 |
__docformat__ = 'restructuredtext en' |
| 7 |
|
| 8 |
import os |
| 9 |
from contextlib import suppress |
| 10 |
|
| 11 |
from setup import Command, __appname__ |
| 12 |
|
| 13 |
|
| 14 |
class GUI(Command): |
| 15 |
description = 'Compile all GUI forms' |
| 16 |
PATH = os.path.join(Command.SRC, __appname__, 'gui2') |
| 17 |
QRC = os.path.join(Command.RESOURCES, 'images.qrc') |
| 18 |
RCC = os.path.join(Command.RESOURCES, 'icons.rcc') |
| 19 |
|
| 20 |
def add_options(self, parser): |
| 21 |
parser.add_option('--summary', default=False, action='store_true', help='Only display a summary about how many files were compiled') |
| 22 |
|
| 23 |
def find_forms(self): |
| 24 |
# We do not use the calibre function find_forms as |
| 25 |
# importing calibre.gui2 may not work |
| 26 |
forms = [] |
| 27 |
for root, _, files in os.walk(self.PATH): |
| 28 |
for name in files: |
| 29 |
path = os.path.abspath(os.path.join(root, name)) |
| 30 |
if name.endswith('.ui'): |
| 31 |
forms.append(path) |
| 32 |
elif name.endswith(('_ui.py', '_ui.pyc')): |
| 33 |
fname = path.rpartition('_')[0] + '.ui' |
| 34 |
if not os.path.exists(fname): |
| 35 |
os.remove(path) |
| 36 |
return forms |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def form_to_compiled_form(cls, form): |
| 40 |
# We do not use the calibre function form_to_compiled_form as |
| 41 |
# importing calibre.gui2 may not work |
| 42 |
return form.rpartition('.')[0] + '_ui.py' |
| 43 |
|
| 44 |
def run(self, opts): |
| 45 |
self.build_forms(summary=opts.summary) |
| 46 |
self.build_images() |
| 47 |
|
| 48 |
def build_images(self): |
| 49 |
cwd = os.getcwd() |
| 50 |
try: |
| 51 |
os.chdir(self.RESOURCES) |
| 52 |
sources, files = [], [] |
| 53 |
for root, _, files2 in os.walk('images'): |
| 54 |
for name in files2: |
| 55 |
sources.append(os.path.join(root, name)) |
| 56 |
if self.newer(self.RCC, sources): |
| 57 |
self.info('Creating icon theme resource file') |
| 58 |
from calibre.utils.rcc import compile_icon_dir_as_themes |
| 59 |
|
| 60 |
compile_icon_dir_as_themes('images', self.RCC) |
| 61 |
if self.newer(self.QRC, sources): |
| 62 |
self.info('Creating images.qrc') |
| 63 |
for s in sources: |
| 64 |
files.append(f'<file>{s}</file>') |
| 65 |
manifest = '<RCC>\n<qresource prefix="/">\n{}\n</qresource>\n</RCC>'.format('\n'.join(sorted(files))) |
| 66 |
if not isinstance(manifest, bytes): |
| 67 |
manifest = manifest.encode('utf-8') |
| 68 |
with open('images.qrc', 'wb') as f: |
| 69 |
f.write(manifest) |
| 70 |
finally: |
| 71 |
os.chdir(cwd) |
| 72 |
|
| 73 |
def build_forms(self, summary=False): |
| 74 |
from calibre.build_forms import build_forms |
| 75 |
|
| 76 |
build_forms(self.SRC, info=self.info, summary=summary, check_icons=False) |
| 77 |
|
| 78 |
def clean(self): |
| 79 |
forms = self.find_forms() |
| 80 |
for form in forms: |
| 81 |
c = self.form_to_compiled_form(form) |
| 82 |
if os.path.exists(c): |
| 83 |
os.remove(c) |
| 84 |
for x in (self.QRC, self.RCC): |
| 85 |
with suppress(FileNotFoundError): |
| 86 |
os.remove(x) |