diff --git a/PyTeX b/PyTeX index e213c70..dbdd99c 160000 --- a/PyTeX +++ b/PyTeX @@ -1 +1 @@ -Subproject commit e213c706eaaad4b3728c52b5a47d9f6d602ddb0e +Subproject commit dbdd99cc3d6e02b0cfadb1d337f6b85c5f52a9b5 diff --git a/build.py b/build.py index 44321fe..204c941 100644 --- a/build.py +++ b/build.py @@ -1,18 +1,53 @@ from pathlib import * import sys +import git +from datetime import * sys.path.insert(0, 'PyTeX/') from package_formatter import PackageFormatter from replacements import make_default_commands +from git_version import git_describe + +BUILD_DETAILS = [ + "Build details:", + " Build time: {build_time}", + " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", + " LatexPackages version: {packages_version} (commit {packages_commit_hash})" +] + + +def build_details(): + repo = git.Repo() + repo_description = git_describe(repo.head.ref.commit) + pytex_repo = repo.submodule('PyTeX').module() + pytex_repo_description = git_describe(pytex_repo.head.ref.commit) + return list(map(lambda line: line.format( + build_time=datetime.now().strftime('%Y/%m/%d %H:%M'), + pytex_version=pytex_repo_description, + pytex_commit_hash=pytex_repo.head.ref.commit.hexsha[0:7], + packages_version=repo_description, + packages_commit_hash=repo.head.ref.commit.hexsha[0:7] + ),BUILD_DETAILS)) + def build(build_dir: str): input_root = Path('./packages').resolve() output = input_root / build_dir + print('[PyTeX] Getting git repository information...') + extra_header = build_details() + print('[PyTeX] ..done.') + if git.Repo().is_dirty(): + extra_header += ['WARNING: Local changes to git repository detected.', + ' The build will not be reproducible (!)'] + num_files = 0 for file in input_root.rglob('*.pysty'): - formatter = PackageFormatter(package_name=file.with_suffix('').name) + num_files+=1 + formatter = PackageFormatter(package_name=file.with_suffix('').name, extra_header=extra_header) print('[PyTeX] Writing file {}'.format(formatter.file_name)) make_default_commands(formatter) formatter.format_package(file, Path('./').resolve() / build_dir / str(file.parent.relative_to(input_root))) + print(f'[PyTeX] Successfully built {num_files} packages in {build_dir}/') if __name__ == "__main__": build('build') + diff --git a/git_version.py b/git_version.py new file mode 100644 index 0000000..11009de --- /dev/null +++ b/git_version.py @@ -0,0 +1,45 @@ +import git +from typing import Dict + + +def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict: + commit_history = {commit.hexsha: { + 'priority': priority, + 'depth': depth + }} + if len(commit.parents) == 0: + return commit_history + else: + commit_history.update(get_history(commit.parents[0], priority, depth+1)) + for parent in commit.parents[1:]: + commit_history.update(get_history(parent, priority+1, depth+1)) + return commit_history + + +def git_describe(commit: git.objects.commit.Commit): + commit_history = get_history(commit) + latest_tag = None + for tag in commit.repo.tags: + sha = tag.commit.hexsha + if sha in commit_history.keys(): + if latest_tag is None: + latest_tag = tag + elif commit_history[sha]['priority'] < commit_history[latest_tag.commit.hexsha]['priority']: + latest_tag = tag + elif commit_history[sha]['priority'] > commit_history[latest_tag.commit.hexsha]['priority']: + pass # move on + elif commit_history[sha]['depth'] < commit_history[latest_tag.commit.hexsha]['depth']: + latest_tag = tag + elif commit_history[sha]['depth'] > commit_history[latest_tag.commit.hexsha]['depth']: + pass # move on + elif tag.object.tagged_date > latest_tag.object.tagged_date: + latest_tag = tag + if latest_tag is None: + return "No tags found - cannot describe anything." + else: + msg = latest_tag.name + if commit_history[latest_tag.commit.hexsha]['depth'] != 0: + msg += "-{depth}".format(depth=commit_history[latest_tag.commit.hexsha]['depth']) + if commit.repo.is_dirty(untracked_files=True): + msg += '-*' + return msg