commit 80b7514f49f76eb3adb358b25a1272e5456c43ab Author: Florian Best Date: Wed Aug 12 16:53:48 2020 +0200 Bug #51812: make it possible to run scripts diff --git a/management/univention-directory-reports/modules/univention/directory/reports/document.py b/management/univention-directory-reports/modules/univention/directory/reports/document.py index 6ce31ab87e..e15ad2d4a9 100644 --- a/management/univention-directory-reports/modules/univention/directory/reports/document.py +++ b/management/univention-directory-reports/modules/univention/directory/reports/document.py @@ -52,7 +52,7 @@ _ = Translation('univention-directory-reports').translate class Document(object): - (TYPE_LATEX, TYPE_CSV, TYPE_RML, TYPE_UNKNOWN) = range(4) + (TYPE_LATEX, TYPE_CSV, TYPE_RML, TYPE_SCRIPT, TYPE_UNKNOWN) = range(5) @classmethod def get_type(cls, template): @@ -62,6 +62,8 @@ class Document(object): return cls.TYPE_CSV elif template.endswith('.rml'): return cls.TYPE_RML + elif template.endswith('.py') or template.endswith('.sh'): + return cls.TYPE_SCRIPT return cls.TYPE_UNKNOWN def __init__(self, template, header=None, footer=None): @@ -88,7 +90,7 @@ class Document(object): elif self._type == Document.TYPE_CSV: suffix = '.csv' else: - suffix = self._template.rsplit('.', 1)[1] + suffix = '.' + self._template.rsplit('.', 1)[1] fd, filename = tempfile.mkstemp(suffix, 'univention-directory-reports-') os.chmod(filename, 0o644) os.close(fd) @@ -138,7 +140,7 @@ class Document(object): return tmpfile - def create_pdf(self, latex_file): + def create_pdf(self, latex_file, objects): """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') @@ -157,7 +159,7 @@ class Document(object): except EnvironmentError: pass - def create_rml_pdf(self, rml_file): + def create_rml_pdf(self, rml_file, objects): output = '%s.pdf' % (os.path.splitext(rml_file)[0],) with open(rml_file, 'rb') as fd: outputfile = trml2pdf.parseString(fd.read(), output) @@ -166,3 +168,9 @@ class Document(object): except EnvironmentError: pass return outputfile + + def create_from_script(self, script_file, objects): + output = '%s.csv' % (os.path.splitext(script_file)[0],) + with open(output, 'w') as fd: + fd.write(subprocess.check_output([sys.executable, script_file] + objects)) + return output diff --git a/management/univention-directory-reports/modules/univention/directory/reports/report.py b/management/univention-directory-reports/modules/univention/directory/reports/report.py index 28527bb478..6625a538ae 100644 --- a/management/univention-directory-reports/modules/univention/directory/reports/report.py +++ b/management/univention-directory-reports/modules/univention/directory/reports/report.py @@ -61,16 +61,25 @@ class Report(object): 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' + suffix = { + Document.TYPE_RML: '.rml', + Document.TYPE_SCRIPT: '.py', + #Document.TYPE_SCRIPT: '.sh', + + }.get(Document.get_type(template), '.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) + func = { + Document.TYPE_RML: doc.create_rml_pdf, + Document.TYPE_LATEX: doc.create_pdf, + Document.TYPE_SCRIPT: doc.create_from_script, + }.get(doc._type) if func: - pdffile = func(tmpfile) + pdffile = func(tmpfile, objects) if not pdffile or not os.path.exists(pdffile): raise ReportError(_('The report could not be created.')) return pdffile