implement parts of builder

This commit is contained in:
Maximilian Keßler 2022-02-08 21:57:30 +01:00
parent 0423cb5de8
commit 189e0178c4
3 changed files with 71 additions and 20 deletions

View file

@ -1,13 +1,14 @@
from pathlib import Path
from typing import Optional, Union, List
from typing import Optional, Union, List, Tuple
from .enums import PyTeXRootDirType
from .pytex_config import PyTeXConfig
from ...logger import logger
from .constants import *
from ..versioning.version_info.version_info import VersionInfo
from ..versioning.version_info.version_info import VersionInfo, FileVersionInfo
from .pytex_file import PyTeXSourceFile
from .relative_path import RelativePath
from .hashing import md5
class PyTeXBuilder:
@ -16,7 +17,6 @@ class PyTeXBuilder:
pytex_config: Optional[Union[PyTeXConfig, Path, str]] = None,
root_dir: Optional[Path] = None
):
self._build_target_type: Optional[PyTeXRootDirType] = None
if isinstance(pytex_config, Path) or isinstance(pytex_config, str):
config_file = Path(pytex_config)
self._pytex_config: Optional[PyTeXConfig] = PyTeXConfig.from_yaml(config_file)
@ -31,8 +31,12 @@ class PyTeXBuilder:
self._root_dir = root_dir
# Non-public attributes
self._version_info: Optional[VersionInfo] = None
self._pytex_files: Optional[List[PyTeXSourceFile]] = None
self._build_target_type: Optional[PyTeXRootDirType] = None
self._old_version_info: Optional[VersionInfo] = None
self._modified_old_version_info: VersionInfo = VersionInfo()
self._pytex_files: List[PyTeXSourceFile] = []
self._files_to_clean: List[RelativePath] = []
self._files_to_overwrite: List[RelativePath] = []
def build_tex_sources(self) -> bool:
self._build_target_type = PyTeXRootDirType.TEX_SOURCE
@ -52,16 +56,19 @@ class PyTeXBuilder:
return self._build()
@property
def version_info(self) -> VersionInfo:
if self._version_info is None:
version_info_file = self.target_root / VERSION_INFO_FILE
if version_info_file.exists():
self._version_info = VersionInfo.from_json(
self.target_root / VERSION_INFO_FILE
)
else:
self._version_info = VersionInfo()
return self._version_info
def old_version_info(self) -> VersionInfo:
if self._old_version_info is None:
self._old_version_info = self._get_version_info()
return self._old_version_info
def _get_version_info(self) -> VersionInfo:
version_info_file = self.target_root / VERSION_INFO_FILE
if version_info_file.exists():
return VersionInfo.from_json(
self.target_root / VERSION_INFO_FILE
)
else:
return VersionInfo()
@property
def pytex_config(self) -> PyTeXConfig:
@ -72,6 +79,8 @@ class PyTeXBuilder:
@property
def target_root(self) -> Path:
if self._build_target_type is None:
raise NotImplementedError
return {
PyTeXRootDirType.BUILD: self.pytex_config.build_dir_specification.build_root,
PyTeXRootDirType.DOC: self.pytex_config.build_dir_specification.doc_root,
@ -134,8 +143,50 @@ class PyTeXBuilder:
# TODO: give pytex source file some additional building information
def _old_version_lookup(self, relative_path: RelativePath) -> FileVersionInfo:
matches = [
file_version_info
for file_version_info in self.old_version_info.files
if file_version_info.relative_name == str(relative_path.relative_path)
]
if len(matches) >= 2:
raise NotImplementedError
elif len(matches) == 1:
return matches[0]
else:
return FileVersionInfo()
def _check_output_directory_integrity(self):
out_dir_files = [
RelativePath(self.target_root, file)
for file in self.target_root.rglob('*')
]
for file in out_dir_files:
version = self._old_version_lookup(file)
if version.file_hash != md5(file.path):
if self.pytex_config.clean_old_files:
raise NotImplementedError # Not ok
else:
if self.pytex_config.overwrite_existing_files:
self._files_to_overwrite.append(file)
else:
pass
# Not ok iff we are going to write this file
def _update_old_version_info(self):
self._modified_old_version_info.files = [] # Make sure this is set so we can get references on it
for source_file in self._pytex_files:
for output_file in source_file.output_files:
self._modified_old_version_info.files.append(
self._old_version_lookup(
output_file
)
)
def _build(self) -> bool:
logger.info("Starting build")
self._load_pytex_files()
logger.info(f"Found {len(self._pytex_files)} source files")
self._update_old_version_info()
return True

View file

@ -5,14 +5,16 @@ from .formatting_config import FormattingConfig
class DTXFormatter(TexFormatter):
@property
def dependencies(self) -> List[str]:
pass
return [] # TODO
def future_config(self) -> List[Tuple[str, FormattingConfig]]:
pass
return [] # TODO
@property
def output_files(self) -> List[str]:
pass
return [] # TODO
def open_output_stream(self, build_dir: Path) -> None:
pass

View file

@ -9,8 +9,6 @@ builder = PyTeXBuilder(conf_path)
builder.build_tex_sources()
v = builder.version_info
pass