ensure file integrity when overwriting files also in dictionary case

This commit is contained in:
Maximilian Keßler 2022-01-13 20:49:26 +01:00
parent f8a6f83de9
commit bfd0f6e01b
4 changed files with 29 additions and 12 deletions

View file

@ -5,6 +5,7 @@ from datetime import *
import csv
from PyTeX.formatter import Formatter
from PyTeX.utils import ensure_file_integrity
class DictionaryFormatter(Formatter):
@ -74,7 +75,11 @@ class DictionaryFormatter(Formatter):
translation=translations[lang][key].strip()
)
lang_lines += '\n'
ensure_file_integrity(
output_dir / self.file_name.format(language=lang),
str(Path(relative_name).parent / self.file_name.format(language=lang)),
last_build_info
)
(output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines))
written_files.append(self.file_name.format(language=lang))
return written_files

View file

@ -6,7 +6,7 @@ from datetime import *
from PyTeX.base import Attributes, Args
from PyTeX.errors import *
from PyTeX.utils import md5
from PyTeX.utils import ensure_file_integrity
from .formatter import Formatter
@ -133,15 +133,7 @@ class TexFormatter(Formatter):
if output_dir is None:
output_dir = input_path.parent
output_dir.mkdir(parents=True, exist_ok=True)
if (output_dir / self.file_name).exists():
found = False
for info in last_build_info:
if info['source file'] == relative_name:
if not md5(output_dir / self.file_name) == info['md5sum']:
raise ModifiedFileInBuildDirectoryError(str(output_dir / self.file_name))
found = True
if not found:
raise UnknownFileInBuildDirectoryError(str(output_dir / self.file_name))
ensure_file_integrity(output_dir / self.file_name, str(Path(relative_name).parent / self.file_name), last_build_info)
(output_dir / self.file_name).write_text(''.join(newlines))
return [str(self.file_name)]

View file

@ -1,5 +1,7 @@
from. checksum import md5
from .file_integrity import ensure_file_integrity
__all__ = [
'md5'
'md5',
'ensure_file_integrity'
]

18
utils/file_integrity.py Normal file
View file

@ -0,0 +1,18 @@
from pathlib import Path
from PyTeX.errors import *
from .checksum import md5
from typing import Optional, List, Dict
def ensure_file_integrity(file: Path, output_file_name: str, build_info: Optional[List[Dict]] = None):
if file.exists():
if not build_info:
raise UnknownFileInBuildDirectoryError(str(file))
found = False
for info in build_info:
if info['name'] == output_file_name:
if not md5(file) == info['md5sum']:
raise ModifiedFileInBuildDirectoryError(str(file))
found = True
if not found:
raise UnknownFileInBuildDirectoryError(str(file))