2021-10-22 14:45:32 +02:00
|
|
|
from pathlib import Path
|
2021-10-22 19:29:23 +02:00
|
|
|
from typing import Optional, List
|
2021-10-22 14:45:32 +02:00
|
|
|
|
|
|
|
from PyTeX.build.git_hook import is_recent, get_latest_commit
|
2022-01-24 01:05:43 +01:00
|
|
|
from PyTeX import PackageFormatter, ClassFormatter, DictionaryFormatter, DocstripFormatter
|
2022-01-12 21:35:47 +01:00
|
|
|
from PyTeX.errors import *
|
2021-10-22 14:45:32 +02:00
|
|
|
from .pytex_msg import pytex_msg
|
2022-01-13 20:04:18 +01:00
|
|
|
from PyTeX.utils import md5
|
2021-10-22 14:45:32 +02:00
|
|
|
|
|
|
|
from .build_information import BuildInfo
|
|
|
|
|
|
|
|
|
|
|
|
class TexFileToFormat:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
src_path: Path,
|
2022-01-13 15:33:16 +01:00
|
|
|
build_root: Path,
|
|
|
|
src_root: Path,
|
2021-10-22 14:45:32 +02:00
|
|
|
latex_name: str,
|
|
|
|
current_build_info: BuildInfo,
|
2022-01-12 22:30:10 +01:00
|
|
|
last_build_info: Optional[List[dict]],
|
2021-10-22 14:45:32 +02:00
|
|
|
allow_dirty: bool = False,
|
|
|
|
overwrite_existing_files: bool = False,
|
|
|
|
build_all: bool = False):
|
|
|
|
self.src_path = src_path
|
2022-01-13 15:33:16 +01:00
|
|
|
self.build_root = build_root
|
|
|
|
self.src_root = src_root
|
|
|
|
self.build_path = build_root / src_path.parent.relative_to(src_root)
|
2021-12-17 15:09:04 +01:00
|
|
|
self.latex_name = latex_name # Still an identifier on how to name the package when being formatted
|
2021-10-22 14:45:32 +02:00
|
|
|
self.current_build_info = current_build_info
|
2022-01-12 22:30:10 +01:00
|
|
|
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
|
2021-10-22 14:45:32 +02:00
|
|
|
self.allow_dirty = allow_dirty
|
|
|
|
self.overwrite_existing_files: overwrite_existing_files
|
|
|
|
self.build_all = build_all
|
2021-10-22 19:29:23 +02:00
|
|
|
self._header: Optional[List[str]] = None
|
|
|
|
self.__format_header()
|
2021-10-22 14:45:32 +02:00
|
|
|
|
|
|
|
self.dirty = not is_recent(self.src_path, self.current_build_info.package_repo, compare=None)
|
|
|
|
self.pytex_dirty: bool = self.current_build_info.pytex_repo.is_dirty(
|
|
|
|
working_tree=True,
|
|
|
|
untracked_files=True
|
|
|
|
)
|
|
|
|
if self.last_build_info:
|
|
|
|
self.recent: bool = is_recent(
|
|
|
|
file=self.src_path,
|
|
|
|
repo=self.current_build_info.package_repo,
|
|
|
|
compare=self.current_build_info.package_repo.commit(self.last_build_info['source commit hash'])
|
|
|
|
)
|
|
|
|
self.pytex_recent: bool = get_latest_commit(
|
|
|
|
self.current_build_info.pytex_repo
|
|
|
|
).hexsha == self.last_build_info['pytex commit hash']
|
|
|
|
else:
|
|
|
|
self.recent = False
|
|
|
|
self.pytex_recent = False
|
|
|
|
|
2022-01-12 22:30:10 +01:00
|
|
|
def format(self) -> List[dict]:
|
2021-10-22 14:45:32 +02:00
|
|
|
if self.dirty or self.pytex_dirty:
|
|
|
|
if not self.allow_dirty:
|
2022-01-12 21:35:47 +01:00
|
|
|
raise SubmoduleDirtyForbiddenError
|
2021-10-22 14:45:32 +02:00
|
|
|
# TODO: add this to the header...?
|
|
|
|
return self.__format() # Dirty files are always built, since we have no information about them
|
|
|
|
elif self.build_all:
|
|
|
|
return self.__format() # Build file since we build all of them
|
|
|
|
elif not self.pytex_recent or not self.recent:
|
|
|
|
return self.__format() # Build file since either pytex or package repo is not recent
|
|
|
|
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:
|
2022-01-13 21:02:47 +01:00
|
|
|
return self.last_build_info_all
|
2021-10-22 14:45:32 +02:00
|
|
|
|
2021-10-22 19:29:23 +02:00
|
|
|
def __format_header(self):
|
|
|
|
new_header = []
|
2021-12-17 15:26:28 +01:00
|
|
|
if self.current_build_info.header:
|
|
|
|
for line in self.current_build_info.header:
|
2022-01-11 21:37:42 +01:00
|
|
|
if '.pysty' in self.src_path.name:
|
|
|
|
latex_file_type = 'package'
|
|
|
|
elif '.pycls' in self.src_path.name:
|
|
|
|
latex_file_type = 'class'
|
|
|
|
elif '.pydict' in self.src_path.name:
|
|
|
|
latex_file_type = 'dictionary'
|
2022-01-24 01:05:43 +01:00
|
|
|
elif '.dtx' in self.src_path.name:
|
|
|
|
latex_file_type = 'ERROR'
|
2022-01-11 21:37:42 +01:00
|
|
|
else:
|
2022-01-12 21:35:47 +01:00
|
|
|
raise ProgrammingError
|
2021-12-17 15:26:28 +01:00
|
|
|
new_header.append(line.format(
|
|
|
|
source_file=self.src_path.name,
|
2022-01-11 21:37:42 +01:00
|
|
|
latex_file_type=latex_file_type
|
2021-12-17 15:26:28 +01:00
|
|
|
))
|
2021-10-22 19:29:23 +02:00
|
|
|
self._header = new_header
|
|
|
|
|
2022-01-12 22:30:10 +01:00
|
|
|
def __format(self) -> List[dict]:
|
2022-01-11 18:51:03 +01:00
|
|
|
if self.src_path.name.endswith('.pysty'):
|
2021-10-22 14:45:32 +02:00
|
|
|
formatter = PackageFormatter(
|
|
|
|
package_name=self.src_path.with_suffix('').name,
|
|
|
|
author=self.current_build_info.author,
|
2022-01-11 18:51:03 +01:00
|
|
|
extra_header=self._header,
|
|
|
|
tex_version='LaTeX2e',
|
2022-01-11 21:37:42 +01:00
|
|
|
version=self.current_build_info.packages_version,
|
2021-12-17 15:09:04 +01:00
|
|
|
latex_name=self.latex_name)
|
2022-01-11 18:51:03 +01:00
|
|
|
elif self.src_path.name.endswith('.pycls'):
|
2021-10-22 14:45:32 +02:00
|
|
|
formatter = ClassFormatter(
|
|
|
|
class_name=self.src_path.with_suffix('').name,
|
|
|
|
author=self.current_build_info.author,
|
2022-01-11 18:51:03 +01:00
|
|
|
extra_header=self._header,
|
|
|
|
tex_version='LaTeX2e',
|
2022-01-11 21:37:42 +01:00
|
|
|
version=self.current_build_info.packages_version,
|
2021-12-17 15:09:04 +01:00
|
|
|
latex_name=self.latex_name)
|
2022-01-11 18:51:03 +01:00
|
|
|
elif self.src_path.name.endswith('.pysty3'):
|
|
|
|
formatter = PackageFormatter(
|
|
|
|
package_name=self.src_path.with_suffix('').name,
|
|
|
|
author=self.current_build_info.author,
|
|
|
|
extra_header=self._header,
|
|
|
|
tex_version='LaTeX3',
|
2022-01-11 21:37:42 +01:00
|
|
|
version=self.current_build_info.packages_version,
|
|
|
|
latex_name=self.latex_name)
|
2022-01-11 18:51:03 +01:00
|
|
|
elif self.src_path.name.endswith('.pycls3'):
|
|
|
|
formatter = ClassFormatter(
|
|
|
|
class_name=self.src_path.with_suffix('').name,
|
|
|
|
author=self.current_build_info.author,
|
|
|
|
extra_header=self._header,
|
|
|
|
tex_version='LaTeX3',
|
2022-01-11 21:37:42 +01:00
|
|
|
version=self.current_build_info.packages_version,
|
|
|
|
latex_name=self.latex_name)
|
2022-01-11 18:51:03 +01:00
|
|
|
elif self.src_path.name.endswith('.pydict'):
|
2022-01-09 14:15:18 +01:00
|
|
|
formatter = DictionaryFormatter(
|
|
|
|
kind=self.src_path.with_suffix('').name,
|
|
|
|
author=self.current_build_info.author,
|
|
|
|
header=self._header
|
|
|
|
)
|
2022-01-24 01:05:43 +01:00
|
|
|
elif self.src_path.name.endswith('.dtx'):
|
|
|
|
formatter = DocstripFormatter(name=self.src_path.with_suffix('').name)
|
2021-10-22 14:45:32 +02:00
|
|
|
else:
|
2022-01-12 21:35:47 +01:00
|
|
|
raise ProgrammingError
|
2021-10-22 14:45:32 +02:00
|
|
|
formatter.make_default_macros()
|
2022-01-13 20:04:18 +01:00
|
|
|
written_files = formatter.format_file(
|
|
|
|
input_path=self.src_path,
|
|
|
|
output_dir=self.build_path,
|
|
|
|
relative_name=str(self.src_path.relative_to(self.src_root)),
|
|
|
|
last_build_info=self.last_build_info_all)
|
2022-01-12 22:30:10 +01:00
|
|
|
build_infos = []
|
|
|
|
for written_file in written_files:
|
|
|
|
info = {
|
2022-01-13 15:33:16 +01:00
|
|
|
'name': str(self.src_path.parent.relative_to(self.src_root)) + "/" + written_file,
|
2022-01-13 19:24:07 +01:00
|
|
|
'source file': str(self.src_path.relative_to(self.src_root)),
|
2022-01-12 22:30:10 +01:00
|
|
|
'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,
|
2022-01-13 19:24:07 +01:00
|
|
|
'md5sum': md5(self.build_root / self.src_path.parent.relative_to(self.src_root) / written_file),
|
2022-01-12 22:30:10 +01:00
|
|
|
'dirty': self.dirty
|
|
|
|
}
|
|
|
|
build_infos.append(info)
|
|
|
|
pytex_msg('Written file {}'.format(written_file))
|
|
|
|
return build_infos
|