Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for base_url #151

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions conda_index/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
will keep python 3.8.X and 3.9.Y in the current_index.json, instead of only the very latest python version.
""",
)
@click.option(
"--base-url",
help="""
If packages should be served separately from repodata.json, URL of the
directory tree holding packages. Generates repodata_version=2 which is
incompatible with existing versions of conda.
dholth marked this conversation as resolved.
Show resolved Hide resolved
""",
jezdez marked this conversation as resolved.
Show resolved Hide resolved
)
@click.option("--threads", default=MAX_THREADS_DEFAULT, show_default=True)
@click.option(
"--verbose",
Expand All @@ -107,6 +115,7 @@ def cli(
rss=False,
run_exports=False,
compact=True,
base_url=None,
):
logutil.configure()
if verbose:
Expand All @@ -125,6 +134,7 @@ def cli(
threads=threads,
write_run_exports=run_exports,
compact_json=compact,
base_url=base_url,
)

current_index_versions = None
Expand Down
11 changes: 10 additions & 1 deletion conda_index/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,15 @@ class ChannelIndex:
:param output_root: Path to write repodata.json etc; defaults to channel_root.
:param channel_url: fsspec URL where package files live. If provided, channel_root will only be used for cache and index output.
:param fs: ``MinimalFS`` instance to be used with channel_url. Wrap fsspec AbstractFileSystem with ``conda_index.index.fs.FsspecFS(fs)``.
:param base_url: Add ``base_url/<subdir>`` to repodata.json to be able to host packages separate from repodata.json
"""

fs: MinimalFS | None = None
channel_url: str | None = None

def __init__(
self,
channel_root: Path,
channel_root: Path | str,
channel_name: str | None,
subdirs: Iterable[str] | None = None,
threads: int | None = MAX_THREADS_DEFAULT,
Expand All @@ -499,8 +500,10 @@ def __init__(
write_zst=False,
write_run_exports=False,
compact_json=True,
*,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make newer arguments keyword-only and not passable by position

channel_url: str | None = None,
fs: MinimalFS | None = None,
base_url: str | None = None,
):
if threads is None:
threads = MAX_THREADS_DEFAULT
Expand All @@ -526,6 +529,7 @@ def __init__(
self.write_zst = write_zst
self.write_run_exports = write_run_exports
self.compact_json = compact_json
self.base_url = base_url

def index(
self,
Expand Down Expand Up @@ -748,6 +752,11 @@ def index_subdir(self, subdir, verbose=False, progress=False):
"removed": [], # can be added by patch/hotfix process
}

if self.base_url:
# per https://github.com/conda-incubator/ceps/blob/main/cep-15.md
new_repodata["info"]["base_url"] = f"{self.base_url.rstrip('/')}/{subdir}/"
new_repodata["repodata_version"] = 2
jezdez marked this conversation as resolved.
Show resolved Hide resolved

return new_repodata

def cache_for_subdir(self, subdir):
Expand Down
4 changes: 1 addition & 3 deletions conda_index/index/convert_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ def basename(path):
return path
return path.rsplit("/")[-1]

conn.create_function(
"migrate_basename", narg=1, func=basename, deterministic=True
)
conn.create_function("migrate_basename", narg=1, func=basename, deterministic=True)

for table in TABLE_NAMES + ["stat"]:
conn.execute(
Expand Down
20 changes: 20 additions & 0 deletions news/base-url
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Enhancements

* Add support for CEP-15 ``base_url`` to host packages separate from repodata.
(#150)

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
32 changes: 32 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import shutil
import tarfile
import urllib.parse
from logging import getLogger
from os.path import dirname, isdir, isfile, join
from pathlib import Path
Expand Down Expand Up @@ -1303,3 +1304,34 @@ def test_bad_patch_version(index_data):
channel_index._create_patch_instructions(
"noarch", {"packages": {}}, patch_generator=str(instructions)
)


def test_base_url(index_data):
"""
conda-index should be able to add base_url to repodata.json.
"""
pkg_dir = Path(index_data, "packages")

# compact json
channel_index = conda_index.index.ChannelIndex(
pkg_dir,
None,
write_bz2=False,
write_zst=False,
compact_json=True,
threads=1,
base_url="https://example.org/somechannel/",
)

channel_index.index(None)

osx = json.loads((pkg_dir / "osx-64" / "repodata.json").read_text())
noarch = json.loads((pkg_dir / "noarch" / "repodata.json").read_text())

assert osx["repodata_version"] == 2

assert osx["info"]["base_url"] == "https://example.org/somechannel/osx-64/"
assert noarch["info"]["base_url"] == "https://example.org/somechannel/noarch/"

package_url = urllib.parse.urljoin(osx["info"]["base_url"], "package-1.0.conda")
assert package_url == "https://example.org/somechannel/osx-64/package-1.0.conda"