Skip to content

Commit

Permalink
Show CLI flags and package contents in the HTML pages (#17)
Browse files Browse the repository at this point in the history
* Show CLI flags in channel page

* Show package filenames in subdir pages

* Only process if repodata has data
  • Loading branch information
jaimergp authored Jun 14, 2024
1 parent b01c4f9 commit 0e56f49
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
21 changes: 12 additions & 9 deletions conda_subchannel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
35 changes: 32 additions & 3 deletions conda_subchannel/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import bz2
import hashlib
import json
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions conda_subchannel/templates/channel.j2.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ <h1>
<p>
Derived from
<a href="{{ source_channel_url }}">{{ source_channel_name }}</a>
with the following flags:
</p>

<ul>
{% for flag, value in cli_flags.items()|sort %}
{% if value %}
<li><code>{{ flag }}</code>: <code>{{ value }}</code></li>
{% endif %}
{% endfor %}
</ul>

<p>Available subdirs:</p>

<ul>
Expand Down
10 changes: 10 additions & 0 deletions conda_subchannel/templates/subdir.j2.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ <h1 id="{{ subchannel_name }}-{{ subdir }}">
{% endfor %}
</tbody>
</table>
<p>
<details>
<summary>Show {{ packages|length }} packages</summary>
<ul>
{% for package in packages %}
<li><a href="{{ base_url + '/' + subdir + '/' + package }}" target="_blank">{{ package }}</a></li>
{% endfor %}
</ul>
</details>
</p>
<p>
<small>Last modified on {{ last_modified.strftime('%Y-%m-%d %H:%M:%S %Z') }}</small>
</p>
Expand Down

0 comments on commit 0e56f49

Please sign in to comment.