def _generate_zip_file(self, files_holder, name_prefix='material', name_suffix=None): """Generate a zip file containing the files passed. :param files_holder: An iterable (or an iterable containing) object that contains the files to be added in the zip file. :param name_prefix: The prefix to the zip file name :param name_suffix: The suffix to the zip file name :return: The generated zip file. """ temp_file = NamedTemporaryFile(suffix='indico.tmp', dir=config.TEMP_DIR) with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler: self.used_filenames = set() for item in self._iter_items(files_holder): name = self._prepare_folder_structure(item) self.used_filenames.add(name) with item.storage.get_local_path( item.storage_file_id) as filepath: zip_handler.write(filepath.encode('utf-8'), name) temp_file.delete = False zip_file_name = '{}-{}.zip'.format( name_prefix, name_suffix) if name_suffix else '{}.zip'.format(name_prefix) chmod_umask(temp_file.name) return send_file(zip_file_name, temp_file.name, 'application/zip', inline=False)
def run(self, template_name, **kwargs): self._dir = tempfile.mkdtemp(prefix="indico-texgen-", dir=config.TEMP_DIR) chmod_umask(self._dir, execute=True) source_filename = os.path.join(self._dir, template_name + '.tex') target_filename = os.path.join(self._dir, template_name + '.pdf') source = self._render_template(template_name + '.tex', kwargs) with codecs.open(source_filename, 'wb', encoding='utf-8') as f: f.write(source) log_filename = os.path.join(self._dir, 'output.log') log_file = open(log_filename, 'a+') try: self.run_latex(source_filename, log_file) if self.has_toc: self.run_latex(source_filename, log_file) finally: log_file.close() if not os.path.exists(target_filename): # something went terribly wrong, no LaTeX file was produced raise LaTeXRuntimeException(source_filename, log_filename) return target_filename
def run(self, template_name, **kwargs): template_dir = os.path.join(get_root_path('indico'), 'legacy/webinterface/tpls/latex') template = tpl_render(os.path.join(template_dir, template_name), kwargs) self._dir = tempfile.mkdtemp(prefix="indico-texgen-", dir=config.TEMP_DIR) chmod_umask(self._dir, execute=True) source_filename = os.path.join(self._dir, template_name + '.tex') target_filename = os.path.join(self._dir, template_name + '.pdf') log_filename = os.path.join(self._dir, 'output.log') log_file = open(log_filename, 'a+') with open(source_filename, 'w') as f: f.write(template) try: self.run_latex(source_filename, log_file) if self.has_toc: self.run_latex(source_filename, log_file) finally: log_file.close() if not os.path.exists(target_filename): # something went terribly wrong, no LaTeX file was produced raise LaTeXRuntimeException(source_filename, log_filename) return target_filename
def _generate_zip_file(self, files_holder, name_prefix='material', name_suffix=None, return_file=False): """Generate a zip file containing the files passed. :param files_holder: An iterable (or an iterable containing) object that contains the files to be added in the zip file. :param name_prefix: The prefix to the zip file name :param name_suffix: The suffix to the zip file name :param return_file: Return the temp file instead of a response """ temp_file = NamedTemporaryFile(suffix='.zip', dir=config.TEMP_DIR, delete=(not return_file)) with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler: self.used_filenames = set() for item in self._iter_items(files_holder): name = self._prepare_folder_structure(item) self.used_filenames.add(name) with item.get_local_path() as filepath: zip_handler.write(filepath, name) zip_file_name = f'{name_prefix}-{name_suffix}.zip' if name_suffix else f'{name_prefix}.zip' chmod_umask(temp_file.name) if return_file: return temp_file return send_file(zip_file_name, temp_file.name, 'application/zip', inline=False)
def create(self): """Trigger the creation of a ZIP file containing the site.""" temp_file = NamedTemporaryFile(prefix=f'static-{self.event.id}-', suffix='.zip', dir=config.TEMP_DIR, delete=False) self._zip_file = ZipFile(temp_file.name, 'w', allowZip64=True) with collect_static_files() as used_assets: # create the home page html html = self._create_home() # Mathjax plugins can only be known in runtime self._copy_folder(os.path.join(self._content_dir, 'static', 'dist', 'js', 'mathjax'), os.path.join(self._static_dir, 'dist', 'js', 'mathjax')) # Materials and additional pages self._copy_all_material() self._create_other_pages() # Create index.html file (main page for the event) index_path = os.path.join(self._content_dir, 'index.html') self._zip_file.writestr(index_path, html) self._write_generated_js() # Copy static assets to ZIP file self._copy_static_files(used_assets) self._copy_plugin_files(used_assets) if config.CUSTOMIZATION_DIR: self._copy_customization_files(used_assets) chmod_umask(temp_file.name) self._zip_file.close() return temp_file.name
def create(self): """Trigger the creation of a ZIP file containing the site.""" temp_file = NamedTemporaryFile(suffix='indico.tmp', dir=config.TEMP_DIR) self._zip_file = ZipFile(temp_file.name, 'w', allowZip64=True) with collect_static_files() as used_assets: # create the home page html html = self._create_home().encode('utf-8') # Mathjax plugins can only be known in runtime self._copy_folder(os.path.join(self._content_dir, 'static', 'dist', 'js', 'mathjax'), os.path.join(self._static_dir, 'dist', 'js', 'mathjax')) # Materials and additional pages self._copy_all_material() self._create_other_pages() # Create index.html file (main page for the event) index_path = os.path.join(self._content_dir, 'index.html') self._zip_file.writestr(index_path, html) self._write_generated_js() # Copy static assets to ZIP file self._copy_static_files(used_assets) self._copy_plugin_files(used_assets) if config.CUSTOMIZATION_DIR: self._copy_customization_files(used_assets) temp_file.delete = False chmod_umask(temp_file.name) self._zip_file.close() return temp_file.name
def run(self, template_name, **kwargs): template_dir = os.path.join(get_root_path('indico'), 'legacy/webinterface/tpls/latex') template = tpl_render(os.path.join(template_dir, template_name), kwargs) self._dir = tempfile.mkdtemp(prefix="indico-texgen-", dir=config.TEMP_DIR) chmod_umask(self._dir, execute=True) source_filename = os.path.join(self._dir, template_name + '.tex') target_filename = os.path.join(self._dir, template_name + '.pdf') log_filename = os.path.join(self._dir, 'output.log') log_file = open(log_filename, 'a+') with open(source_filename, 'w') as f: f.write(template) try: self.run_latex(source_filename, log_file) if self.has_toc: self.run_latex(source_filename, log_file) finally: log_file.close() if not os.path.exists(target_filename): # something went terribly wrong, no LaTeX file was produced raise LaTeXRuntimeException(source_filename, log_filename) return target_filename
def prepare(self, template_name, **kwargs): chmod_umask(self.source_dir, execute=True) source_filename = os.path.join(self.source_dir, template_name + '.tex') target_filename = os.path.join(self.source_dir, template_name + '.pdf') source = self._render_template(template_name + '.tex', kwargs) with codecs.open(source_filename, 'wb', encoding='utf-8') as f: f.write(source) distribution = pkg_resources.get_distribution('indico-fonts') font_dir = os.path.join(distribution.location, 'indico_fonts') os.symlink(font_dir, os.path.join(self.source_dir, 'fonts')) return source_filename, target_filename
def _generate_zip_file(self, files_holder, name_prefix='material', name_suffix=None): """Generate a zip file containing the files passed. :param files_holder: An iterable (or an iterable containing) object that contains the files to be added in the zip file. :param name_prefix: The prefix to the zip file name :param name_suffix: The suffix to the zip file name :return: The generated zip file. """ temp_file = NamedTemporaryFile(suffix='indico.tmp', dir=config.TEMP_DIR) with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler: self.used_filenames = set() for item in self._iter_items(files_holder): name = self._prepare_folder_structure(item) self.used_filenames.add(name) with item.storage.get_local_path(item.storage_file_id) as filepath: zip_handler.write(filepath.encode('utf-8'), name) temp_file.delete = False zip_file_name = '{}-{}.zip'.format(name_prefix, name_suffix) if name_suffix else '{}.zip'.format(name_prefix) chmod_umask(temp_file.name) return send_file(zip_file_name, temp_file.name, 'application/zip', inline=False)
def run(self, template_name, **kwargs): self._dir = tempfile.mkdtemp(prefix="indico-texgen-", dir=config.TEMP_DIR) chmod_umask(self._dir, execute=True) source_filename = os.path.join(self._dir, template_name + '.tex') target_filename = os.path.join(self._dir, template_name + '.pdf') source = self._render_template(template_name + '.tex', kwargs) with codecs.open(source_filename, 'wb', encoding='utf-8') as f: f.write(source) log_filename = os.path.join(self._dir, 'output.log') log_file = open(log_filename, 'a+') try: self.run_latex(source_filename, log_file) if self.has_toc: self.run_latex(source_filename, log_file) finally: log_file.close() if not os.path.exists(target_filename): # something went terribly wrong, no LaTeX file was produced raise LaTeXRuntimeException(source_filename, log_filename) return target_filename