From ed6494826f6df095635d9660c9f31e0b14aa4d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 30 Jan 2022 14:35:55 +0100 Subject: [PATCH] fix error when building from dtx files wait for subprocess to complete catch some more errors --- build/build.py | 2 +- default_formatters/docstrip_formatter.py | 18 +++++++++++++----- errors/__init__.py | 6 ++++-- errors/errors.py | 20 +++++++++++++++++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/build/build.py b/build/build.py index 5a152bb..f24384f 100644 --- a/build/build.py +++ b/build/build.py @@ -148,7 +148,7 @@ def build( elif not str(file.relative_to(output_dir)) in built_files: if not file.is_dir() and not str(file.relative_to(output_dir)) == 'build_info.json': # PyTeX does not at all know something about this file - raise UnknownFileInBuildDirectory(file.relative_to(output_dir)) + raise UnknownFileInBuildDirectoryError(file.relative_to(output_dir)) if write_build_information: with open(output_dir / 'build_info.json', 'w') as f: diff --git a/default_formatters/docstrip_formatter.py b/default_formatters/docstrip_formatter.py index 69ceec5..aa7ecbf 100644 --- a/default_formatters/docstrip_formatter.py +++ b/default_formatters/docstrip_formatter.py @@ -4,6 +4,7 @@ import subprocess from PyTeX.formatter import Formatter from PyTeX.utils import ensure_file_integrity +from PyTeX.errors import FileNotGeneratedFromDTXFileError, LatexMKError class DocstripFormatter(Formatter): @@ -19,13 +20,20 @@ class DocstripFormatter(Formatter): relative_name: Optional[str] = None, last_build_info: Optional[List[Dict]] = None) -> List[str]: ensure_file_integrity(output_dir / self.filename, str(Path(relative_name).parent / self.filename), last_build_info) - result = subprocess.Popen(['pdflatex', self.name + '.dtx'], cwd=str(input_path.parent), - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL - ) + try: + result = subprocess.run(['latexmk', '-gg', '-output-directory=/tmp'], cwd=str(input_path.parent), + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + except subprocess.CalledProcessError: + raise LatexMKError(self.filename) if not result: pass - sty = input_path.with_suffix('.sty').read_text() + sty_file = (Path('/tmp') / input_path.with_suffix('.sty').name) + if sty_file.exists(): + sty = sty_file.read_text() + else: + raise FileNotGeneratedFromDTXFileError(self.filename, 'style') output_dir.mkdir(parents=True, exist_ok=True) (output_dir / self.filename).write_text(sty) return [self.filename] diff --git a/errors/__init__.py b/errors/__init__.py index 3d4a68a..777779e 100644 --- a/errors/__init__.py +++ b/errors/__init__.py @@ -1,6 +1,6 @@ from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \ UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryNoOverwriteError, \ - UnknownFileInBuildDirectory + UnknownFileInBuildDirectoryError, LatexMKError, FileNotGeneratedFromDTXFileError __all__ = [ 'PyTexError', @@ -10,5 +10,7 @@ __all__ = [ 'UnknownTexVersionError', 'ModifiedFileInBuildDirectoryError', 'UnknownFileInBuildDirectoryNoOverwriteError', - 'UnknownFileInBuildDirectory' + 'UnknownFileInBuildDirectoryError', + 'LatexMKError', + 'FileNotGeneratedFromDTXFileError' ] diff --git a/errors/errors.py b/errors/errors.py index e9901c4..b680aa9 100644 --- a/errors/errors.py +++ b/errors/errors.py @@ -56,10 +56,28 @@ class UnknownFileInBuildDirectoryNoOverwriteError(PyTexError): ) -class UnknownFileInBuildDirectory(PyTexError): +class UnknownFileInBuildDirectoryError(PyTexError): def __init__(self, filename): super().__init__( f"Detected unknown file {filename} in build directory." f"PyTeX has no knowledge about this, you should probably" f"remove it." ) + + +class LatexMKError(PyTexError): + def __init__(self, filename): + super().__init__( + f"Running latexmk on file {filename} resulted in an error. " + f"Make sure this file is well-formed and an appropriate " + f"'.latexmkrc' is present in its directory." + ) + + +class FileNotGeneratedFromDTXFileError(PyTexError): + def __init__(self, filename, type): + super().__init__( + f"Running latexmk on {filename} did not produce a LaTeX " + f"{type} file as needed. I do not know how to build " + f"a {type} from this '.dtx' file." + )