pytex/PyTeX/format/docstrip_formatter.py

62 lines
2 KiB
Python
Raw Permalink Normal View History

2022-02-18 15:56:57 +01:00
import tempfile
2022-02-18 23:45:55 +01:00
import os
2022-02-18 15:56:57 +01:00
from .enums import TeXType
from .formatting_config import FormattingConfig
from .pytex_formatter import PyTeXFormatter
from pathlib import Path
import subprocess
import shutil
from typing import List, Tuple
class DocStripFormatter(PyTeXFormatter):
2022-02-18 16:27:03 +01:00
@property
2022-02-18 15:56:57 +01:00
def output_files(self) -> List[str]:
if self.config.tex_out_type == TeXType.TeXClass:
2022-02-18 23:45:55 +01:00
return [self.raw_name + '.cls']
elif self.config.tex_out_type == TeXType.TeXPackage:
return [self.raw_name + '.sty']
2022-02-18 15:56:57 +01:00
else:
raise NotImplementedError
def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
tmp_dir: Path = Path(tempfile.mkdtemp())
shutil.copy(
self.input_file,
tmp_dir
)
if self.input_file.with_suffix('.ins').exists():
2022-02-18 23:45:55 +01:00
shutil.copy(
self.input_file.with_suffix('.ins'),
tmp_dir
)
2022-02-18 15:56:57 +01:00
result = subprocess.run(
2022-02-18 23:45:55 +01:00
["tex", self.input_file.with_suffix('.ins').name],
2022-02-18 15:56:57 +01:00
cwd=tmp_dir,
2022-02-18 23:45:55 +01:00
stderr=subprocess.DEVNULL, # TODO
stdout=subprocess.DEVNULL # TODO
2022-02-18 15:56:57 +01:00
)
if not result.returncode == 0:
2022-02-18 23:45:55 +01:00
raise NotImplementedError('no correct returncode')
2022-02-18 15:56:57 +01:00
else:
result = subprocess.run(
['pdflatex', self.input_file.name],
cwd=tmp_dir,
stderr=subprocess.STDOUT
)
if not result.returncode == 0:
raise NotImplementedError
2022-02-18 16:27:03 +01:00
for file in self.output_files:
2022-02-18 15:56:57 +01:00
outfile = tmp_dir / file
if not outfile.exists():
2022-02-18 23:45:55 +01:00
raise NotImplementedError(f'output file {outfile} does not exist')
2022-02-18 15:56:57 +01:00
shutil.copy(outfile, build_dir)
shutil.rmtree(tmp_dir)
return [] # No future config
2022-02-18 16:27:03 +01:00
@property
2022-02-18 15:56:57 +01:00
def dependencies(self) -> List[str]:
return [] # TODO