def create(self, module, report, objects): """Create a report of objects for the specified module in the specified report type format""" connect(access=self.lo) clear_cache() template = self.config.get_report(module, report) if template is None: if not module: raise ReportError(_('Please specify a module.')) if module not in self.config._reports: raise ReportError(_('No report for the specified module %r exists.') % (module,)) if report: raise ReportError(_('The report %r does not exists or is misconfigured.') % (report,)) raise ReportError(_('No %r report exists for the module %r.') % (report, module)) suffix = '.rml' if Document.get_type(template) == Document.TYPE_RML else '.tex' header = self.config.get_header(module, report, suffix) footer = self.config.get_footer(module, report, suffix) doc = Document(template, header=header, footer=footer) tmpfile = doc.create_source(objects) pdffile = tmpfile func = {Document.TYPE_RML: doc.create_rml_pdf, Document.TYPE_LATEX: doc.create_pdf}.get(doc._type) if func: pdffile = func(tmpfile) if not pdffile or not os.path.exists(pdffile): raise ReportError(_('The report could not be created.')) return pdffile
def create_pdf(self, latex_file): """Run pdflatex on latex_file and return path to generated file or None on errors.""" cmd = [ '/usr/bin/pdflatex', '-interaction=nonstopmode', '-halt-on-error', '-output-directory=%s' % os.path.dirname(latex_file), latex_file ] devnull = open(os.path.devnull, 'w') try: env_vars = { 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'HOME': '/var/cache/univention-directory-reports' } if not subprocess.call( cmd, stdout=devnull, stderr=devnull, env=env_vars): if not subprocess.call( cmd, stdout=devnull, stderr=devnull, env=env_vars): return '%s.pdf' % latex_file.rsplit('.', 1)[0] raise ReportError(_('Failed creating PDF file.')) finally: devnull.close() basefile = latex_file.rsplit('.', 1)[0] # strip suffix for file_ in [latex_file] + [ '%s.%s' % (basefile, suffix) for suffix in ('aux', 'log') ]: try: os.unlink(file_) except EnvironmentError: pass
def __check_files(self): if self._type in (Document.TYPE_LATEX, Document.TYPE_RML): files = (self._header, self._footer, self._template) elif self._type == Document.TYPE_CSV: files = (self._template, ) else: files = tuple() for filename in files: if not os.path.isfile(filename): raise ReportError(_("Configuration error: File %r could not be opened.") % (filename,))