ensure file integrity when overwriting files also in dictionary case
This commit is contained in:
parent
f8a6f83de9
commit
bfd0f6e01b
4 changed files with 29 additions and 12 deletions
|
@ -5,6 +5,7 @@ from datetime import *
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
from PyTeX.formatter import Formatter
|
from PyTeX.formatter import Formatter
|
||||||
|
from PyTeX.utils import ensure_file_integrity
|
||||||
|
|
||||||
|
|
||||||
class DictionaryFormatter(Formatter):
|
class DictionaryFormatter(Formatter):
|
||||||
|
@ -74,7 +75,11 @@ class DictionaryFormatter(Formatter):
|
||||||
translation=translations[lang][key].strip()
|
translation=translations[lang][key].strip()
|
||||||
)
|
)
|
||||||
lang_lines += '\n'
|
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))
|
(output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines))
|
||||||
written_files.append(self.file_name.format(language=lang))
|
written_files.append(self.file_name.format(language=lang))
|
||||||
return written_files
|
return written_files
|
||||||
|
|
|
@ -6,7 +6,7 @@ from datetime import *
|
||||||
|
|
||||||
from PyTeX.base import Attributes, Args
|
from PyTeX.base import Attributes, Args
|
||||||
from PyTeX.errors import *
|
from PyTeX.errors import *
|
||||||
from PyTeX.utils import md5
|
from PyTeX.utils import ensure_file_integrity
|
||||||
|
|
||||||
from .formatter import Formatter
|
from .formatter import Formatter
|
||||||
|
|
||||||
|
@ -133,15 +133,7 @@ class TexFormatter(Formatter):
|
||||||
if output_dir is None:
|
if output_dir is None:
|
||||||
output_dir = input_path.parent
|
output_dir = input_path.parent
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
if (output_dir / self.file_name).exists():
|
ensure_file_integrity(output_dir / self.file_name, str(Path(relative_name).parent / self.file_name), last_build_info)
|
||||||
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))
|
|
||||||
|
|
||||||
(output_dir / self.file_name).write_text(''.join(newlines))
|
(output_dir / self.file_name).write_text(''.join(newlines))
|
||||||
return [str(self.file_name)]
|
return [str(self.file_name)]
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from. checksum import md5
|
from. checksum import md5
|
||||||
|
from .file_integrity import ensure_file_integrity
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'md5'
|
'md5',
|
||||||
|
'ensure_file_integrity'
|
||||||
]
|
]
|
||||||
|
|
18
utils/file_integrity.py
Normal file
18
utils/file_integrity.py
Normal 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))
|
Loading…
Reference in a new issue