Skip to content

Commit

Permalink
Implement --base-url CLI flag (#9)
Browse files Browse the repository at this point in the history
* add base_url argument

* fix repodata schema

* fix header

* fix readme

* add test
  • Loading branch information
jaimergp authored May 16, 2024
1 parent 522d787 commit 6f4ead9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Create subsets of conda channels thanks to CEP-15 metadata

```bash
$ conda install -n base conda-subchannel
$ conda subchannel --channel=conda-forge python=3.9
$ conda subchannel --channel=conda-forge --keep-tree python=3.9
$ python -m http.serve --directory subchannel/
```

Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: "Source repodata file to process from channel"
required: false
default: "repodata.json"
base-url:
description: "URL where the packages will be available. Defaults to the base URL for 'channel'"
required: false
default: ""
subdirs:
description: "List of platforms to support, space separated. Defaults to linux-64. Noarch is always included"
required: false
Expand Down Expand Up @@ -91,6 +95,9 @@ runs:
"--repodata-fn",
"""${{ inputs.repodata-fn }}""".strip(),
]
base_url = """${{ inputs.base-url }}""".strip()
if base_url:
args += ["--base_url", base_url]
after = """${{ inputs.after }}""".strip()
if after:
args += ["--after", after]
Expand Down
11 changes: 10 additions & 1 deletion conda_subchannel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from conda.base.constants import REPODATA_FN
from conda.base.context import context
from conda.common.io import Spinner
from conda.models.channel import Channel

from .core import _fetch_channel, _reduce_index, _dump_records, _write_to_disk

Expand Down Expand Up @@ -47,6 +48,13 @@ def configure_parser(parser: argparse.ArgumentParser):
default=REPODATA_FN,
help="Source repodata file to process from channel.",
)
parser.add_argument(
"--base-url",
required=False,
help="URL where the packages will be available. "
"Defaults to the base URL for '--channel'. Only needed if the user wants to mirror "
"the required packages separately."
)
parser.add_argument(
"--output",
default="subchannel",
Expand Down Expand Up @@ -123,7 +131,8 @@ def execute(args: argparse.Namespace) -> int:
print(" - Reduced from", total_count, "to", filtered_count, "records")

with Spinner(f"Writing output to {args.output}"):
repodatas = _dump_records(records, args.channel)
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)

return 0
33 changes: 24 additions & 9 deletions conda_subchannel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,40 @@ def _reduce_index(


def _dump_records(
records: dict[tuple[str, str], PackageRecord], source_channel: Channel | str
records: dict[tuple[str, str], PackageRecord], base_url: str
) -> dict[str, dict[str, Any]]:
source_channel = Channel(source_channel)
repodatas = {}
record_keys = (
"build",
"build_number",
"constrains",
"depends",
"license",
"md5",
"name",
"sha256",
"size",
"subdir",
"timestamp",
"version",
)
for (subdir, filename), record in records.items():
if subdir not in repodatas:
repodatas[subdir] = {
"metadata": {
"repodata_version": 2,
"base_url": source_channel.base_url,
"repodata_version": 2,
"info": {
"base_url": base_url,
"subdir": subdir,
},
"packages": {},
"packages.conda": {},
"removed": [],
}
key = "packages.conda" if record.fn.endswith(".conda") else "packages"
repodatas[record.subdir][key][filename] = record.dump()
dumped = record.dump()
repodatas[record.subdir][key][filename] = {
key: dumped[key] for key in record_keys if key in dumped
}
return repodatas


Expand Down Expand Up @@ -206,9 +223,7 @@ def _write_subdir_index_md(subdir_path: Path):
lastmod = datetime.fromtimestamp(stat.st_mtime)
sha256 = _checksum(path, "sha256")
md5 = _checksum(path, "md5")
lines.append(
f"| [{path.name}]({path.name}) | {size} | {lastmod} | `{sha256}` | `{md5}` |"
)
lines.append(f"| [{path.name}]({path.name}) | {size} | {lastmod} | `{sha256}` | `{md5}` |")
lines.append("")
lines.append(f"> Last modified on {datetime.now(tz=timezone.utc)}")
(subdir_path / "index.md").write_text("\n".join(lines))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_subchannel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
from datetime import datetime, timezone

Expand Down Expand Up @@ -210,3 +211,23 @@ def test_between_dates(conda_cli, tmp_path):
timestamp_2023 <= rec.timestamp <= timestamp_2024 for rec in sd.iter_records()
)
assert tested

def test_base_url(conda_cli, tmp_path):
base_url = "https://a-redefined-base.url"
out, err, rc = conda_cli(
"subchannel",
"-c",
"conda-forge",
"--keep",
"python=3.9",
"--base-url",
base_url,
"--output",
tmp_path,
)
print(out)
print(err, file=sys.stderr)
assert rc == 0

data = json.loads((tmp_path / context.subdir / "repodata.json").read_text())
assert data["info"]["base_url"] == base_url

0 comments on commit 6f4ead9

Please sign in to comment.