update builder
This commit is contained in:
parent
5f8411d907
commit
096bfd2ae2
1 changed files with 64 additions and 15 deletions
|
@ -1,5 +1,5 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Union, List, Tuple
|
from typing import Optional, Union, List, Tuple, Set
|
||||||
|
|
||||||
from .enums import PyTeXRootDirType
|
from .enums import PyTeXRootDirType
|
||||||
from .pytex_config import PyTeXConfig
|
from .pytex_config import PyTeXConfig
|
||||||
|
@ -9,6 +9,7 @@ from ..versioning.version_info.version_info import VersionInfo, FileVersionInfo
|
||||||
from .pytex_file import PyTeXSourceFile
|
from .pytex_file import PyTeXSourceFile
|
||||||
from .relative_path import RelativePath
|
from .relative_path import RelativePath
|
||||||
from .hashing import md5
|
from .hashing import md5
|
||||||
|
from .pytex_output_file import PyTeXOutputFile
|
||||||
|
|
||||||
|
|
||||||
class PyTeXBuilder:
|
class PyTeXBuilder:
|
||||||
|
@ -26,17 +27,19 @@ class PyTeXBuilder:
|
||||||
if isinstance(pytex_config, Path):
|
if isinstance(pytex_config, Path):
|
||||||
self._root_dir = pytex_config.parent
|
self._root_dir = pytex_config.parent
|
||||||
else:
|
else:
|
||||||
self._root_dir: Path = Path('.') # Working directory
|
self._root_dir = Path('.') # Working directory
|
||||||
else:
|
else:
|
||||||
self._root_dir = root_dir
|
self._root_dir = root_dir
|
||||||
|
|
||||||
# Non-public attributes
|
# Non-public attributes
|
||||||
self._build_target_type: Optional[PyTeXRootDirType] = None
|
self._build_target_type: Optional[PyTeXRootDirType] = None
|
||||||
self._old_version_info: Optional[VersionInfo] = None
|
self._old_version_info: Optional[VersionInfo] = None
|
||||||
self._modified_old_version_info: VersionInfo = VersionInfo()
|
self._output_files: List[PyTeXOutputFile] = []
|
||||||
self._pytex_files: List[PyTeXSourceFile] = []
|
self._pytex_files: List[PyTeXSourceFile] = []
|
||||||
self._files_to_clean: List[RelativePath] = []
|
self._files_to_clean: Set[RelativePath] = set()
|
||||||
self._files_to_overwrite: List[RelativePath] = []
|
self._files_to_overwrite: Set[RelativePath] = set()
|
||||||
|
self._files_to_build: Set[PyTeXSourceFile] = set()
|
||||||
|
self._tmp_dir: Path = self._root_dir / '.pytex'
|
||||||
|
|
||||||
def build_tex_sources(self) -> bool:
|
def build_tex_sources(self) -> bool:
|
||||||
self._build_target_type = PyTeXRootDirType.TEX_SOURCE
|
self._build_target_type = PyTeXRootDirType.TEX_SOURCE
|
||||||
|
@ -156,6 +159,19 @@ class PyTeXBuilder:
|
||||||
else:
|
else:
|
||||||
return FileVersionInfo()
|
return FileVersionInfo()
|
||||||
|
|
||||||
|
def _pytex_file_lookup(self, name: str) -> PyTeXSourceFile:
|
||||||
|
matches = [
|
||||||
|
source_file
|
||||||
|
for source_file in self._pytex_files
|
||||||
|
if source_file.relative_path.path.name == name
|
||||||
|
]
|
||||||
|
if len(matches) >= 2:
|
||||||
|
raise NotImplementedError
|
||||||
|
elif len(matches) == 1:
|
||||||
|
return matches[0]
|
||||||
|
else:
|
||||||
|
raise NotImplementedError # what to do in this case? dependency does not exist...
|
||||||
|
|
||||||
def _check_output_directory_integrity(self):
|
def _check_output_directory_integrity(self):
|
||||||
out_dir_files = [
|
out_dir_files = [
|
||||||
RelativePath(self.target_root, file)
|
RelativePath(self.target_root, file)
|
||||||
|
@ -168,25 +184,58 @@ class PyTeXBuilder:
|
||||||
raise NotImplementedError # Not ok
|
raise NotImplementedError # Not ok
|
||||||
else:
|
else:
|
||||||
if self.pytex_config.overwrite_existing_files:
|
if self.pytex_config.overwrite_existing_files:
|
||||||
self._files_to_overwrite.append(file)
|
self._files_to_overwrite.add(file)
|
||||||
else:
|
else:
|
||||||
pass
|
if file.relative_path in \
|
||||||
|
{x.relative_path.relative_path for x in self._files_to_build}:
|
||||||
|
raise NotImplementedError
|
||||||
# Not ok iff we are going to write this file
|
# Not ok iff we are going to write this file
|
||||||
|
|
||||||
def _update_old_version_info(self):
|
def _dependencies_hash(self, pytex_output_file: PyTeXOutputFile) -> str:
|
||||||
self._modified_old_version_info.files = [] # Make sure this is set so we can get references on it
|
t = pytex_output_file.dependencies
|
||||||
|
deps: Set[str] = set(t)
|
||||||
|
deps.add(pytex_output_file.source_file.relative_path.path.name)
|
||||||
|
hashes = set()
|
||||||
|
for dep in deps:
|
||||||
|
hashes.add(
|
||||||
|
self._pytex_file_lookup(dep).file_hash
|
||||||
|
)
|
||||||
|
return md5(hashes)
|
||||||
|
|
||||||
|
def _init_output_files(self):
|
||||||
for source_file in self._pytex_files:
|
for source_file in self._pytex_files:
|
||||||
for output_file in source_file.output_files:
|
for output_file in source_file.output_files:
|
||||||
self._modified_old_version_info.files.append(
|
self._output_files.append(
|
||||||
self._old_version_lookup(
|
PyTeXOutputFile(
|
||||||
output_file
|
output_file=output_file,
|
||||||
|
source_file=source_file,
|
||||||
|
last_version_info=self._old_version_lookup(output_file)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _compute_files_to_build(self):
|
||||||
|
self._files_to_build = {
|
||||||
|
output_file.source_file for output_file in self._output_files
|
||||||
|
if self._dependencies_hash(output_file) != output_file.last_version_info.sources_hash
|
||||||
|
or output_file.last_version_info.file_hash != output_file.file_hash
|
||||||
|
}
|
||||||
|
|
||||||
|
def _build_files(self):
|
||||||
|
for source_file in self._files_to_build:
|
||||||
|
import os
|
||||||
|
out_dir = self._tmp_dir / source_file.file_hash
|
||||||
|
out_dir.mkdir(exist_ok=False, parents=True)
|
||||||
|
source_file.format(self._tmp_dir / source_file.file_hash)
|
||||||
|
|
||||||
def _build(self) -> bool:
|
def _build(self) -> bool:
|
||||||
logger.info("Starting build")
|
logger.info("Starting build")
|
||||||
self._load_pytex_files()
|
self._load_pytex_files()
|
||||||
logger.info(f"Found {len(self._pytex_files)} source files")
|
logger.info(f"Found {len(self._pytex_files)} source files")
|
||||||
self._update_old_version_info()
|
self._init_output_files()
|
||||||
|
logger.info(f"Found {len(self._output_files)} potential files to build.")
|
||||||
|
self._compute_files_to_build()
|
||||||
|
logger.info(f"Needing to build {len(self._files_to_build)} many files.")
|
||||||
|
self._check_output_directory_integrity()
|
||||||
|
logger.info(f"Starting build")
|
||||||
|
self._build_files()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue