def run(self, opts): from lzma.xz import compress self.h = sha1() tdir = mkdtemp('calibre-mathjax-build') try: src = opts.path_to_mathjax or self.download_mathjax_release( tdir, opts.mathjax_url) self.info('Compressing MathJax...') t = SpooledTemporaryFile() with ZipFile(t, 'w', ZIP_STORED) as zf: self.add_file(zf, self.j(src, 'unpacked', 'MathJax.js'), 'MathJax.js') self.add_tree( zf, self.j(src, 'fonts', 'HTML-CSS', self.FONT_FAMILY, 'woff'), 'fonts/HTML-CSS/%s/woff' % self.FONT_FAMILY) for d in 'extensions jax/element jax/input jax/output/CommonHTML'.split( ): self.add_tree(zf, self.j(src, 'unpacked', *d.split('/')), d) zf.comment = self.h.hexdigest() t.seek(0) with open( self.j(self.RESOURCES, 'content-server', 'mathjax.zip.xz'), 'wb') as f: compress(t, f, level=1 if is_travis else 9) with open( self.j(self.RESOURCES, 'content-server', 'mathjax.version'), 'wb') as f: f.write(zf.comment) finally: shutil.rmtree(tdir)
def create_themeball(report, progress=None, abort=None): pool = ThreadPool(processes=cpu_count()) buf = BytesIO() num = count() error_occurred = Event() def optimize(name): if abort is not None and abort.is_set(): return if error_occurred.is_set(): return try: i = next(num) if progress is not None: progress(i, _('Optimizing %s') % name) srcpath = os.path.join(report.path, name) ext = srcpath.rpartition('.')[-1].lower() if ext == 'png': optimize_png(srcpath) elif ext in ('jpg', 'jpeg'): optimize_jpeg(srcpath) except Exception: return sys.exc_info() errors = tuple(filter(None, pool.map(optimize, tuple(report.name_map)))) pool.close(), pool.join() if abort is not None and abort.is_set(): return if errors: e = errors[0] reraise(*e) if progress is not None: progress(next(num), _('Creating theme file')) with ZipFile(buf, 'w') as zf: for name in report.name_map: srcpath = os.path.join(report.path, name) with lopen(srcpath, 'rb') as f: zf.writestr(name, f.read(), compression=ZIP_STORED) buf.seek(0) out = BytesIO() if abort is not None and abort.is_set(): return None, None if progress is not None: progress(next(num), _('Compressing theme file')) compress(buf, out, level=9) buf = BytesIO() prefix = report.name if abort is not None and abort.is_set(): return None, None with ZipFile(buf, 'w') as zf: with lopen(os.path.join(report.path, THEME_METADATA), 'rb') as f: zf.writestr(prefix + '/' + THEME_METADATA, f.read()) zf.writestr(prefix + '/' + THEME_COVER, create_cover(report)) zf.writestr(prefix + '/' + 'icons.zip.xz', out.getvalue(), compression=ZIP_STORED) if progress is not None: progress(next(num), _('Finished')) return buf.getvalue(), prefix
def run(self, opts): from lzma.xz import compress self.h = sha1() tdir = mkdtemp("calibre-mathjax-build") try: src = opts.path_to_mathjax or self.download_mathjax_release(tdir, opts.mathjax_url) self.info("Compressing MathJax...") t = SpooledTemporaryFile() with ZipFile(t, "w", ZIP_STORED) as zf: self.add_file(zf, self.j(src, "unpacked", "MathJax.js"), "MathJax.js") self.add_tree( zf, self.j(src, "fonts", "HTML-CSS", self.FONT_FAMILY, "woff"), "fonts/HTML-CSS/%s/woff" % self.FONT_FAMILY, ) for d in "extensions jax/element jax/input jax/output/CommonHTML".split(): self.add_tree(zf, self.j(src, "unpacked", *d.split("/")), d) zf.comment = self.h.hexdigest() t.seek(0) with open(self.j(self.RESOURCES, "content-server", "mathjax.zip.xz"), "wb") as f: compress(t, f, level=9) with open(self.j(self.RESOURCES, "content-server", "mathjax.version"), "wb") as f: f.write(zf.comment) finally: shutil.rmtree(tdir)
def create_themeball(report, progress=None, abort=None): pool = ThreadPool(processes=cpu_count()) buf = BytesIO() num = count() error_occurred = Event() def optimize(name): if abort is not None and abort.is_set(): return if error_occurred.is_set(): return try: i = next(num) if progress is not None: progress(i, _('Optimizing %s') % name) srcpath = os.path.join(report.path, name) ext = srcpath.rpartition('.')[-1].lower() if ext == 'png': optimize_png(srcpath) elif ext in ('jpg', 'jpeg'): optimize_jpeg(srcpath) except Exception: return sys.exc_info() errors = tuple(filter(None, pool.map(optimize, tuple(report.name_map.iterkeys())))) pool.close(), pool.join() if abort is not None and abort.is_set(): return if errors: e = errors[0] raise e[0], e[1], e[2] if progress is not None: progress(next(num), _('Creating theme file')) with ZipFile(buf, 'w') as zf: for name in report.name_map: srcpath = os.path.join(report.path, name) with lopen(srcpath, 'rb') as f: zf.writestr(name, f.read(), compression=ZIP_STORED) buf.seek(0) out = BytesIO() if abort is not None and abort.is_set(): return None, None if progress is not None: progress(next(num), _('Compressing theme file')) compress(buf, out, level=9) buf = BytesIO() prefix = report.name if abort is not None and abort.is_set(): return None, None with ZipFile(buf, 'w') as zf: with lopen(os.path.join(report.path, THEME_METADATA), 'rb') as f: zf.writestr(prefix + '/' + THEME_METADATA, f.read()) zf.writestr(prefix + '/' + THEME_COVER, create_cover(report)) zf.writestr(prefix + '/' + 'icons.zip.xz', out.getvalue(), compression=ZIP_STORED) if progress is not None: progress(next(num), _('Finished')) return buf.getvalue(), prefix
def update_rapydscript(): from lzma.xz import compress d = os.path.dirname base = d(d(d(d(d(abspath(__file__)))))) base = os.path.join(base, 'rapydscript') with TemporaryDirectory() as tdir: subprocess.check_call(['node', '--harmony', os.path.join(base, 'bin', 'web-repl-export'), tdir]) with open(os.path.join(tdir, 'rapydscript.js'), 'rb') as f: raw = f.read() path = P(COMPILER_PATH, allow_user_override=False) with open(path, 'wb') as f: compress(raw, f, 9)
def update_rapydscript(): d = os.path.dirname base = d(d(d(d(d(abspath(__file__)))))) base = os.path.join(base, 'rapydscript') raw = subprocess.check_output(['node', '--harmony', os.path.join(base, 'bin', 'export')]) if isinstance(raw, type('')): raw = raw.encode('utf-8') path = P(COMPILER_PATH, allow_user_override=False) with open(path, 'wb') as f: compress(raw, f, 9) base = os.path.join(base, 'src', 'lib') dest = os.path.join(P('rapydscript', allow_user_override=False), 'lib') if not os.path.exists(dest): os.mkdir(dest) for x in glob.glob(os.path.join(base, '*.pyj')): shutil.copy2(x, dest)
def update_rapydscript(): d = os.path.dirname base = d(d(d(d(d(abspath(__file__)))))) base = os.path.join(base, "rapydscript") raw = subprocess.check_output(["node", "--harmony", os.path.join(base, "bin", "export")]) if isinstance(raw, type("")): raw = raw.encode("utf-8") path = P(COMPILER_PATH, allow_user_override=False) with open(path, "wb") as f: compress(raw, f, 9) base = os.path.join(base, "src", "lib") dest = os.path.join(P("rapydscript", allow_user_override=False), "lib") if not os.path.exists(dest): os.mkdir(dest) for x in glob.glob(os.path.join(base, "*.pyj")): shutil.copy2(x, dest)
def update_rapydscript(): d = os.path.dirname base = d(d(d(d(d(abspath(__file__)))))) base = os.path.join(base, 'rapydscript') raw = subprocess.check_output(['node', '--harmony', os.path.join(base, 'bin', 'export')]) if isinstance(raw, unicode_type): raw = raw.encode('utf-8') path = P(COMPILER_PATH, allow_user_override=False) with open(path, 'wb') as f: compress(raw, f, 9) base = os.path.join(base, 'src', 'lib') dest = os.path.join(P('rapydscript', allow_user_override=False), 'lib') if not os.path.exists(dest): os.mkdir(dest) for x in glob.glob(os.path.join(base, '*.pyj')): shutil.copy2(x, dest)
def create_themeball(report): buf = BytesIO() with ZipFile(buf, 'w') as zf: for name, path in report.name_map.iteritems(): with open(os.path.join(report.path, name), 'rb') as f: zf.writestr(name, f.read(), compression=ZIP_STORED) buf.seek(0) out = BytesIO() compress(buf, out, level=9) buf = BytesIO() prefix = report.name with ZipFile(buf, 'w') as zf: with open(os.path.join(report.path, THEME_METADATA), 'rb') as f: zf.writestr(prefix + '/' + THEME_METADATA, f.read()) zf.writestr(prefix + '/' + THEME_COVER, create_cover(report)) zf.writestr(prefix + '/' + 'icons.zip.xz', out.getvalue(), compression=ZIP_STORED) return buf.getvalue(), prefix
def create_themeball(report): buf = BytesIO() with ZipFile(buf, 'w') as zf: for name, path in report.name_map.iteritems(): if name not in report.extra: with open(os.path.join(report.path, name), 'rb') as f: zf.writestr(name, f.read(), compression=ZIP_STORED) buf.seek(0) out = BytesIO() compress(buf, out, level=9) buf = BytesIO() prefix = ascii_filename(report.theme.title).replace(' ', '_').lower() with ZipFile(buf, 'w') as zf: with open(os.path.join(report.path, THEME_METADATA), 'rb') as f: zf.writestr(prefix + '/' + THEME_METADATA, f.read()) zf.writestr(prefix + '/' + THEME_COVER, create_cover(report)) zf.writestr(prefix + '/' + 'icons.zip.xz', out.getvalue(), compression=ZIP_STORED) return buf.getvalue()
def run(self, opts): from lzma.xz import compress self.h = sha1() tdir = mkdtemp('calibre-mathjax-build') try: src = opts.path_to_mathjax or self.download_mathjax_release(tdir, opts.mathjax_url) self.info('Compressing MathJax...') t = SpooledTemporaryFile() with ZipFile(t, 'w', ZIP_STORED) as zf: self.add_file(zf, self.j(src, 'unpacked', 'MathJax.js'), 'MathJax.js') self.add_tree(zf, self.j(src, 'fonts', 'HTML-CSS', self.FONT_FAMILY, 'woff'), 'fonts/HTML-CSS/%s/woff' % self.FONT_FAMILY) for d in 'extensions jax/element jax/input jax/output/CommonHTML'.split(): self.add_tree(zf, self.j(src, 'unpacked', *d.split('/')), d) zf.comment = self.h.hexdigest() t.seek(0) with open(self.j(self.RESOURCES, 'content-server', 'mathjax.zip.xz'), 'wb') as f: compress(t, f, level=4 if is_ci else 9) with open(self.j(self.RESOURCES, 'content-server', 'mathjax.version'), 'wb') as f: f.write(zf.comment) finally: shutil.rmtree(tdir)