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 pathlib import Path
from typing import Optional, Union, List, Tuple, Set from typing import Optional, Union, List, Tuple, Set
import shutil
from .enums import PyTeXRootDirType from .enums import PyTeXRootDirType
from .pytex_config import PyTeXConfig from .pytex_config import PyTeXConfig
@ -173,23 +174,24 @@ class PyTeXBuilder:
raise NotImplementedError # what to do in this case? dependency does not exist... 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: List[RelativePath] = [
RelativePath(self.target_root, file) RelativePath(self.target_root, file)
for file in self.target_root.rglob('*') for file in self.target_root.rglob('*')
] ]
for file in out_dir_files: for file in out_dir_files:
version = self._old_version_lookup(file) if not file.relative_path.is_dir():
if version.file_hash != md5(file.path): version = self._old_version_lookup(file)
if self.pytex_config.clean_old_files: if version.file_hash != md5(file.path):
raise NotImplementedError # Not ok if self.pytex_config.clean_old_files:
else: raise NotImplementedError # Not ok
if self.pytex_config.overwrite_existing_files:
self._files_to_overwrite.add(file)
else: else:
if file.relative_path in \ if self.pytex_config.overwrite_existing_files:
{x.relative_path.relative_path for x in self._files_to_build}: self._files_to_overwrite.add(file)
raise NotImplementedError else:
# Not ok iff we are going to write this file 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: def _dependencies_hash(self, pytex_output_file: PyTeXOutputFile) -> str:
t = pytex_output_file.dependencies t = pytex_output_file.dependencies
@ -222,11 +224,22 @@ class PyTeXBuilder:
def _build_files(self): def _build_files(self):
for source_file in self._files_to_build: for source_file in self._files_to_build:
import os
out_dir = self._tmp_dir / source_file.file_hash out_dir = self._tmp_dir / source_file.file_hash
out_dir.mkdir(exist_ok=False, parents=True) out_dir.mkdir(exist_ok=False, parents=True)
source_file.format(self._tmp_dir / source_file.file_hash) 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: def _build(self) -> bool:
logger.info("Starting build") logger.info("Starting build")
self._load_pytex_files() self._load_pytex_files()
@ -238,4 +251,5 @@ class PyTeXBuilder:
self._check_output_directory_integrity() self._check_output_directory_integrity()
logger.info(f"Starting build") logger.info(f"Starting build")
self._build_files() self._build_files()
self._move_files()
return True return True

View file

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