Пример #1
0
def run_tex_in_directory(source, directory, template_name=None):
    filename = "texput.tex"
    command = getattr(settings, "LATEX_INTERPRETER", DEFAULT_INTERPRETER)
    latex_interpreter_options = getattr(settings, "LATEX_INTERPRETER_OPTIONS",
                                        "")
    with open(os.path.join(directory, filename),
              "x",
              encoding="utf-8",
              newline='') as f:
        f.write(source)
    args = f'cd "{directory}" && {command} -interaction=batchmode {latex_interpreter_options} {filename}'
    try:
        run(args, shell=True, stdout=PIPE, stderr=PIPE, check=True)
        # Run again in order to get marks
        run(args, shell=True, stdout=PIPE, stderr=PIPE, check=True)
    except CalledProcessError as called_process_error:
        try:
            with open(os.path.join(directory, "texput.log"),
                      "r",
                      encoding="utf-8") as f:
                log = f.read()
        except FileNotFoundError:
            raise called_process_error
        else:
            raise TexError(log=log, source=source, template_name=template_name)
    with open(os.path.join(directory, "texput.pdf"), "rb") as f:
        pdf = f.read()
    return pdf
Пример #2
0
def run_tex(source):
    with tempfile.TemporaryDirectory() as tempdir:
        filename = os.path.join(tempdir, 'texput.tex')
        with open(filename, 'x', encoding='utf-8') as f:
            f.write(source)
        latex_interpreter = getattr(settings, 'LATEX_INTERPRETER', DEFAULT_INTERPRETER)
        latex_command = f'cd "{tempdir}" && {latex_interpreter} -interaction=batchmode {os.path.basename(filename)}'
        process = run(latex_command, shell=True, stdout=PIPE, stderr=PIPE)
        process = run(latex_command, shell=True, stdout=PIPE, stderr=PIPE)
        try:
            if process.returncode == 1:
                with open(os.path.join(tempdir, 'texput.log'), encoding='utf8') as f:
                    log = f.read()
                raise TexError(log=log, source=source)
            with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as pdf_file:
                pdf = pdf_file.read()
        except FileNotFoundError:
            if process.stderr:
                raise Exception(process.stderr.decode('utf-8'))
            raise
    return pdf
Пример #3
0
 def _process_tex(self, call_back: Callable):
     """
     Process the tex file. Once finished the resulting dir and file will be given to the call back function
     that performs the last operations (currently either reading the file or printing it)
     :param call_back: callable that takes the parameters (tempdir, pdf_filename)
     :return: return value of call back function
     """
     if not callable(call_back):
         raise ValueError("parameter call_back needs to be callable!")
     with tempfile.TemporaryDirectory() as tempdir:
         filename = os.path.join(tempdir, f'{self.base_filename}.tex')
         with open(filename, 'x', encoding='utf-8') as f:
             f.write(self.source)
         latex_interpreter = getattr(settings, 'LATEX_INTERPRETER',
                                     DEFAULT_INTERPRETER)
         latex_run_count = getattr(settings, 'LATEX_RUN_COUNT',
                                   DEFAULT_RUN_COUNT)
         latex_interpreter_options = getattr(settings,
                                             'LATEX_INTERPRETER_OPTIONS',
                                             '')
         latex_call = f' && {latex_interpreter} -interaction=batchmode {latex_interpreter_options} {os.path.basename(filename)}'
         latex_command = f'cd "{tempdir}" {latex_run_count * latex_call}'
         process = run(latex_command, shell=True, stdout=PIPE, stderr=PIPE)
         try:
             if process.returncode == 1:
                 with open(os.path.join(tempdir, 'texput.log'),
                           encoding='utf8') as f:
                     log = f.read()
                 raise TexError(log=log, source=self.source)
             pdf_temp_file = os.path.join(tempdir,
                                          f'{self.base_filename}.pdf')
             if not os.path.isfile(pdf_temp_file):
                 raise FileNotFoundError(
                     f"File {pdf_temp_file} not found or is not a file!")
             return call_back(tempdir, f'{self.base_filename}.pdf')
         except FileNotFoundError:
             if process.stderr:
                 raise Exception(process.stderr.decode('utf-8'))
             raise