2021-10-18 15:55:52 +02:00
|
|
|
from pathlib import Path
|
|
|
|
import git
|
|
|
|
|
|
|
|
import PyTeX
|
|
|
|
|
2021-10-18 16:27:20 +02:00
|
|
|
from build_scripts.git_hook import get_latest_commit, is_recent
|
2021-10-18 15:55:52 +02:00
|
|
|
|
|
|
|
from .build_information import build_information
|
|
|
|
|
|
|
|
|
2021-10-18 16:27:20 +02:00
|
|
|
def build(src_dir: Path, build_dir: Path, check_existence: bool = True):
|
2021-10-18 15:55:52 +02:00
|
|
|
print('[PyTeX] Getting git repository information...')
|
|
|
|
extra_header, repo_description = build_information()
|
|
|
|
print('[PyTeX] Building version {version} of LatexPackages'.format(version=repo_description))
|
|
|
|
print('[PyTeX] Latest commit message: ' + get_latest_commit(git.Repo()).message.strip())
|
|
|
|
if git.Repo().is_dirty(untracked_files=True):
|
|
|
|
extra_header += ['WARNING: Local changes to git repository detected.',
|
|
|
|
' The build will not be reproducible (!)']
|
|
|
|
num_packages = 0
|
2021-10-18 16:27:20 +02:00
|
|
|
num_skipped_packages = 0
|
2021-10-18 15:55:52 +02:00
|
|
|
num_classes = 0
|
2021-10-18 16:27:20 +02:00
|
|
|
num_skipped_classes = 0
|
2021-10-18 15:55:52 +02:00
|
|
|
|
|
|
|
for file in src_dir.rglob('*.pysty'):
|
2021-10-18 16:27:20 +02:00
|
|
|
output_dir = build_dir / str(file.parent.relative_to(src_dir))
|
|
|
|
if not is_recent(file, git.Repo()):
|
|
|
|
if not check_existence:
|
|
|
|
print('[PyTex] Skipping file {file} since it was not modified since last build'.format(file=file.name))
|
|
|
|
num_skipped_packages += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if (output_dir / ('mkessler-' + str(file.with_suffix('.sty').name))).exists():
|
|
|
|
print('[PyTex] Skipping file {file} since it was not modified since '
|
|
|
|
'last build and already exists'.format(file=file.name))
|
|
|
|
num_skipped_packages += 1
|
|
|
|
continue
|
|
|
|
|
2021-10-18 15:55:52 +02:00
|
|
|
num_packages += 1
|
|
|
|
formatter = PyTeX.PackageFormatter(
|
|
|
|
package_name=file.with_suffix('').name,
|
|
|
|
extra_header=extra_header)
|
|
|
|
print('[PyTeX] Writing file {}'.format(formatter.file_name))
|
|
|
|
formatter.make_default_macros()
|
2021-10-18 16:27:20 +02:00
|
|
|
formatter.format_file(file, output_dir)
|
2021-10-18 15:55:52 +02:00
|
|
|
for file in src_dir.rglob('*.pycls'):
|
2021-10-18 16:27:20 +02:00
|
|
|
output_dir = build_dir / str(file.parent.relative_to(src_dir))
|
|
|
|
if not is_recent(file, git.Repo()):
|
|
|
|
if not check_existence:
|
|
|
|
print('[PyTex] Skipping file {file} since it was not modified since last build'.format(file=file.name))
|
|
|
|
num_skipped_classes += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if (output_dir / ('mkessler-' + str(file.with_suffix('.cls').name))).exists():
|
|
|
|
print('[PyTex] Skipping file {file} since it was not modified since '
|
|
|
|
'last build and already exists'.format(file=file.name))
|
|
|
|
num_skipped_classes += 1
|
|
|
|
continue
|
|
|
|
output_dir = build_dir / str(file.parent.relative_to(src_dir))
|
2021-10-18 15:55:52 +02:00
|
|
|
num_classes += 1
|
|
|
|
formatter = PyTeX.ClassFormatter(
|
|
|
|
class_name=file.with_suffix('').name,
|
|
|
|
extra_header=extra_header)
|
|
|
|
print('[PyTeX] Writing class file {}'.format(formatter.file_name))
|
|
|
|
formatter.make_default_macros()
|
2021-10-18 16:27:20 +02:00
|
|
|
formatter.format_file(input_path=file, output_dir=output_dir)
|
|
|
|
print(f'[PyTeX] Successfully built {num_packages} packages (skipped {num_skipped_packages}) '
|
|
|
|
f'and {num_classes} classes (skipped {num_skipped_classes}) in {build_dir}/')
|