fix error when building from dtx files

wait for subprocess to complete
    catch some more errors
This commit is contained in:
Maximilian Keßler 2022-01-30 14:35:55 +01:00
parent e661ceee64
commit ed6494826f
4 changed files with 37 additions and 9 deletions

View file

@ -148,7 +148,7 @@ def build(
elif not str(file.relative_to(output_dir)) in built_files: 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': 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 # 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: if write_build_information:
with open(output_dir / 'build_info.json', 'w') as f: with open(output_dir / 'build_info.json', 'w') as f:

View file

@ -4,6 +4,7 @@ import subprocess
from PyTeX.formatter import Formatter from PyTeX.formatter import Formatter
from PyTeX.utils import ensure_file_integrity from PyTeX.utils import ensure_file_integrity
from PyTeX.errors import FileNotGeneratedFromDTXFileError, LatexMKError
class DocstripFormatter(Formatter): class DocstripFormatter(Formatter):
@ -19,13 +20,20 @@ class DocstripFormatter(Formatter):
relative_name: Optional[str] = None, relative_name: Optional[str] = None,
last_build_info: Optional[List[Dict]] = None) -> List[str]: 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) 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), try:
result = subprocess.run(['latexmk', '-gg', '-output-directory=/tmp'], cwd=str(input_path.parent),
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL stderr=subprocess.DEVNULL
) )
except subprocess.CalledProcessError:
raise LatexMKError(self.filename)
if not result: if not result:
pass 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.mkdir(parents=True, exist_ok=True)
(output_dir / self.filename).write_text(sty) (output_dir / self.filename).write_text(sty)
return [self.filename] return [self.filename]

View file

@ -1,6 +1,6 @@
from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \ from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \
UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryNoOverwriteError, \ UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryNoOverwriteError, \
UnknownFileInBuildDirectory UnknownFileInBuildDirectoryError, LatexMKError, FileNotGeneratedFromDTXFileError
__all__ = [ __all__ = [
'PyTexError', 'PyTexError',
@ -10,5 +10,7 @@ __all__ = [
'UnknownTexVersionError', 'UnknownTexVersionError',
'ModifiedFileInBuildDirectoryError', 'ModifiedFileInBuildDirectoryError',
'UnknownFileInBuildDirectoryNoOverwriteError', 'UnknownFileInBuildDirectoryNoOverwriteError',
'UnknownFileInBuildDirectory' 'UnknownFileInBuildDirectoryError',
'LatexMKError',
'FileNotGeneratedFromDTXFileError'
] ]

View file

@ -56,10 +56,28 @@ class UnknownFileInBuildDirectoryNoOverwriteError(PyTexError):
) )
class UnknownFileInBuildDirectory(PyTexError): class UnknownFileInBuildDirectoryError(PyTexError):
def __init__(self, filename): def __init__(self, filename):
super().__init__( super().__init__(
f"Detected unknown file {filename} in build directory." f"Detected unknown file {filename} in build directory."
f"PyTeX has no knowledge about this, you should probably" f"PyTeX has no knowledge about this, you should probably"
f"remove it." 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."
)