detect unknown files in build dir

This commit is contained in:
Maximilian Keßler 2022-01-13 21:12:35 +01:00
parent 2cad289b32
commit 60488127e5
4 changed files with 21 additions and 6 deletions

View file

@ -141,6 +141,10 @@ def build(
if str(file.relative_to(output_dir)) not in built_files: if str(file.relative_to(output_dir)) not in built_files:
print(f'[PyTeX] Removing old built file {str(file.relative_to(output_dir))}') print(f'[PyTeX] Removing old built file {str(file.relative_to(output_dir))}')
file.unlink() file.unlink()
elif not str(file.relative_to(output_dir)) in built_files:
if not file.is_dir() and not str(file.relative_to(output_dir)) == 'build_info.json':
# PyTeX does not at all know something about this file
raise UnknownFileInBuildDirectory(file.relative_to(output_dir))
if write_build_information: if write_build_information:
with open(output_dir / 'build_info.json', 'w') as f: with open(output_dir / 'build_info.json', 'w') as f:

View file

@ -1,5 +1,6 @@
from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \ from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \
UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryError UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryNoOverwriteError, \
UnknownFileInBuildDirectory
__all__ = [ __all__ = [
'PyTexError', 'PyTexError',
@ -8,5 +9,6 @@ __all__ = [
'ExtraHeaderFileNotFoundError', 'ExtraHeaderFileNotFoundError',
'UnknownTexVersionError', 'UnknownTexVersionError',
'ModifiedFileInBuildDirectoryError', 'ModifiedFileInBuildDirectoryError',
'UnknownFileInBuildDirectoryError' 'UnknownFileInBuildDirectoryNoOverwriteError',
'UnknownFileInBuildDirectory'
] ]

View file

@ -45,7 +45,7 @@ class ModifiedFileInBuildDirectoryError(PyTexError):
) )
class UnknownFileInBuildDirectoryError(PyTexError): class UnknownFileInBuildDirectoryNoOverwriteError(PyTexError):
def __init__(self, filename: str): def __init__(self, filename: str):
super().__init__( super().__init__(
f"Unknown file {filename} in build directory found. " f"Unknown file {filename} in build directory found. "
@ -53,4 +53,13 @@ class UnknownFileInBuildDirectoryError(PyTexError):
f"Refusing to overwrite this file, since you could lose your data. " f"Refusing to overwrite this file, since you could lose your data. "
f"If you are sure, this can be got rid of, delete the file manually, " f"If you are sure, this can be got rid of, delete the file manually, "
f"and run the build again." f"and run the build again."
) )
class UnknownFileInBuildDirectory(PyTexError):
def __init__(self, filename):
super().__init__(
f"Detected unknown file {filename} in build directory."
f"PyTeX has no knowledge about this, you should probably"
f"remove it."
)

View file

@ -7,7 +7,7 @@ from typing import Optional, List, Dict
def ensure_file_integrity(file: Path, output_file_name: str, build_info: Optional[List[Dict]] = None): def ensure_file_integrity(file: Path, output_file_name: str, build_info: Optional[List[Dict]] = None):
if file.exists(): if file.exists():
if not build_info: if not build_info:
raise UnknownFileInBuildDirectoryError(str(file)) raise UnknownFileInBuildDirectoryNoOverwriteError(str(file))
found = False found = False
for info in build_info: for info in build_info:
if info['name'] == output_file_name: if info['name'] == output_file_name:
@ -15,4 +15,4 @@ def ensure_file_integrity(file: Path, output_file_name: str, build_info: Optiona
raise ModifiedFileInBuildDirectoryError(str(file)) raise ModifiedFileInBuildDirectoryError(str(file))
found = True found = True
if not found: if not found:
raise UnknownFileInBuildDirectoryError(str(file)) raise UnknownFileInBuildDirectoryNoOverwriteError(str(file))