pass more attributes to TexFileToFormat class, write dirty / recent information into build file

This commit is contained in:
Maximilian Keßler 2021-10-22 12:56:00 +02:00
parent c24eb9cd25
commit 408a154548

View file

@ -8,6 +8,10 @@ import PyTeX
from .build_information import BuildInfo from .build_information import BuildInfo
from PyTeX.build.git_hook.recent import is_recent
BUILD_INFO_FILENAME = 'build_info.json'
def pytex_msg(msg: str): def pytex_msg(msg: str):
print('[PyTeX] ' + msg) print('[PyTeX] ' + msg)
@ -19,27 +23,37 @@ class TexFileToFormat:
src_path: Path, src_path: Path,
build_dir: Path, build_dir: Path,
latex_name: str, latex_name: str,
build_info: BuildInfo, current_build_info: BuildInfo,
last_build_info: Optional[dict],
allow_dirty: bool = False, allow_dirty: bool = False,
overwrite_existing_files: bool = False, overwrite_existing_files: bool = False,
build_all: bool = False): build_all: bool = False):
self.src_path = src_path self.src_path = src_path
self.build_path = build_dir self.build_path = build_dir
self.tex_name = latex_name # Still an identifier on how to name the package when being formatted self.tex_name = latex_name # Still an identifier on how to name the package when being formatted
self.build_info = build_info self.current_build_info = current_build_info
self.last_build_info = last_build_info
self.allow_dirty = allow_dirty self.allow_dirty = allow_dirty
self.overwrite_existing_files: overwrite_existing_files self.overwrite_existing_files: overwrite_existing_files
self.build_all = build_all self.build_all = build_all
def format(self): self.dirty = is_recent(self.src_path, self.current_build_info.package_repo, compare=None)
self.recent: bool = is_recent(
self.src_path,
self.current_build_info.package_repo,
compare=self.current_build_info.package_repo.commit(self.last_build_info['source commit hash'])
) if self.last_build_info else False
pass
def format(self) -> dict:
if '.pysty' in self.src_path.name: if '.pysty' in self.src_path.name:
formatter = PyTeX.PackageFormatter( formatter = PyTeX.PackageFormatter(
package_name=self.src_path.with_suffix('').name, package_name=self.src_path.with_suffix('').name,
extra_header=self.build_info.header) extra_header=self.current_build_info.header)
elif '.pycls' in self.src_path.name: elif '.pycls' in self.src_path.name:
formatter = PyTeX.ClassFormatter( formatter = PyTeX.ClassFormatter(
class_name=self.src_path.with_suffix('').name, class_name=self.src_path.with_suffix('').name,
extra_header=self.build_info.header) extra_header=self.current_build_info.header)
else: else:
exit(1) exit(1)
pytex_msg('Writing file {}'.format(formatter.file_name)) pytex_msg('Writing file {}'.format(formatter.file_name))
@ -48,12 +62,12 @@ class TexFileToFormat:
info = { info = {
'name': formatter.file_name, 'name': formatter.file_name,
'source file': self.src_path.name, 'source file': self.src_path.name,
'build time': self.build_info.build_time, 'build time': self.current_build_info.build_time,
'source version': self.build_info.packages_version, 'source version': self.current_build_info.packages_version,
'source commit hash': self.build_info.packages_hash, 'source commit hash': self.current_build_info.packages_hash,
'pytex version': self.build_info.pytex_version, 'pytex version': self.current_build_info.pytex_version,
'pytex commit hash': self.build_info.pytex_hash, 'pytex commit hash': self.current_build_info.pytex_hash,
'dirty': False 'dirty': self.dirty
} }
return info return info
@ -90,10 +104,12 @@ def build(
input_dir = src_dir if src_dir else input_file.parent input_dir = src_dir if src_dir else input_file.parent
output_dir = build_dir if build_dir else input_file.parent output_dir = build_dir if build_dir else input_file.parent
with open(output_dir / 'build_info.json') as f: last_build_info_file = output_dir / BUILD_INFO_FILENAME
old_build_info = json.load(f) if last_build_info_file.exists():
# extra_header += ['WARNING: Local changes to git repository detected.', with open(output_dir / 'build_info.json', 'r') as f:
# ' The build will not be reproducible (!)'] last_build_info = json.load(f)
else:
last_build_info = None
files = [] files = []
if input_file: if input_file:
@ -112,12 +128,18 @@ def build(
sources_to_build = [] sources_to_build = []
for file in files: 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), {})
else:
last_build_info_for_this_file = None
sources_to_build.append( sources_to_build.append(
TexFileToFormat( TexFileToFormat(
src_path=file, src_path=file,
build_dir=output_dir / file.parent.relative_to(input_dir), build_dir=output_dir / file.parent.relative_to(input_dir),
latex_name=latex_name, latex_name=latex_name,
build_info=current_build_info, current_build_info=current_build_info,
last_build_info=last_build_info_for_this_file,
allow_dirty=allow_dirty, allow_dirty=allow_dirty,
overwrite_existing_files=overwrite_existing_files, overwrite_existing_files=overwrite_existing_files,
build_all=build_all build_all=build_all