fix some small bugs: main building mechanism now working

This commit is contained in:
Maximilian Keßler 2022-02-09 00:13:34 +01:00
parent ec26ec232f
commit 1bcd6040fb
2 changed files with 28 additions and 14 deletions

View file

@ -1,5 +1,6 @@
from pathlib import Path
from typing import Optional, Union, List, Tuple, Set
import shutil
from .enums import PyTeXRootDirType
from .pytex_config import PyTeXConfig
@ -173,23 +174,24 @@ class PyTeXBuilder:
raise NotImplementedError # what to do in this case? dependency does not exist...
def _check_output_directory_integrity(self):
out_dir_files = [
out_dir_files: List[RelativePath] = [
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.add(file)
if not file.relative_path.is_dir():
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 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
if self.pytex_config.overwrite_existing_files:
self._files_to_overwrite.add(file)
else:
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
def _dependencies_hash(self, pytex_output_file: PyTeXOutputFile) -> str:
t = pytex_output_file.dependencies
@ -222,11 +224,22 @@ class PyTeXBuilder:
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 _move_files(self):
for source_file in self._files_to_build:
tmp_dir = self._tmp_dir / source_file.file_hash
for filename in source_file.output_files:
out_dir = self.target_root / source_file.relative_path.relative_path.parent
out_dir.mkdir(parents=True, exist_ok=True)
shutil.move(
tmp_dir / filename.relative_path.name,
out_dir / filename.relative_path.name
)
shutil.rmtree(self._tmp_dir)
def _build(self) -> bool:
logger.info("Starting build")
self._load_pytex_files()
@ -238,4 +251,5 @@ class PyTeXBuilder:
self._check_output_directory_integrity()
logger.info(f"Starting build")
self._build_files()
self._move_files()
return True

View file

@ -53,7 +53,7 @@ class PyTeXSourceFile:
raise NotImplementedError # TODO
files: List[str] = self._formatter.output_files
paths = [
self._relative_path.with_name(filename)
RelativePath(self._relative_path.root_dir, self._relative_path.with_name(filename))
for filename in files
]
return paths