From 4a9f1f3b254cddafb07aaa984f4a2b3362892d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 18 Oct 2021 15:55:52 +0200 Subject: [PATCH] refactor building files into subfolder --- PyTeX | 2 +- build.py | 65 ++----------------- build_scripts/__init__.py | 0 build_scripts/build/__init__.py | 5 ++ build_scripts/build/build.py | 38 +++++++++++ build_scripts/build/build_information.py | 20 ++++++ build_scripts/build/config.py | 6 ++ build_scripts/git_hook/__init__.py | 7 ++ .../git_hook/git_version.py | 13 ---- build_scripts/travis/__init__.py | 5 ++ build_scripts/travis/deploy_message.py | 13 ++++ 11 files changed, 101 insertions(+), 73 deletions(-) create mode 100644 build_scripts/__init__.py create mode 100644 build_scripts/build/__init__.py create mode 100644 build_scripts/build/build.py create mode 100644 build_scripts/build/build_information.py create mode 100644 build_scripts/build/config.py create mode 100644 build_scripts/git_hook/__init__.py rename git_version.py => build_scripts/git_hook/git_version.py (82%) create mode 100644 build_scripts/travis/__init__.py create mode 100644 build_scripts/travis/deploy_message.py diff --git a/PyTeX b/PyTeX index a1bb182..05f53a9 160000 --- a/PyTeX +++ b/PyTeX @@ -1 +1 @@ -Subproject commit a1bb182eec81012fac4897c048c628edbff6b4a3 +Subproject commit 05f53a917113648a81ab96f353800d64dd9e6ec9 diff --git a/build.py b/build.py index fe70496..538ead2 100644 --- a/build.py +++ b/build.py @@ -1,61 +1,8 @@ -from pathlib import * -from datetime import * -import git -import PyTeX -from git_version import git_describe, get_latest_commit - -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(get_latest_commit(repo)) - pytex_repo = repo.submodule('PyTeX').module() - pytex_repo_description = git_describe(get_latest_commit(pytex_repo)) - 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=get_latest_commit(pytex_repo).hexsha[0:7], - packages_version=repo_description, - packages_commit_hash=get_latest_commit(repo).hexsha[0:7] - ), BUILD_DETAILS)), repo_description - - -def build(build_dir: str): - input_root = Path('./src').resolve() - output = input_root / build_dir - print('[PyTeX] Getting git repository information...') - extra_header, repo_description = build_details() - 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 - num_classes = 0 - for file in input_root.rglob('*.pysty'): - 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() - formatter.format_file(file, Path('./').resolve() / build_dir / str(file.parent.relative_to(input_root))) - for file in input_root.rglob('*.pycls'): - 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() - formatter.format_file(file, Path('./').resolve() / build_dir / str(file.parent.relative_to(input_root))) - print(f'[PyTeX] Successfully built {num_packages} packages and {num_classes} classes in {build_dir}/') - +from build_scripts.build import build +from pathlib import Path if __name__ == "__main__": - build('build') + build( + src_dir=Path('./src').resolve(), + build_dir=Path('./build').resolve() + ) diff --git a/build_scripts/__init__.py b/build_scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/build_scripts/build/__init__.py b/build_scripts/build/__init__.py new file mode 100644 index 0000000..3b3e07b --- /dev/null +++ b/build_scripts/build/__init__.py @@ -0,0 +1,5 @@ +from .build import build + +__all__ = [ + 'build' +] diff --git a/build_scripts/build/build.py b/build_scripts/build/build.py new file mode 100644 index 0000000..cb4ea8d --- /dev/null +++ b/build_scripts/build/build.py @@ -0,0 +1,38 @@ +from pathlib import Path +import git + +import PyTeX + +from build_scripts.git_hook.git_version import get_latest_commit + +from .build_information import build_information + + +def build(src_dir: Path, build_dir: Path): + 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 + num_classes = 0 + + for file in src_dir.rglob('*.pysty'): + 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() + formatter.format_file(file, build_dir / str(file.parent.relative_to(src_dir))) + for file in src_dir.rglob('*.pycls'): + 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() + formatter.format_file(file, build_dir / str(file.parent.relative_to(src_dir))) + print(f'[PyTeX] Successfully built {num_packages} packages and {num_classes} classes in {build_dir}/') diff --git a/build_scripts/build/build_information.py b/build_scripts/build/build_information.py new file mode 100644 index 0000000..217c3d9 --- /dev/null +++ b/build_scripts/build/build_information.py @@ -0,0 +1,20 @@ +import git +import datetime + +from build_scripts.git_hook import git_describe, get_latest_commit + +from .config import BUILD_DETAILS + + +def build_information(): + repo = git.Repo() + repo_description = git_describe(get_latest_commit(repo)) + pytex_repo = repo.submodule('PyTeX').module() + pytex_repo_description = git_describe(get_latest_commit(pytex_repo)) + return list(map(lambda line: line.format( + build_time=datetime.datetime.now().strftime('%Y/%m/%d %H:%M'), + pytex_version=pytex_repo_description, + pytex_commit_hash=get_latest_commit(pytex_repo).hexsha[0:7], + packages_version=repo_description, + packages_commit_hash=get_latest_commit(repo).hexsha[0:7] + ), BUILD_DETAILS)), repo_description diff --git a/build_scripts/build/config.py b/build_scripts/build/config.py new file mode 100644 index 0000000..c69f02c --- /dev/null +++ b/build_scripts/build/config.py @@ -0,0 +1,6 @@ +BUILD_DETAILS = [ + "Build details:", + " Build time: {build_time}", + " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", + " LatexPackages version: {packages_version} (commit {packages_commit_hash})" +] diff --git a/build_scripts/git_hook/__init__.py b/build_scripts/git_hook/__init__.py new file mode 100644 index 0000000..422c72e --- /dev/null +++ b/build_scripts/git_hook/__init__.py @@ -0,0 +1,7 @@ +from .git_version import git_describe, get_history, get_latest_commit + +__all__ = [ + 'git_describe', + 'get_history', + 'get_latest_commit' +] diff --git a/git_version.py b/build_scripts/git_hook/git_version.py similarity index 82% rename from git_version.py rename to build_scripts/git_hook/git_version.py index 08965b9..ad9f179 100644 --- a/git_version.py +++ b/build_scripts/git_hook/git_version.py @@ -1,6 +1,5 @@ import git from typing import Dict -import os def get_latest_commit(repo): @@ -10,18 +9,6 @@ def get_latest_commit(repo): return repo.head.ref.commit -def get_deploy_message(): - repo = git.Repo() - old_msg = get_latest_commit(repo).message - return "{old_msg}\n" \ - "\n" \ - "Build branch {branch} ({hexsha}) from {repo_name}" \ - .format(old_msg=old_msg, - branch=os.environ['TRAVIS_BRANCH'], - hexsha=get_latest_commit(repo).hexsha[0:7], - repo_name='kesslermaximilian/LatexPackages') - - def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict: commit_history = {commit.hexsha: { 'priority': priority, diff --git a/build_scripts/travis/__init__.py b/build_scripts/travis/__init__.py new file mode 100644 index 0000000..a756aa3 --- /dev/null +++ b/build_scripts/travis/__init__.py @@ -0,0 +1,5 @@ +from .deploy_message import get_deploy_message + +__all__ = [ + 'get_deploy_message' +] diff --git a/build_scripts/travis/deploy_message.py b/build_scripts/travis/deploy_message.py new file mode 100644 index 0000000..19f0327 --- /dev/null +++ b/build_scripts/travis/deploy_message.py @@ -0,0 +1,13 @@ +import os +from build_scripts.git_hook import get_latest_commit + + +def get_deploy_message(repo): + old_msg = get_latest_commit(repo).message + return "{old_msg}\n" \ + "\n" \ + "Build branch {branch} ({hexsha}) from {repo_name}" \ + .format(old_msg=old_msg, + branch=os.environ['TRAVIS_BRANCH'], + hexsha=get_latest_commit(repo).hexsha[0:7], + repo_name='kesslermaximilian/LatexPackages')