From 05f53a917113648a81ab96f353800d64dd9e6ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 18 Oct 2021 15:55:26 +0200 Subject: [PATCH 01/14] fix extension mistake in package formatter class --- default_formatters/package_formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default_formatters/package_formatter.py b/default_formatters/package_formatter.py index 3da18e7..873ef40 100644 --- a/default_formatters/package_formatter.py +++ b/default_formatters/package_formatter.py @@ -5,7 +5,7 @@ import PyTeX.macros class PackageFormatter(PyTeX.formatter.TexFormatter): def __init__(self, package_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []): - PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.cls') + PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty') def make_default_macros(self): PyTeX.macros.make_default_macros(self, 'package') From 90acc2baf7920d3c0f385ece3ab060d2a5060aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 10:01:33 +0200 Subject: [PATCH 02/14] add build scripts --- build/__init__.py | 0 build/build/__init__.py | 5 ++ build/build/build.py | 116 +++++++++++++++++++++++++++++++ build/build/build_information.py | 76 ++++++++++++++++++++ build/build/config.py | 10 +++ build/git_hook/__init__.py | 9 +++ build/git_hook/git_version.py | 53 ++++++++++++++ build/git_hook/recent.py | 30 ++++++++ build/utils/__init__.py | 0 9 files changed, 299 insertions(+) create mode 100644 build/__init__.py create mode 100644 build/build/__init__.py create mode 100644 build/build/build.py create mode 100644 build/build/build_information.py create mode 100644 build/build/config.py create mode 100644 build/git_hook/__init__.py create mode 100644 build/git_hook/git_version.py create mode 100644 build/git_hook/recent.py create mode 100644 build/utils/__init__.py diff --git a/build/__init__.py b/build/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/build/build/__init__.py b/build/build/__init__.py new file mode 100644 index 0000000..3b3e07b --- /dev/null +++ b/build/build/__init__.py @@ -0,0 +1,5 @@ +from .build import build + +__all__ = [ + 'build' +] diff --git a/build/build/build.py b/build/build/build.py new file mode 100644 index 0000000..03b23a6 --- /dev/null +++ b/build/build/build.py @@ -0,0 +1,116 @@ +from pathlib import Path +from typing import Optional + +import PyTeX + +from .build_information import BuildInfo + + +def pytex_msg(msg: str): + print('[PyTeX] ' + msg) + + +class TexFileToFormat: + def __init__(self, src_path: Path, build_dir: Path): + self.src_path = src_path + self.build_path = build_dir + + def format(self): + if '.pysty' in self.src_path.name: + formatter = PyTeX.PackageFormatter( + package_name=self.src_path.with_suffix('').name, + extra_header=[]) # TODO: extra header + else: + formatter = PyTeX.ClassFormatter( + class_name=self.src_path.with_suffix('').name, + extra_header=[]) # TODO + pytex_msg('Writing file {}'.format(formatter.file_name)) + formatter.make_default_macros() + formatter.format_file(self.src_path, self.build_path) + + +def build( + src_dir: Optional[Path] = None, + build_dir: Optional[Path] = None, + input_file: Optional[Path] = None, + author: Optional[str] = None, + latex_name: str = 'prepend-author', # name handling + recursive: bool = False, # input control + include_timestamp: bool = False, # header + include_pytex_version: bool = False, # header + include_license: bool = False, # header + include_git_version: bool = False, # header + include_pytex_info_text: bool = False, # header + use_git: bool = False, # versioning + allow_dirty: bool = False, # versioning + overwrite_existing_files: bool = False, # output control + build_all: bool = False, # output control / versioning + write_build_information: bool = True, # meta + ): + pytex_msg('Getting git repository information...') + current_build_info = BuildInfo( + include_timestamp=include_timestamp, + include_pytex_version=include_pytex_version, + include_license=include_license, + include_git_version=include_git_version, + include_pytex_info_text=include_pytex_info_text, + author=author, + pytex_repo=None, # TODO + packages_repo=None # TODO + ) + old_build_info = {} # TODO: read this in from file + # extra_header += ['WARNING: Local changes to git repository detected.', + # ' The build will not be reproducible (!)'] + + files = [] + if input_file: + files.append(input_file) + if src_dir: + if recursive: + for file in src_dir.rglob('*.pysty'): + files.append(file) + for file in src_dir.rglob('*.pycls'): + files.append(file) + else: + for file in src_dir.glob('*.pysty'): + files.append(file) + for file in src_dir.glob('*.pycls'): + files.append(file) + + input_dir = src_dir if src_dir else input_file.parent + output_dir = build_dir if build_dir else input_file.parent + sources_to_build = [] + for file in files: + sources_to_build.append( + TexFileToFormat( + src_path=file, + build_dir=output_dir / file.parent.relative_to(input_dir) + )) + + for source in sources_to_build: + source.format() + + current_build_info = { + 'build_time': '', + 'packages': { + 'built': '', + 'skipped': '' + }, + 'classes': { + 'built': '', + 'skipped': '' + }, + 'LatexPackages': { + 'version': '', + 'branch': '', + 'commit': '', + 'dirty': '' + }, + 'PyTeX': { + 'version': '', + 'branch': '', + 'commit': '', + 'dirty': '' + } + } + pytex_msg('Build done') diff --git a/build/build/build_information.py b/build/build/build_information.py new file mode 100644 index 0000000..e0a3bc3 --- /dev/null +++ b/build/build/build_information.py @@ -0,0 +1,76 @@ +import git +import datetime +from typing import Optional + +from PyTeX.build.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 + + +class BuildInfo: + def __init__( + self, + include_timestamp: bool = False, + include_pytex_version: bool = False, + include_license: bool = False, + include_git_version: bool = False, + include_pytex_info_text: bool = False, + author: Optional[str] = None, + pytex_repo: Optional[git.Repo] = None, + packages_repo: Optional[git.Repo] = None): + self.author = author + + self._pytex_repo = pytex_repo + self._packages_repo = packages_repo + self._pytex_repo_commit = None + self._packages_repo_commit = None + self._pytex_repo_version = None + self._packages_repo_version = None + + self._header = None + + self.get_repo_commits() + self.get_repo_version() + + self.create_header( + include_timestamp=include_timestamp, + include_pytex_version=include_pytex_version, + include_license=include_license, + include_git_version=include_git_version, + include_pytex_info_text=include_pytex_info_text + ) + + def get_repo_commits(self): + if self._packages_repo: + self._packages_repo_commit = get_latest_commit(self._packages_repo) + if self._pytex_repo: + self._pytex_repo_commit = get_latest_commit(self._pytex_repo) + + def get_repo_version(self): + if self._packages_repo_commit: + self._packages_repo_version = git_describe(self._packages_repo_commit) + if self._pytex_repo_commit: + self._pytex_repo_version = git_describe(self._pytex_repo_commit) + + def create_header( + self, + include_timestamp: bool = False, + include_pytex_version: bool = False, + include_license: bool = False, + include_git_version: bool = False, + include_pytex_info_text: bool = False): + pass diff --git a/build/build/config.py b/build/build/config.py new file mode 100644 index 0000000..a6d8ad0 --- /dev/null +++ b/build/build/config.py @@ -0,0 +1,10 @@ +BUILD_DETAILS = [ + "Build details:", + " Build time: {build_time}", + " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", + " LatexPackages version: {packages_version} (commit {packages_commit_hash})" +] + +FILENAME_TYPE_PREPEND_AUTHOR = 'prepend-author' +FILENAME_TYPE_RAW_NAME = 'raw' +DATE_FORMAT = '%Y/%m/%d %H:%M' diff --git a/build/git_hook/__init__.py b/build/git_hook/__init__.py new file mode 100644 index 0000000..851f2bc --- /dev/null +++ b/build/git_hook/__init__.py @@ -0,0 +1,9 @@ +from .git_version import git_describe, get_history, get_latest_commit +from .recent import is_recent + +__all__ = [ + 'git_describe', + 'get_history', + 'get_latest_commit', + 'is_recent' +] diff --git a/build/git_hook/git_version.py b/build/git_hook/git_version.py new file mode 100644 index 0000000..ad9f179 --- /dev/null +++ b/build/git_hook/git_version.py @@ -0,0 +1,53 @@ +import git +from typing import Dict + + +def get_latest_commit(repo): + if repo.head.is_detached: + return repo.head.commit + else: + return repo.head.ref.commit + + +def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict: + commit_history = {commit.hexsha: { + 'priority': priority, + 'depth': depth + }} + try: + if len(commit.parents) > 0: + 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)) + except ValueError: + pass + 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 diff --git a/build/git_hook/recent.py b/build/git_hook/recent.py new file mode 100644 index 0000000..2c86345 --- /dev/null +++ b/build/git_hook/recent.py @@ -0,0 +1,30 @@ +import git + +from .git_version import get_latest_commit +from typing import Union, Optional, List + + +def is_recent(file, repo, compare: Optional[Union[git.Commit, List[git.Commit]]] = None) -> Optional[bool]: + modified_files = [item.a_path for item in repo.index.diff(None)] + if file in modified_files: + return True + + newly_committed_files = [] + if type(compare) == git.Commit: + newly_committed_files = [item.a_path for item in repo.index.diff(compare)] + elif type(compare) == list: + for commit in compare: + for item in repo.index.diff(commit): + newly_committed_files.append(item.a_path) + elif type is None: + for parent in get_latest_commit(repo).parents: + for item in repo.index.diff(parent): + newly_committed_files.append(item.a_path) + else: + print("Invalid argument type for compare") + return None + + if file in newly_committed_files: + return True + else: + return False diff --git a/build/utils/__init__.py b/build/utils/__init__.py new file mode 100644 index 0000000..e69de29 From b44af744b0e529ea5e933ae035f8ab463be248b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 10:12:28 +0200 Subject: [PATCH 03/14] retrieve repositories --- build/build/build.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 03b23a6..22b1e06 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -1,6 +1,8 @@ from pathlib import Path from typing import Optional +import git + import PyTeX from .build_information import BuildInfo @@ -55,8 +57,8 @@ def build( include_git_version=include_git_version, include_pytex_info_text=include_pytex_info_text, author=author, - pytex_repo=None, # TODO - packages_repo=None # TODO + pytex_repo=git.Repo(__file__, search_parent_directories=True), # TODO + packages_repo=git.Repo(src_dir, search_parent_directories=True) # TODO ) old_build_info = {} # TODO: read this in from file # extra_header += ['WARNING: Local changes to git repository detected.', From cf3edf33c4ddd87faafe32a9f2cfc28ce65d0482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 10:57:16 +0200 Subject: [PATCH 04/14] write and read in build information in json format --- build/build/build.py | 70 +++++++++++++++++++------------- build/build/build_information.py | 24 ++++++++++- 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 22b1e06..35f05cb 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -1,3 +1,4 @@ +import json from pathlib import Path from typing import Optional @@ -13,22 +14,35 @@ def pytex_msg(msg: str): class TexFileToFormat: - def __init__(self, src_path: Path, build_dir: Path): + def __init__(self, src_path: Path, build_dir: Path, latex_name: str, build_info: BuildInfo): self.src_path = src_path self.build_path = build_dir + self.tex_name = latex_name # Still an identifier on how to name the package when being formatted + self.build_info = build_info def format(self): if '.pysty' in self.src_path.name: formatter = PyTeX.PackageFormatter( package_name=self.src_path.with_suffix('').name, - extra_header=[]) # TODO: extra header + extra_header=self.build_info.header) else: formatter = PyTeX.ClassFormatter( class_name=self.src_path.with_suffix('').name, - extra_header=[]) # TODO + extra_header=self.build_info.header) pytex_msg('Writing file {}'.format(formatter.file_name)) formatter.make_default_macros() formatter.format_file(self.src_path, self.build_path) + info = { + 'name': formatter.file_name, + 'source file': self.src_path.name, + 'build time': self.build_info.build_time, + 'source version': self.build_info.packages_version, + 'source commit hash': self.build_info.packages_hash, + 'pytex version': self.build_info.pytex_version, + 'pytex commit hash': self.build_info.pytex_hash, + 'dirty': False + } + return info def build( @@ -60,7 +74,11 @@ def build( pytex_repo=git.Repo(__file__, search_parent_directories=True), # TODO packages_repo=git.Repo(src_dir, search_parent_directories=True) # TODO ) - old_build_info = {} # TODO: read this in from file + input_dir = src_dir if src_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: + old_build_info = json.load(f) # extra_header += ['WARNING: Local changes to git repository detected.', # ' The build will not be reproducible (!)'] @@ -79,40 +97,36 @@ def build( for file in src_dir.glob('*.pycls'): files.append(file) - input_dir = src_dir if src_dir else input_file.parent - output_dir = build_dir if build_dir else input_file.parent sources_to_build = [] for file in files: sources_to_build.append( TexFileToFormat( 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, + build_info=current_build_info )) - for source in sources_to_build: - source.format() - - current_build_info = { + info_dict = { 'build_time': '', - 'packages': { - 'built': '', - 'skipped': '' - }, - 'classes': { - 'built': '', - 'skipped': '' - }, 'LatexPackages': { - 'version': '', - 'branch': '', - 'commit': '', - 'dirty': '' + 'version': current_build_info.packages_version, + 'commit': current_build_info.packages_hash, + 'dirty': False # TODO }, 'PyTeX': { - 'version': '', - 'branch': '', - 'commit': '', - 'dirty': '' - } + 'version': current_build_info.pytex_version, + 'commit': current_build_info.pytex_hash, + 'dirty': False # TODO + }, + 'tex_sources': [ + + ] } + + for source in sources_to_build: + info = source.format() + info_dict['tex_sources'].append(info) + with open(output_dir / 'build_info.json', 'w') as f: + json.dump(info_dict, f, indent=4) pytex_msg('Build done') diff --git a/build/build/build_information.py b/build/build/build_information.py index e0a3bc3..2fd5384 100644 --- a/build/build/build_information.py +++ b/build/build/build_information.py @@ -32,7 +32,9 @@ class BuildInfo: author: Optional[str] = None, pytex_repo: Optional[git.Repo] = None, packages_repo: Optional[git.Repo] = None): + self.author = author + self.build_time = datetime.datetime.now().strftime('%Y/%m/%d %H:%M') self._pytex_repo = pytex_repo self._packages_repo = packages_repo @@ -54,6 +56,26 @@ class BuildInfo: include_pytex_info_text=include_pytex_info_text ) + @property + def header(self): + return self._header + + @property + def pytex_version(self): + return self._pytex_repo_version + + @property + def packages_version(self): + return self._packages_repo_version + + @property + def pytex_hash(self): + return self._pytex_repo_commit.hexsha + + @property + def packages_hash(self): + return self._packages_repo_commit.hexsha + def get_repo_commits(self): if self._packages_repo: self._packages_repo_commit = get_latest_commit(self._packages_repo) @@ -73,4 +95,4 @@ class BuildInfo: include_license: bool = False, include_git_version: bool = False, include_pytex_info_text: bool = False): - pass + self._header = [] From 7b277ad8b66a94811ddedad71a0ad04ff588b7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 11:58:56 +0200 Subject: [PATCH 05/14] integrate some more options. forward arguments to file formatter --- build/build/build.py | 38 +++++++++++++++++++++++--------- build/build/build_information.py | 8 +++++++ build/git_hook/recent.py | 9 ++------ 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 35f05cb..3754fc0 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -14,21 +14,34 @@ def pytex_msg(msg: str): class TexFileToFormat: - def __init__(self, src_path: Path, build_dir: Path, latex_name: str, build_info: BuildInfo): + def __init__( + self, + src_path: Path, + build_dir: Path, + latex_name: str, + build_info: BuildInfo, + allow_dirty: bool = False, + overwrite_existing_files: bool = False, + build_all: bool = False): self.src_path = src_path self.build_path = build_dir self.tex_name = latex_name # Still an identifier on how to name the package when being formatted self.build_info = build_info + self.allow_dirty = allow_dirty + self.overwrite_existing_files: overwrite_existing_files + self.build_all = build_all def format(self): if '.pysty' in self.src_path.name: formatter = PyTeX.PackageFormatter( package_name=self.src_path.with_suffix('').name, extra_header=self.build_info.header) - else: + elif '.pycls' in self.src_path.name: formatter = PyTeX.ClassFormatter( class_name=self.src_path.with_suffix('').name, extra_header=self.build_info.header) + else: + exit(1) pytex_msg('Writing file {}'.format(formatter.file_name)) formatter.make_default_macros() formatter.format_file(self.src_path, self.build_path) @@ -57,7 +70,7 @@ def build( include_license: bool = False, # header include_git_version: bool = False, # header include_pytex_info_text: bool = False, # header - use_git: bool = False, # versioning + use_git: bool = False, # versioning (not implemented yet) allow_dirty: bool = False, # versioning overwrite_existing_files: bool = False, # output control build_all: bool = False, # output control / versioning @@ -104,20 +117,23 @@ def build( src_path=file, build_dir=output_dir / file.parent.relative_to(input_dir), latex_name=latex_name, - build_info=current_build_info + build_info=current_build_info, + allow_dirty=allow_dirty, + overwrite_existing_files=overwrite_existing_files, + build_all=build_all )) info_dict = { 'build_time': '', - 'LatexPackages': { + 'source files': { 'version': current_build_info.packages_version, 'commit': current_build_info.packages_hash, - 'dirty': False # TODO + 'dirty': current_build_info.package_repo.is_dirty(untracked_files=True) }, - 'PyTeX': { + 'pytex': { 'version': current_build_info.pytex_version, 'commit': current_build_info.pytex_hash, - 'dirty': False # TODO + 'dirty': current_build_info.pytex_repo.is_dirty(untracked_files=True) }, 'tex_sources': [ @@ -127,6 +143,8 @@ def build( for source in sources_to_build: info = source.format() info_dict['tex_sources'].append(info) - with open(output_dir / 'build_info.json', 'w') as f: - json.dump(info_dict, f, indent=4) + + if write_build_information: + with open(output_dir / 'build_info.json', 'w') as f: + json.dump(info_dict, f, indent=4) pytex_msg('Build done') diff --git a/build/build/build_information.py b/build/build/build_information.py index 2fd5384..71b4d6d 100644 --- a/build/build/build_information.py +++ b/build/build/build_information.py @@ -76,6 +76,14 @@ class BuildInfo: def packages_hash(self): return self._packages_repo_commit.hexsha + @property + def package_repo(self): + return self._packages_repo + + @property + def pytex_repo(self): + return self._pytex_repo + def get_repo_commits(self): if self._packages_repo: self._packages_repo_commit = get_latest_commit(self._packages_repo) diff --git a/build/git_hook/recent.py b/build/git_hook/recent.py index 2c86345..c3e28eb 100644 --- a/build/git_hook/recent.py +++ b/build/git_hook/recent.py @@ -5,21 +5,16 @@ from typing import Union, Optional, List def is_recent(file, repo, compare: Optional[Union[git.Commit, List[git.Commit]]] = None) -> Optional[bool]: - modified_files = [item.a_path for item in repo.index.diff(None)] - if file in modified_files: - return True newly_committed_files = [] if type(compare) == git.Commit: newly_committed_files = [item.a_path for item in repo.index.diff(compare)] + elif type is None: + newly_committed_files = [item.a_path for item in repo.index.diff(None)] elif type(compare) == list: for commit in compare: for item in repo.index.diff(commit): newly_committed_files.append(item.a_path) - elif type is None: - for parent in get_latest_commit(repo).parents: - for item in repo.index.diff(parent): - newly_committed_files.append(item.a_path) else: print("Invalid argument type for compare") return None From c24eb9cd25063695efef4e67af6a851a5844bd21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 12:55:15 +0200 Subject: [PATCH 06/14] fix bugs: correctly diff against working tree or other commits now. add documentation for recent method --- build/git_hook/recent.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/build/git_hook/recent.py b/build/git_hook/recent.py index c3e28eb..6e161db 100644 --- a/build/git_hook/recent.py +++ b/build/git_hook/recent.py @@ -2,24 +2,41 @@ import git from .git_version import get_latest_commit from typing import Union, Optional, List +from pathlib import Path -def is_recent(file, repo, compare: Optional[Union[git.Commit, List[git.Commit]]] = None) -> Optional[bool]: +def is_recent(file: Path, repo: git.Repo, compare: Optional[Union[git.Commit, List[git.Commit]]] = None) -> Optional[bool]: + """ + :param file: file to check + :param repo: repo that the file belongs to + :param compare: commit or list of commits to compare to. None stands for 'working tree' + :return: Whether the given file is currently the same as in compared commit + If compare is a commit, checks if the file has changed since given commit, compared to the most recent commit + of the repository + For a list of commits, checks the same, but for all of these commits. In particular, only returns true if the file + is the same in all of the specified commits (and in the most recent of the repository) + + If compare is None, compares the file against the corking tree, i.e. if the file has been modified since the last + commit on the repo. This also involves staged files, i.e. modified and staged files will be considered as + 'not recent' since changes are not committed + """ newly_committed_files = [] if type(compare) == git.Commit: - newly_committed_files = [item.a_path for item in repo.index.diff(compare)] - elif type is None: - newly_committed_files = [item.a_path for item in repo.index.diff(None)] + newly_committed_files = [item.a_path for item in get_latest_commit(repo).diff(compare)] + elif compare is None: + com = get_latest_commit(repo) + newly_committed_files = [item.a_path for item in com.diff(None)] + pass elif type(compare) == list: for commit in compare: - for item in repo.index.diff(commit): + for item in get_latest_commit(repo).diff(commit): newly_committed_files.append(item.a_path) else: print("Invalid argument type for compare") return None - if file in newly_committed_files: + if str(file.relative_to(repo.working_dir)) in newly_committed_files: return True else: return False From 408a154548c7c222c6beb606f7eff3bcd239f100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 12:56:00 +0200 Subject: [PATCH 07/14] pass more attributes to TexFileToFormat class, write dirty / recent information into build file --- build/build/build.py | 54 +++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 3754fc0..2a7d7c0 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -8,6 +8,10 @@ import PyTeX 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): print('[PyTeX] ' + msg) @@ -19,27 +23,37 @@ class TexFileToFormat: src_path: Path, build_dir: Path, latex_name: str, - build_info: BuildInfo, + current_build_info: BuildInfo, + last_build_info: Optional[dict], allow_dirty: bool = False, overwrite_existing_files: bool = False, build_all: bool = False): self.src_path = src_path self.build_path = build_dir 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.overwrite_existing_files: overwrite_existing_files 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: formatter = PyTeX.PackageFormatter( 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: formatter = PyTeX.ClassFormatter( class_name=self.src_path.with_suffix('').name, - extra_header=self.build_info.header) + extra_header=self.current_build_info.header) else: exit(1) pytex_msg('Writing file {}'.format(formatter.file_name)) @@ -48,12 +62,12 @@ class TexFileToFormat: info = { 'name': formatter.file_name, 'source file': self.src_path.name, - 'build time': self.build_info.build_time, - 'source version': self.build_info.packages_version, - 'source commit hash': self.build_info.packages_hash, - 'pytex version': self.build_info.pytex_version, - 'pytex commit hash': self.build_info.pytex_hash, - 'dirty': False + '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, + 'dirty': self.dirty } return info @@ -90,10 +104,12 @@ def build( input_dir = src_dir if src_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: - old_build_info = json.load(f) - # extra_header += ['WARNING: Local changes to git repository detected.', - # ' The build will not be reproducible (!)'] + last_build_info_file = output_dir / BUILD_INFO_FILENAME + if last_build_info_file.exists(): + with open(output_dir / 'build_info.json', 'r') as f: + last_build_info = json.load(f) + else: + last_build_info = None files = [] if input_file: @@ -112,12 +128,18 @@ def build( sources_to_build = [] 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( TexFileToFormat( src_path=file, build_dir=output_dir / file.parent.relative_to(input_dir), 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, overwrite_existing_files=overwrite_existing_files, build_all=build_all From e6018a89c8e71d2f1dff8623e9d4dfbf50872a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 13:39:29 +0200 Subject: [PATCH 08/14] fix mistake with header type --- build/build/build_information.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build/build_information.py b/build/build/build_information.py index 71b4d6d..65b9012 100644 --- a/build/build/build_information.py +++ b/build/build/build_information.py @@ -103,4 +103,4 @@ class BuildInfo: include_license: bool = False, include_git_version: bool = False, include_pytex_info_text: bool = False): - self._header = [] + self._header = [] # TODO From 96f2608751b793a22608fca553b510c2a977880a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 13:39:56 +0200 Subject: [PATCH 09/14] check if PyTeX is dirty, implement incremental build --- build/build/build.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 2a7d7c0..01640f2 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -9,6 +9,7 @@ import PyTeX from .build_information import BuildInfo from PyTeX.build.git_hook.recent import is_recent +from PyTeX.build.git_hook.git_version import get_latest_commit BUILD_INFO_FILENAME = 'build_info.json' @@ -38,14 +39,40 @@ class TexFileToFormat: self.build_all = build_all 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 + 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 def format(self) -> dict: + if self.dirty or self.pytex_dirty: + if not self.allow_dirty: + raise Exception( + '{file} is dirty, but writing dirty files not allowed.'.format( + file=self.src_path.name if self.dirty else 'Submodule PyTeX') + ) + # 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 + else: + pass # Do not build + + def __format(self) -> dict: if '.pysty' in self.src_path.name: formatter = PyTeX.PackageFormatter( package_name=self.src_path.with_suffix('').name, @@ -98,8 +125,8 @@ def build( include_git_version=include_git_version, include_pytex_info_text=include_pytex_info_text, author=author, - pytex_repo=git.Repo(__file__, search_parent_directories=True), # TODO - packages_repo=git.Repo(src_dir, search_parent_directories=True) # TODO + pytex_repo=git.Repo(__file__, search_parent_directories=True), + packages_repo=git.Repo(src_dir, search_parent_directories=True) ) input_dir = src_dir if src_dir else input_file.parent output_dir = build_dir if build_dir else input_file.parent From f0c32260fa149978d690a27a6422b391241ac493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 13:51:31 +0200 Subject: [PATCH 10/14] fix some bugs --- build/build/build.py | 4 ++-- build/git_hook/recent.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build/build/build.py b/build/build/build.py index 01640f2..40ed33a 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -38,7 +38,7 @@ class TexFileToFormat: self.overwrite_existing_files: overwrite_existing_files self.build_all = build_all - self.dirty = is_recent(self.src_path, self.current_build_info.package_repo, compare=None) + 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 @@ -70,7 +70,7 @@ class TexFileToFormat: elif not self.pytex_recent or not self.recent: return self.__format() # Build file since either pytex or package repo is not recent else: - pass # Do not build + return self.last_build_info def __format(self) -> dict: if '.pysty' in self.src_path.name: diff --git a/build/git_hook/recent.py b/build/git_hook/recent.py index 6e161db..d824bdb 100644 --- a/build/git_hook/recent.py +++ b/build/git_hook/recent.py @@ -37,6 +37,6 @@ def is_recent(file: Path, repo: git.Repo, compare: Optional[Union[git.Commit, Li return None if str(file.relative_to(repo.working_dir)) in newly_committed_files: - return True - else: return False + else: + return True From de10ca75461de877297f31eb0b61ee5d9f16193d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 13:56:09 +0200 Subject: [PATCH 11/14] also build when last build of file has been dirty --- build/build/build.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/build/build.py b/build/build/build.py index 40ed33a..4544a56 100644 --- a/build/build/build.py +++ b/build/build/build.py @@ -69,6 +69,8 @@ class TexFileToFormat: 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: return self.last_build_info From 4c3867e0f4e1b2c3bc55688868beb3b7ccad5451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 14:45:32 +0200 Subject: [PATCH 12/14] refactor files into better module structure --- base/__init__.py | 2 - build/__init__.py | 5 ++ build/{build => }/build.py | 96 +-------------------- build/build/__init__.py | 5 -- build/build/config.py | 10 --- build/utils/__init__.py | 9 ++ build/{build => utils}/build_information.py | 16 ---- build/utils/pytex_file.py | 93 ++++++++++++++++++++ build/utils/pytex_msg.py | 2 + config/__init__.py | 13 +++ config/constants.py | 4 + base/config.py => config/header_parts.py | 9 +- default_formatters/class_formatter.py | 2 +- default_formatters/package_formatter.py | 2 +- macros/default_macros.py | 5 +- 15 files changed, 140 insertions(+), 133 deletions(-) rename build/{build => }/build.py (50%) delete mode 100644 build/build/__init__.py delete mode 100644 build/build/config.py rename build/{build => utils}/build_information.py (80%) create mode 100644 build/utils/pytex_file.py create mode 100644 build/utils/pytex_msg.py create mode 100644 config/__init__.py create mode 100644 config/constants.py rename base/config.py => config/header_parts.py (90%) diff --git a/base/__init__.py b/base/__init__.py index 299428e..d956802 100644 --- a/base/__init__.py +++ b/base/__init__.py @@ -1,9 +1,7 @@ -from .config import LICENSE, DEFAULT_AUTHOR, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT from .enums import Attributes, Args __all__ = [ 'LICENSE', - 'DEFAULT_AUTHOR', 'PACKAGE_INFO_TEXT', 'PYTEX_INFO_TEXT', 'Args', diff --git a/build/__init__.py b/build/__init__.py index e69de29..3b3e07b 100644 --- a/build/__init__.py +++ b/build/__init__.py @@ -0,0 +1,5 @@ +from .build import build + +__all__ = [ + 'build' +] diff --git a/build/build/build.py b/build/build.py similarity index 50% rename from build/build/build.py rename to build/build.py index 4544a56..99e3f99 100644 --- a/build/build/build.py +++ b/build/build.py @@ -4,101 +4,9 @@ from typing import Optional import git -import PyTeX +from PyTeX.config.constants import BUILD_INFO_FILENAME -from .build_information import BuildInfo - -from PyTeX.build.git_hook.recent import is_recent -from PyTeX.build.git_hook.git_version import get_latest_commit - -BUILD_INFO_FILENAME = 'build_info.json' - - -def pytex_msg(msg: str): - print('[PyTeX] ' + msg) - - -class TexFileToFormat: - def __init__( - self, - src_path: Path, - build_dir: Path, - latex_name: str, - current_build_info: BuildInfo, - last_build_info: Optional[dict], - allow_dirty: bool = False, - overwrite_existing_files: bool = False, - build_all: bool = False): - self.src_path = src_path - self.build_path = build_dir - self.tex_name = latex_name # Still an identifier on how to name the package when being formatted - self.current_build_info = current_build_info - self.last_build_info = last_build_info - self.allow_dirty = allow_dirty - self.overwrite_existing_files: overwrite_existing_files - self.build_all = build_all - - 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 - - def format(self) -> dict: - if self.dirty or self.pytex_dirty: - if not self.allow_dirty: - raise Exception( - '{file} is dirty, but writing dirty files not allowed.'.format( - file=self.src_path.name if self.dirty else 'Submodule PyTeX') - ) - # 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: - return self.last_build_info - - def __format(self) -> dict: - if '.pysty' in self.src_path.name: - formatter = PyTeX.PackageFormatter( - package_name=self.src_path.with_suffix('').name, - extra_header=self.current_build_info.header) - elif '.pycls' in self.src_path.name: - formatter = PyTeX.ClassFormatter( - class_name=self.src_path.with_suffix('').name, - extra_header=self.current_build_info.header) - else: - exit(1) - pytex_msg('Writing file {}'.format(formatter.file_name)) - formatter.make_default_macros() - formatter.format_file(self.src_path, self.build_path) - info = { - 'name': formatter.file_name, - 'source file': self.src_path.name, - '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, - 'dirty': self.dirty - } - return info +from .utils import BuildInfo, pytex_msg, TexFileToFormat def build( diff --git a/build/build/__init__.py b/build/build/__init__.py deleted file mode 100644 index 3b3e07b..0000000 --- a/build/build/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .build import build - -__all__ = [ - 'build' -] diff --git a/build/build/config.py b/build/build/config.py deleted file mode 100644 index a6d8ad0..0000000 --- a/build/build/config.py +++ /dev/null @@ -1,10 +0,0 @@ -BUILD_DETAILS = [ - "Build details:", - " Build time: {build_time}", - " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", - " LatexPackages version: {packages_version} (commit {packages_commit_hash})" -] - -FILENAME_TYPE_PREPEND_AUTHOR = 'prepend-author' -FILENAME_TYPE_RAW_NAME = 'raw' -DATE_FORMAT = '%Y/%m/%d %H:%M' diff --git a/build/utils/__init__.py b/build/utils/__init__.py index e69de29..a29fb42 100644 --- a/build/utils/__init__.py +++ b/build/utils/__init__.py @@ -0,0 +1,9 @@ +from .build_information import BuildInfo +from .pytex_file import TexFileToFormat +from .pytex_msg import pytex_msg + +__all__ = [ + 'BuildInfo', + 'TexFileToFormat', + 'pytex_msg' +] diff --git a/build/build/build_information.py b/build/utils/build_information.py similarity index 80% rename from build/build/build_information.py rename to build/utils/build_information.py index 65b9012..1f9bafc 100644 --- a/build/build/build_information.py +++ b/build/utils/build_information.py @@ -4,22 +4,6 @@ from typing import Optional from PyTeX.build.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 - class BuildInfo: def __init__( diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py new file mode 100644 index 0000000..972bf8e --- /dev/null +++ b/build/utils/pytex_file.py @@ -0,0 +1,93 @@ +from pathlib import Path +from typing import Optional + +from PyTeX.build.git_hook import is_recent, get_latest_commit +from PyTeX import PackageFormatter, ClassFormatter +from .pytex_msg import pytex_msg + +from .build_information import BuildInfo + + +class TexFileToFormat: + def __init__( + self, + src_path: Path, + build_dir: Path, + latex_name: str, + current_build_info: BuildInfo, + last_build_info: Optional[dict], + allow_dirty: bool = False, + overwrite_existing_files: bool = False, + build_all: bool = False): + self.src_path = src_path + self.build_path = build_dir + self.tex_name = latex_name # Still an identifier on how to name the package when being formatted + self.current_build_info = current_build_info + self.last_build_info = last_build_info + self.allow_dirty = allow_dirty + self.overwrite_existing_files: overwrite_existing_files + self.build_all = build_all + + 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 + + def format(self) -> dict: + if self.dirty or self.pytex_dirty: + if not self.allow_dirty: + raise Exception( + '{file} is dirty, but writing dirty files not allowed.'.format( + file=self.src_path.name if self.dirty else 'Submodule PyTeX') + ) + # 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: + return self.last_build_info + + def __format(self) -> dict: + if '.pysty' in self.src_path.name: + formatter = PackageFormatter( + package_name=self.src_path.with_suffix('').name, + author=self.current_build_info.author, + extra_header=self.current_build_info.header) + elif '.pycls' in self.src_path.name: + formatter = ClassFormatter( + class_name=self.src_path.with_suffix('').name, + author=self.current_build_info.author, + extra_header=self.current_build_info.header) + else: + exit(1) + pytex_msg('Writing file {}'.format(formatter.file_name)) + formatter.make_default_macros() + formatter.format_file(self.src_path, self.build_path) + info = { + 'name': formatter.file_name, + 'source file': self.src_path.name, + '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, + 'dirty': self.dirty + } + return info diff --git a/build/utils/pytex_msg.py b/build/utils/pytex_msg.py new file mode 100644 index 0000000..4905001 --- /dev/null +++ b/build/utils/pytex_msg.py @@ -0,0 +1,2 @@ +def pytex_msg(msg: str): + print('[PyTeX] ' + msg) diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..0f7abce --- /dev/null +++ b/config/__init__.py @@ -0,0 +1,13 @@ +from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME +from .header_parts import LICENSE, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT, BUILD_DETAILS + +__all__ = [ + 'FILENAME_TYPE_PREPEND_AUTHOR', + 'FILENAME_TYPE_RAW_NAME', + 'DATE_FORMAT', + 'BUILD_INFO_FILENAME', + 'LICENSE', + 'PACKAGE_INFO_TEXT', + 'PYTEX_INFO_TEXT', + 'BUILD_DETAILS' +] diff --git a/config/constants.py b/config/constants.py new file mode 100644 index 0000000..a2d2e38 --- /dev/null +++ b/config/constants.py @@ -0,0 +1,4 @@ +FILENAME_TYPE_PREPEND_AUTHOR = 'prepend-author' +FILENAME_TYPE_RAW_NAME = 'raw' +DATE_FORMAT = '%Y/%m/%d %H:%M' +BUILD_INFO_FILENAME = 'build_info.json' diff --git a/base/config.py b/config/header_parts.py similarity index 90% rename from base/config.py rename to config/header_parts.py index 281db91..2c2dc8f 100644 --- a/base/config.py +++ b/config/header_parts.py @@ -1,5 +1,3 @@ -DEFAULT_AUTHOR = 'Maximilian Keßler' - LICENSE = [ 'Copyright © {year} {copyright_holders}', '', @@ -39,3 +37,10 @@ PYTEX_INFO_TEXT = [ "changes will not be versioned by Git and overwritten by the next build. Always", "edit the source file and build the {latex_file_type} again." ] + +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/default_formatters/class_formatter.py b/default_formatters/class_formatter.py index 9a1b77b..c93a8a4 100644 --- a/default_formatters/class_formatter.py +++ b/default_formatters/class_formatter.py @@ -4,7 +4,7 @@ import PyTeX.macros class ClassFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, class_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []): + def __init__(self, class_name: str, author: str, extra_header: [str] = []): PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls') def make_default_macros(self): diff --git a/default_formatters/package_formatter.py b/default_formatters/package_formatter.py index 873ef40..774e4ab 100644 --- a/default_formatters/package_formatter.py +++ b/default_formatters/package_formatter.py @@ -4,7 +4,7 @@ import PyTeX.macros class PackageFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, package_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []): + def __init__(self, package_name: str, author: str, extra_header: [str] = []): PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty') def make_default_macros(self): diff --git a/macros/default_macros.py b/macros/default_macros.py index 8dc89ba..d0074df 100644 --- a/macros/default_macros.py +++ b/macros/default_macros.py @@ -1,12 +1,13 @@ import PyTeX.formatter import PyTeX.base +import PyTeX.config def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str): header = '%' * 80 + '\n' \ + '\n'.join(map(lambda line: '% ' + line, - PyTeX.base.LICENSE + [''] + PyTeX.base.PACKAGE_INFO_TEXT + [ - ''] + PyTeX.base.PYTEX_INFO_TEXT + PyTeX.config.LICENSE + [''] + PyTeX.config.PACKAGE_INFO_TEXT + [ + ''] + PyTeX.config.PYTEX_INFO_TEXT + [''] + formatter.extra_header) ) \ + '\n' + '%' * 80 + '\n\n' \ From 12a03e79897dfc4cf3de218ed88ba7c56debfcad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 14:52:10 +0200 Subject: [PATCH 13/14] add parser file for building --- build/__init__.py | 4 +- build/build_parser.py | 99 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 build/build_parser.py diff --git a/build/__init__.py b/build/__init__.py index 3b3e07b..312c358 100644 --- a/build/__init__.py +++ b/build/__init__.py @@ -1,5 +1,7 @@ from .build import build +from .build_parser import parse_and_build __all__ = [ - 'build' + 'build', + 'parse_and_build' ] diff --git a/build/build_parser.py b/build/build_parser.py new file mode 100644 index 0000000..610ec76 --- /dev/null +++ b/build/build_parser.py @@ -0,0 +1,99 @@ +import argparse +import pathlib + +from PyTeX.config import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME + +from .build import build + + +def parse_and_build(arglist: [str]): + parser = argparse.ArgumentParser(description='Incrementally build LatexPackages with PyTeX') + input_group = parser.add_mutually_exclusive_group(required=True) + input_group.add_argument( + '-s', '--source-dir', + metavar='SRC_DIR', + help='Relative or absolute path to source directory of .pysty or .pycls files', + type=pathlib.Path, + nargs='?', + default='./src', + dest='src_dir' + ) + parser.add_argument( + '-b', '--build-dir', + metavar='BUILD_DIR', + help='Relativ or absolute path to output directory for processed packages and classes', + type=pathlib.Path, + nargs='?', + default='./build', + dest='build_dir' + ) + parser.add_argument( + '-r', '--recursive', + help='Recursively search subdirectories for files. Default: false', + action='store_true', + dest='recursive' + ) + input_group.add_argument( + '-i', '--input-file', + metavar='FILE', + help='Filename to be built. Can be in valid .pysty or .pycls format', + type=pathlib.Path, + dest='input_file' + ) + parser.add_argument( + '-n', '--name', + help='Name of the package / class to be formatted.', + type=str, + choices=[FILENAME_TYPE_RAW_NAME, FILENAME_TYPE_PREPEND_AUTHOR], + default=FILENAME_TYPE_PREPEND_AUTHOR, + dest='latex_name' + ) + parser.add_argument( + '-g', '--git-version', + help='Insert git version information into build. This assumes your input' + 'files are located in a git repository. Default: false', + action='store_true', + dest='use_git' + ) + parser.add_argument( + '-d', '--allow-dirty', + help='If git flag is set, allow building of a dirty repo. Default: false', + action='store_true', + dest='allow_dirty' + ) + parser.add_argument( + '-p', + '--pytex-version', + help='Write PyTeX version information into built LaTeX files', + action='store_true', + dest='include_pytex_version' + ) + parser.add_argument( + '-t', '--build-time', + help='Insert build time into built LaTeX files', + action='store_true', + dest='include_timestamp' + ) + parser.add_argument( + '-l', '--license', + help='Insert MIT license into package header', + action='store_true', + dest='include_license' + ) + parser.add_argument( + '-a', '--author', + help='Set author of packages', + type=str, + dest='author' + ) + parser.add_argument( + '-f', '--force', + help='Overwrite unknown existing files without confirmation', + action='store_true', + dest='overwrite_existing_files' + ) + args = vars(parser.parse_args(arglist)) + for arg in args.keys(): + if type(args[arg]) == pathlib.PosixPath: + args[arg] = args[arg].resolve() + build(**args) From 126d420b7a6ed2d4b2a42d534ab2e12344869643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 19:29:23 +0200 Subject: [PATCH 14/14] handle header correctly, i.e. do not only expand on macro --- build/build.py | 10 +++++- build/build_parser.py | 14 +++++++- build/utils/build_information.py | 57 ++++++++++++++++++++++++++++---- build/utils/pytex_file.py | 19 ++++++++--- config/__init__.py | 3 +- config/header_parts.py | 27 ++++++++------- formatter/tex_formatter.py | 17 +++++++--- macros/default_macros.py | 15 ++------- 8 files changed, 117 insertions(+), 45 deletions(-) diff --git a/build/build.py b/build/build.py index 99e3f99..2df2930 100644 --- a/build/build.py +++ b/build/build.py @@ -21,19 +21,27 @@ def build( include_license: bool = False, # header include_git_version: bool = False, # header include_pytex_info_text: bool = False, # header - use_git: bool = False, # versioning (not implemented yet) + extra_header: Optional[Path] = None, allow_dirty: bool = False, # versioning overwrite_existing_files: bool = False, # output control build_all: bool = False, # output control / versioning write_build_information: bool = True, # meta ): pytex_msg('Getting git repository information...') + if extra_header: + if extra_header.exists(): + with open(extra_header, 'r') as f: + text = f.readlines() + extra_header = [line.rstrip() for line in text] + else: + raise FileNotFoundError('Path to extra header content is invalid.') current_build_info = BuildInfo( include_timestamp=include_timestamp, include_pytex_version=include_pytex_version, include_license=include_license, include_git_version=include_git_version, include_pytex_info_text=include_pytex_info_text, + extra_header=extra_header, author=author, pytex_repo=git.Repo(__file__, search_parent_directories=True), packages_repo=git.Repo(src_dir, search_parent_directories=True) diff --git a/build/build_parser.py b/build/build_parser.py index 610ec76..5957f12 100644 --- a/build/build_parser.py +++ b/build/build_parser.py @@ -53,7 +53,7 @@ def parse_and_build(arglist: [str]): help='Insert git version information into build. This assumes your input' 'files are located in a git repository. Default: false', action='store_true', - dest='use_git' + dest='include_git_version' ) parser.add_argument( '-d', '--allow-dirty', @@ -92,6 +92,18 @@ def parse_and_build(arglist: [str]): action='store_true', dest='overwrite_existing_files' ) + parser.add_argument( + '--pytex-info-text', + help='Include a PyTeX info text into headers', + action='store_true', + dest='include_pytex_info_text' + ) + parser.add_argument( + '-e', '--extra-header', + help='Path to file containing extra text for header of each package', + type=pathlib.Path, + dest='extra_header' + ) args = vars(parser.parse_args(arglist)) for arg in args.keys(): if type(args[arg]) == pathlib.PosixPath: diff --git a/build/utils/build_information.py b/build/utils/build_information.py index 1f9bafc..769eabe 100644 --- a/build/utils/build_information.py +++ b/build/utils/build_information.py @@ -1,8 +1,9 @@ import git import datetime -from typing import Optional +from typing import Optional, List from PyTeX.build.git_hook import git_describe, get_latest_commit +from PyTeX.config.header_parts import * class BuildInfo: @@ -13,6 +14,7 @@ class BuildInfo: include_license: bool = False, include_git_version: bool = False, include_pytex_info_text: bool = False, + extra_header: Optional[List[str]] = None, author: Optional[str] = None, pytex_repo: Optional[git.Repo] = None, packages_repo: Optional[git.Repo] = None): @@ -33,11 +35,12 @@ class BuildInfo: self.get_repo_version() self.create_header( - include_timestamp=include_timestamp, - include_pytex_version=include_pytex_version, include_license=include_license, + include_pytex_info_text=include_pytex_info_text, + include_timestamp=include_timestamp, include_git_version=include_git_version, - include_pytex_info_text=include_pytex_info_text + include_pytex_version=include_pytex_version, + extra_header=extra_header ) @property @@ -86,5 +89,47 @@ class BuildInfo: include_pytex_version: bool = False, include_license: bool = False, include_git_version: bool = False, - include_pytex_info_text: bool = False): - self._header = [] # TODO + include_pytex_info_text: bool = False, + extra_header: Optional[List[str]] = None + ): + if not (include_license + or include_pytex_info_text + or include_timestamp + or include_pytex_version + or include_git_version): + self._header = None + return + else: + self._header = [] + if include_license: + self._header += LICENSE + [''] + if include_pytex_info_text: + self._header += PYTEX_INFO_TEXT + [''] + if include_timestamp or include_pytex_version or include_git_version: + self._header += BUILD_DETAILS + if include_timestamp: + self._header += BUILD_TIME + if include_pytex_version: + self._header += PYTEX_VERSION + if include_git_version: + self._header += SOURCE_CODE_VERSION + self._header += [''] + if extra_header: + self._header += extra_header + [''] + + if self._header[-1] == '': + self._header.pop() + + formatted_header = [] + for line in self._header: + formatted_header.append(line.format( + year=datetime.datetime.now().strftime('%Y'), + copyright_holders=self.author, + source_file='{source_file}', + latex_file_type='{latex_file_type}', + pytex_version=self.pytex_version, + pytex_commit_hash=self.pytex_hash[:7], + packages_version=self.packages_version, + packages_commit_hash=self.packages_hash[:7] + )) + self._header = formatted_header diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 972bf8e..42a5758 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Optional +from typing import Optional, List from PyTeX.build.git_hook import is_recent, get_latest_commit from PyTeX import PackageFormatter, ClassFormatter @@ -27,6 +27,8 @@ class TexFileToFormat: self.allow_dirty = allow_dirty self.overwrite_existing_files: overwrite_existing_files self.build_all = build_all + self._header: Optional[List[str]] = None + self.__format_header() 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( @@ -64,19 +66,28 @@ class TexFileToFormat: else: return self.last_build_info + def __format_header(self): + new_header = [] + for line in self.current_build_info.header: + new_header.append(line.format( + source_file=self.src_path.name, + latex_file_type='package' if '.pysty' in self.src_path.name else 'class' + )) + self._header = new_header + def __format(self) -> dict: if '.pysty' in self.src_path.name: formatter = PackageFormatter( package_name=self.src_path.with_suffix('').name, author=self.current_build_info.author, - extra_header=self.current_build_info.header) + extra_header=self._header) elif '.pycls' in self.src_path.name: formatter = ClassFormatter( class_name=self.src_path.with_suffix('').name, author=self.current_build_info.author, - extra_header=self.current_build_info.header) + extra_header=self._header) else: - exit(1) + raise Exception('Programming error. Please contact the developer.') pytex_msg('Writing file {}'.format(formatter.file_name)) formatter.make_default_macros() formatter.format_file(self.src_path, self.build_path) diff --git a/config/__init__.py b/config/__init__.py index 0f7abce..9671b4c 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -1,5 +1,5 @@ from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME -from .header_parts import LICENSE, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT, BUILD_DETAILS +from .header_parts import LICENSE, PYTEX_INFO_TEXT, BUILD_DETAILS __all__ = [ 'FILENAME_TYPE_PREPEND_AUTHOR', @@ -7,7 +7,6 @@ __all__ = [ 'DATE_FORMAT', 'BUILD_INFO_FILENAME', 'LICENSE', - 'PACKAGE_INFO_TEXT', 'PYTEX_INFO_TEXT', 'BUILD_DETAILS' ] diff --git a/config/header_parts.py b/config/header_parts.py index 2c2dc8f..41bf08b 100644 --- a/config/header_parts.py +++ b/config/header_parts.py @@ -19,16 +19,6 @@ LICENSE = [ 'SOFTWARE.' ] -PACKAGE_INFO_TEXT = [ - "This LaTeX {latex_file_type} is free software and distributed under the MIT License. You", - "may use it freely for your purposes. The latest version of the {latex_file_type} can be", - "obtained via GitHub under", - " https://github.com/kesslermaximilian/LatexPackages", - "For further information see the url above.", - "Reportings of bugs, suggestions and improvements are welcome, see the README", - "at the Git repository for further information." -] - PYTEX_INFO_TEXT = [ "This {latex_file_type} has been generated by PyTeX, available at", " https://github.com/kesslermaximilian/PyTeX", @@ -39,8 +29,17 @@ PYTEX_INFO_TEXT = [ ] BUILD_DETAILS = [ - "Build details:", - " Build time: {build_time}", - " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", - " LatexPackages version: {packages_version} (commit {packages_commit_hash})" + "Build details:" +] + +BUILD_TIME = [ + " Build time: {build_time}" +] + +PYTEX_VERSION = [ + " PyTeX version: {pytex_version} (commit {pytex_commit_hash})" +] + +SOURCE_CODE_VERSION = [ + " Source code version: {packages_version} (commit {packages_commit_hash})" ] diff --git a/formatter/tex_formatter.py b/formatter/tex_formatter.py index f773cda..60ca700 100644 --- a/formatter/tex_formatter.py +++ b/formatter/tex_formatter.py @@ -1,15 +1,15 @@ import datetime import re from pathlib import Path -from typing import Dict +from typing import Dict, Optional, List from datetime import * from PyTeX.base import Attributes, Args class TexFormatter: - def __init__(self, name: str, author: str, extra_header: [str], file_extension: str): - self.extra_header = extra_header + def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str): + self.header = header self.name_raw = name self.author = author author_parts = self.author.lower().replace('ß', 'ss').split(' ') @@ -28,6 +28,10 @@ class TexFormatter: def __command_name2keyword(keyword: str): return '__' + keyword.upper().strip().replace(' ', '_') + '__' + @property + def filename(self): + return self.file_name + def __parse_replacement_args(self, match_groups, *user_args, **user_kwargs): new_args = [] for arg in user_args: @@ -93,7 +97,12 @@ class TexFormatter: self.source_file_name = str(input_path.name) input_file = input_path.open() lines = input_file.readlines() - newlines = [] + if self.header: + newlines = '%' * 80 + '\n' \ + + '\n'.join(map(lambda line: '% ' + line, self.header)) \ + + '\n' + '%' * 80 + '\n\n' + else: + newlines = [] for line in lines: newlines += self.__format_string_with_arg(self.__format_string(line)) if output_dir is None: diff --git a/macros/default_macros.py b/macros/default_macros.py index d0074df..2653383 100644 --- a/macros/default_macros.py +++ b/macros/default_macros.py @@ -4,26 +4,15 @@ import PyTeX.config def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str): - header = '%' * 80 + '\n' \ - + '\n'.join(map(lambda line: '% ' + line, - PyTeX.config.LICENSE + [''] + PyTeX.config.PACKAGE_INFO_TEXT + [ - ''] + PyTeX.config.PYTEX_INFO_TEXT - + [''] + formatter.extra_header) - ) \ - + '\n' + '%' * 80 + '\n\n' \ - + '\\NeedsTeXFormat{{LaTeX2e}}\n' \ - '\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n' + header = '\\NeedsTeXFormat{{LaTeX2e}}\n' \ + '\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n' formatter.add_arg_replacement( 1, 'header', header, name_lowercase=PyTeX.base.Attributes.name_lowercase, date=PyTeX.base.Attributes.date, description=PyTeX.base.Args.one, - year=PyTeX.base.Attributes.year, - copyright_holders=PyTeX.base.Attributes.author, - source_file=PyTeX.base.Attributes.source_file_name, Type=latex_file_type.capitalize(), - latex_file_type=latex_file_type ) formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.name_lowercase) formatter.add_replacement('{Type} prefix'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.prefix)