From 0e56f49bed3b536ad5c673ba4591327eb9003892 Mon Sep 17 00:00:00 2001 From: jaimergp Date: Fri, 14 Jun 2024 13:29:58 +0200 Subject: [PATCH] Show CLI flags and package contents in the HTML pages (#17) * Show CLI flags in channel page * Show package filenames in subdir pages * Only process if repodata has data --- conda_subchannel/cli.py | 21 +++++++------ conda_subchannel/core.py | 35 ++++++++++++++++++++-- conda_subchannel/templates/channel.j2.html | 9 ++++++ conda_subchannel/templates/subdir.j2.html | 10 +++++++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/conda_subchannel/cli.py b/conda_subchannel/cli.py index e2c09a8..87f8f64 100644 --- a/conda_subchannel/cli.py +++ b/conda_subchannel/cli.py @@ -114,15 +114,17 @@ def execute(args: argparse.Namespace) -> int: for sd in sorted(subdir_datas, key=lambda sd: sd.channel.name): print(" -", sd.channel.name, sd.channel.subdir) + kwargs = { + "subdir_datas": subdir_datas, + "specs_to_keep": args.keep, + "specs_to_remove": args.remove, + "trees_to_keep": args.keep_tree, + "after": args.after, + "before": args.before, + } with Spinner("Filtering package records"): - records = _reduce_index( - subdir_datas=subdir_datas, - specs_to_keep=args.keep, - specs_to_remove=args.remove, - trees_to_keep=args.keep_tree, - after=args.after, - before=args.before, - ) + + records = _reduce_index(**kwargs) total_count = sum(len(sd._package_records) for sd in subdir_datas) filtered_count = len(records) if total_count == filtered_count: @@ -133,6 +135,7 @@ def execute(args: argparse.Namespace) -> int: with Spinner(f"Writing output to {args.output}"): base_url = args.base_url or Channel(args.channel).base_url repodatas = _dump_records(records, base_url) - _write_to_disk(args.channel, repodatas, args.output) + kwargs.pop("subdir_datas") + _write_to_disk(args.channel, repodatas, args.output, cli_flags=kwargs) return 0 diff --git a/conda_subchannel/core.py b/conda_subchannel/core.py index 1589176..0973116 100644 --- a/conda_subchannel/core.py +++ b/conda_subchannel/core.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import bz2 import hashlib import json @@ -16,6 +17,7 @@ from conda.core.subdir_data import SubdirData from conda.models.channel import Channel from conda.models.match_spec import MatchSpec +from conda.models.version import VersionOrder if TYPE_CHECKING: import os @@ -190,7 +192,7 @@ def _checksum(path, algorithm, buffersize=65536): return hash_impl.hexdigest() -def _write_channel_index_html(source_channel: Channel, channel_path: Path): +def _write_channel_index_html(source_channel: Channel, channel_path: Path, cli_flags: dict[str, Any]): templates_dir = Path(__file__).parent / "templates" environment = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir)) template = environment.get_template("channel.j2.html") @@ -199,7 +201,8 @@ def _write_channel_index_html(source_channel: Channel, channel_path: Path): subchannel_name=channel_path.name, source_channel_url=source_channel.base_url, source_channel_name=source_channel.name, - subdirs=[path.name for path in channel_path.glob("*") if path.is_dir()] + subdirs=[path.name for path in channel_path.glob("*") if path.is_dir()], + cli_flags=cli_flags, ) (channel_path / "index.html").write_text(content) (channel_path / "style.css").write_text((templates_dir / "style.css").read_text()) @@ -210,6 +213,8 @@ def _write_subdir_index_html(subdir_path: Path): environment = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir)) template = environment.get_template("subdir.j2.html") repodatas = [] + packages = [] + base_url = None for path in sorted(subdir_path.glob("*")): if path.name in ("index.md", "index.html"): continue @@ -224,12 +229,23 @@ def _write_subdir_index_html(subdir_path: Path): "md5": _checksum(path, "md5"), } ) + if path.name == "repodata.json": + repodata = json.loads(path.read_text()) + if repodata: + base_url = repodata["info"]["base_url"] + for key in ("packages", "packages.conda"): + for filename in repodata.get(key, ()): + packages.append(filename) + + packages.sort(key=_sortkey_package_filenames) content = template.render( subchannel_name=subdir_path.parent.name, subchannel_url="../", subdir=subdir_path.name, repodatas=repodatas, last_modified=datetime.now(tz=timezone.utc), + base_url=base_url, + packages=packages, ) (subdir_path / "index.html").write_text(content) @@ -238,6 +254,7 @@ def _write_to_disk( source_channel: Channel | str, repodatas: dict[str, dict[str, Any]], path: os.PathLike | str, + cli_flags: dict[str, Any], outputs: Iterable[str] = ("bz2", "zstd"), ): path = Path(path) @@ -267,4 +284,16 @@ def _write_to_disk( noarch_repodata.write_text("{}") _write_subdir_index_html(path / "noarch") - _write_channel_index_html(Channel(source_channel), path) + _write_channel_index_html(Channel(source_channel), path, cli_flags) + + +def _sortkey_package_filenames(fn: str): + basename, ext = os.path.splitext(fn) + name, version, build = basename.rsplit("-", 2) + build_number = build + if "_" in build: + for field in build.split("_"): + if field.isdigit(): + build_number = field + break + return name, VersionOrder(version), build_number, ext \ No newline at end of file diff --git a/conda_subchannel/templates/channel.j2.html b/conda_subchannel/templates/channel.j2.html index d9871c5..73fde66 100644 --- a/conda_subchannel/templates/channel.j2.html +++ b/conda_subchannel/templates/channel.j2.html @@ -16,8 +16,17 @@

Derived from {{ source_channel_name }} + with the following flags:

+ +

Available subdirs: