pthbs_genpkgpy

Template engine for producing packages for pthbs written using Python and Jinja
git clone https://ccx.te2000.cz/git/pthbs_genpkgpy
Log | Files | Refs | Submodules | README

commit 2fa2b94fac465a4ef847afcf5643c550e80174d6
parent ab9ad05c798eca7e22231813fc1cdbbf36cdec1d
Author: ccx <ccx@te2000.cz>
Date:   Wed, 30 Oct 2024 20:28:23 +0000

Change path handling for submodule usage

Diffstat:
Mgenpkg.py | 72+++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mgenpkgpy.mk | 8++++++--
2 files changed, 57 insertions(+), 23 deletions(-)

diff --git a/genpkg.py b/genpkg.py @@ -10,9 +10,9 @@ import yaml class SubmoduleInfo: - def __init__(self): + def __init__(self, cache_dir='cache'): self._current_commits = None - self._by_commit = Path('./sources/by-commit') # TODO: configurable + self._by_commit = Path(cache_dir) / "link" / "git-commit-sha1" # TODO: configurable @property def current(self): @@ -28,7 +28,7 @@ class SubmoduleInfo: } for repo, commit in self._current_commits.items(): if not (self._by_commit / commit).exists(): - os.symlink("../" + repo, str(self._by_commit / commit)) + raise RuntimeError(f"commit ID does not seem to be linked: {commit} from {repo!r} in {self._by_commit}") return self._current_commits def commit_info(self, commit_id): @@ -43,9 +43,10 @@ class SubmoduleInfo: class DownloadsInfo: - def __init__(self): + def __init__(self, downloadlist_path): + assert isinstance(downloadlist_path, Path) self._basenames = {} - with open('downloadlist.sha256', 'rt') as f: # TODO: configurable + with downloadlist_path.open('rt') as f: # TODO: configurable for line in f: if line[0] in '#\n': continue @@ -68,9 +69,12 @@ class DownloadsInfo: class FileInfo: - def __init__(self): + def __init__(self, files_dir, cache_dir): + assert isinstance(files_dir, Path) + assert isinstance(cache_dir, Path) + self._files_dir = files_dir self._sha256_cache = {} - self._files_dir = Path('./files') # TODO: configurable + self._by_sha256 = Path(cache_dir) / "link" / "files-sha256" # TODO: configurable def __getitem__(self, key): if key in self._sha256_cache: @@ -79,13 +83,33 @@ class FileInfo: with fp.open('rb') as f: file_hash = hashlib.file_digest(f, "sha256").hexdigest() self._sha256_cache[key] = file_hash - if not (self._files_dir / "by-sha256" / file_hash).exists(): - os.symlink("../" + key, str(self._files_dir / "by-sha256" / file_hash)) + if not (self._by_sha256 / file_hash).exists(): + raise RuntimeError(f"file {key!r} does not seem to be linked as {file_hash} from {self._by_sha256}") return file_hash +def parse_filelist(filelist_path): + assert isinstance(filelist_path, Path) + with filelist_path.open('rt') as f: + return { + os.path.basename(fname): fhash + for fhash, fname + in (line.rstrip('\n').split(' ', maxsplit=1) for line in f) + } + + class Main: - def __init__(self, out_dir="packages", template_dir="templates"): + argument_parser = argparse.ArgumentParser() + argument_parser.add_argument('-P', '--package-dir', default='packages') + argument_parser.add_argument('-T', '--template-dir', default='templates') + argument_parser.add_argument('-I', '--index-dir', default='.') + argument_parser.add_argument('-C', '--cache-dir', default='cache') + + def __init__(self, out_dir, template_dir, index_dir, cache_dir): + assert isinstance(out_dir, Path) + assert isinstance(template_dir, Path) + assert isinstance(index_dir, Path) + assert isinstance(cache_dir, Path) self.out_dir = Path(out_dir) self.template_dir = Path(template_dir) self.env = jinja2.Environment( @@ -96,13 +120,24 @@ class Main: self.env.globals["pkg_sha256"] = self.pkg_sha256 self.env.globals["pkg_install_name"] = self.pkg_install_name self.env.globals["pkg_install_dir"] = self.pkg_install_dir - self.env.globals["submodule"] = SubmoduleInfo() - self.env.globals["files"] = FileInfo() - self.env.globals["downloads"] = DownloadsInfo() + self.env.globals["submodule"] = SubmoduleInfo(cache_dir=cache_dir) + # self.env.globals["files"] = FileInfo(files_dir=Path('files'), cache_dir=cache_dir) + self.env.globals["files"] = parse_filelist(index_dir / 'filelist.sha256') + self.env.globals["downloads"] = DownloadsInfo(index_dir / 'downloadlist.sha256') self.package_hashes = {} self.rendering = [] self.deps = {} + @classmethod + def from_argv(cls, args=None): + args = cls.argument_parser.parse_args(args) + return cls( + out_dir=Path(args.package_dir), + template_dir=Path(args.template_dir), + index_dir=Path(args.index_dir), + cache_dir=Path(args.cache_dir), + ) + def load_vars_yaml(self, fname="vars.yaml"): with open(fname) as f: self.env.globals.update(yaml.safe_load(f)) @@ -186,17 +221,12 @@ class Main: ) -argument_parser = argparse.ArgumentParser() -argument_parser.add_argument('-P', '--package-dir', default='packages') -argument_parser.add_argument('-T', '--template-dir', default='templates') - if __name__ == '__main__': - from pprint import pprint as pp + #from pprint import pprint as pp - args = argument_parser.parse_args() - m = Main(out_dir=args.package_dir, template_dir=args.template_dir) + m = Main.from_argv() m.load_vars_yaml() - pp(m.env.list_templates(filter_func=lambda name: "/." not in name)) + #pp(m.env.list_templates(filter_func=lambda name: "/." not in name)) m.render_all() # pylama:linters=pycodestyle,pyflakes:ignore=D212,D203,D100,D101,D102,D105,D107 diff --git a/genpkgpy.mk b/genpkgpy.mk @@ -32,8 +32,12 @@ py-venv: $(VENV)/.done py-virtualenv: py-venv -py-genpkg: $(VENV)/.done - '$(VENV)/bin/python' genpkg.py --package-dir='$(packages)' --template-dir='$(templates)' +py-genpkg: $(VENV)/.done $(cache)/link/git-commit-sha1/.local $(cache)/link/file-sha256/.local + '$(VENV)/bin/python' $(pthbs_genpkgpy)/genpkg.py \ + --package-dir='$(packages)' \ + --template-dir='$(templates)' \ + --index-dir='$(index)' \ + --cache-dir='$(cache)' # -- requirement file rules