diff --git a/build/build.py b/build/build.py index f43e99f..8cbe920 100644 --- a/build/build.py +++ b/build/build.py @@ -27,6 +27,7 @@ def build( overwrite_existing_files: bool = False, # output control build_all: bool = False, # output control / versioning write_build_information: bool = True, # meta + clean_old_files: bool = False ): pytex_msg('Getting git repository information...') if extra_header: @@ -90,10 +91,10 @@ def build( sources_to_build = [] for file in files: if last_build_info: - last_build_info_for_this_file = next( - (info for info in last_build_info['tex_sources'] if info['source file'] == file.name), {}) + last_build_info_for_this_file =\ + list(filter(lambda i: i['source file'] == file.name, last_build_info['tex_sources'])) else: - last_build_info_for_this_file = None + last_build_info_for_this_file = [] sources_to_build.append( TexFileToFormat( src_path=file, @@ -124,8 +125,9 @@ def build( } for source in sources_to_build: - info = source.format() - info_dict['tex_sources'].append(info) + infos = source.format() + for info in infos: + info_dict['tex_sources'].append(info) if write_build_information: with open(output_dir / 'build_info.json', 'w') as f: diff --git a/build/build_parser.py b/build/build_parser.py index b400ea3..9d614f2 100644 --- a/build/build_parser.py +++ b/build/build_parser.py @@ -105,6 +105,13 @@ def parse_and_build(arglist: [str]): type=pathlib.Path, dest='extra_header' ) + parser.add_argument( + '-c', '--clean-old-files', + help='Cleans old files present in build order that are not present in the sources anymore. ' + 'Setting this option guarantees that the build directory will be equivalent as if a ' + 'clean new build has been made (Build metadata might differ).', + action='store_true' + ) args = vars(parser.parse_args(arglist)) for arg in args.keys(): if type(args[arg]) == pathlib.PosixPath: diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 2f04c4b..f59d09b 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -16,7 +16,7 @@ class TexFileToFormat: build_dir: Path, latex_name: str, current_build_info: BuildInfo, - last_build_info: Optional[dict], + last_build_info: Optional[List[dict]], allow_dirty: bool = False, overwrite_existing_files: bool = False, build_all: bool = False): @@ -24,7 +24,8 @@ class TexFileToFormat: self.build_path = build_dir self.latex_name = latex_name # Still an identifier on how to name the package when being formatted self.current_build_info = current_build_info - self.last_build_info = last_build_info + self.last_build_info_all = last_build_info + self.last_build_info = self.last_build_info_all[0] if self.last_build_info_all else None self.allow_dirty = allow_dirty self.overwrite_existing_files: overwrite_existing_files self.build_all = build_all @@ -49,7 +50,7 @@ class TexFileToFormat: self.recent = False self.pytex_recent = False - def format(self) -> dict: + def format(self) -> List[dict]: if self.dirty or self.pytex_dirty: if not self.allow_dirty: raise SubmoduleDirtyForbiddenError @@ -62,7 +63,7 @@ class TexFileToFormat: elif self.last_build_info and self.last_build_info['dirty']: return self.__format() # Build file since we do not know in what state it is else: - return self.last_build_info + return [self.last_build_info] def __format_header(self): new_header = [] @@ -82,7 +83,7 @@ class TexFileToFormat: )) self._header = new_header - def __format(self) -> dict: + def __format(self) -> List[dict]: if self.src_path.name.endswith('.pysty'): formatter = PackageFormatter( package_name=self.src_path.with_suffix('').name, @@ -123,17 +124,20 @@ class TexFileToFormat: ) else: raise ProgrammingError - pytex_msg('Writing file {}'.format(formatter.file_name)) formatter.make_default_macros() - formatter.format_file(self.src_path, self.build_path) - info = { - 'name': formatter.file_name, - 'source file': self.src_path.name, - 'build time': self.current_build_info.build_time, - 'source version': self.current_build_info.packages_version, - 'source commit hash': self.current_build_info.packages_hash, - 'pytex version': self.current_build_info.pytex_version, - 'pytex commit hash': self.current_build_info.pytex_hash, - 'dirty': self.dirty - } - return info + written_files = formatter.format_file(self.src_path, self.build_path) + build_infos = [] + for written_file in written_files: + info = { + 'name': written_file, + 'source file': self.src_path.name, + 'build time': self.current_build_info.build_time, + 'source version': self.current_build_info.packages_version, + 'source commit hash': self.current_build_info.packages_hash, + 'pytex version': self.current_build_info.pytex_version, + 'pytex commit hash': self.current_build_info.pytex_hash, + 'dirty': self.dirty + } + build_infos.append(info) + pytex_msg('Written file {}'.format(written_file)) + return build_infos diff --git a/default_formatters/dictionary_formatter.py b/default_formatters/dictionary_formatter.py index a513916..1bc5553 100644 --- a/default_formatters/dictionary_formatter.py +++ b/default_formatters/dictionary_formatter.py @@ -4,8 +4,10 @@ from typing import Dict, Optional, List from datetime import * import csv +from PyTeX.formatter import Formatter -class DictionaryFormatter: + +class DictionaryFormatter(Formatter): def __init__(self, kind: str, author: str, header: Optional[List[str]]): self.header = header self.kind = kind.lower() @@ -22,13 +24,14 @@ class DictionaryFormatter: self.replace_dict: Dict = {} self.arg_replace_dict: Dict = {} self.source_file_name = "not specified" + super().__init__() - @property - def filename(self): + def expected_file_name(self): return self.file_name - def format_file(self, input_path: Path, output_dir: Path = None): + def format_file(self, input_path: Path, output_dir: Path = None) -> List[str]: self.source_file_name = str(input_path.name) + written_files = [] if self.header: lines = '%' * 80 + '\n' \ @@ -67,6 +70,8 @@ class DictionaryFormatter: lang_lines += '\n' (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 def make_default_macros(self): pass diff --git a/formatter/__init__.py b/formatter/__init__.py index 7093d28..2c5ce21 100644 --- a/formatter/__init__.py +++ b/formatter/__init__.py @@ -1,5 +1,6 @@ -from .tex_formatter import TexFormatter +from .tex_formatter import TexFormatter, Formatter __all__ = [ - 'TexFormatter' + 'TexFormatter', + 'Formatter' ] diff --git a/formatter/formatter.py b/formatter/formatter.py new file mode 100644 index 0000000..7708731 --- /dev/null +++ b/formatter/formatter.py @@ -0,0 +1,17 @@ +from pathlib import Path +from typing import List + + +class Formatter: + def __init__(self, *args, **kwargs): + """ Implementation unknown, this is an Interface""" + pass + + def make_default_macros(self) -> None: + pass + + def format_file(self, input_path: Path, output_dir: Path) -> List[str]: + pass + + def expected_file_name(self) -> str: + pass diff --git a/formatter/tex_formatter.py b/formatter/tex_formatter.py index 9afb731..39bfd98 100644 --- a/formatter/tex_formatter.py +++ b/formatter/tex_formatter.py @@ -7,8 +7,10 @@ from datetime import * from PyTeX.base import Attributes, Args from PyTeX.errors import UnknownTexVersionError +from .formatter import Formatter -class TexFormatter: + +class TexFormatter(Formatter): def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str, tex_version: str, version: str, latex_name: str): @@ -36,11 +38,15 @@ class TexFormatter: self.prefix = '__' + self.name_lowercase.replace('-', '_') + '_' else: raise UnknownTexVersionError(tex_version) + super().__init__() @staticmethod def __command_name2keyword(keyword: str): return '__' + keyword.upper().strip().replace(' ', '_') + '__' + def expected_file_name(self): + return self.file_name + @property def filename(self): return self.file_name @@ -106,7 +112,7 @@ class TexFormatter: 'format_kwargs': kwargs } - def format_file(self, input_path: Path, output_dir: Path = None): + def format_file(self, input_path: Path, output_dir: Path = None) -> List[str]: self.source_file_name = str(input_path.name) input_file = input_path.open() lines = input_file.readlines() @@ -122,3 +128,4 @@ class TexFormatter: output_dir = input_path.parent output_dir.mkdir(parents=True, exist_ok=True) (output_dir / self.file_name).write_text(''.join(newlines)) + return [str(self.file_name)]