Skip to content

Commit cd6efc3

Browse files
authored
Add package url factory (#108)
* Add package url factory * Update test
1 parent 6b8a7a5 commit cd6efc3

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

empack/pack.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
from pathlib import Path, PosixPath, PureWindowsPath
77
from tempfile import TemporaryDirectory
8+
from typing import Callable, Optional
89

910
from appdirs import user_cache_dir
1011

@@ -171,6 +172,7 @@ def pack_env(
171172
compression_format=ALLOWED_FORMATS[0],
172173
compresslevel=9,
173174
outdir=None,
175+
package_url_factory: Optional[Callable] = None,
174176
):
175177
with TemporaryDirectory() as tmp_dir:
176178
# filter the complete environment
@@ -196,16 +198,22 @@ def pack_env(
196198
)
197199

198200
base_fname = filename_base_from_meta(pkg_meta)
199-
packages_info.append(
200-
dict(
201-
name=pkg_meta["name"],
202-
version=pkg_meta["version"],
203-
build=pkg_meta["build"],
204-
filename_stem=base_fname,
205-
filename=f"{base_fname}.tar.{compression_format}",
206-
)
201+
202+
pkg_dict = dict(
203+
name=pkg_meta["name"],
204+
version=pkg_meta["version"],
205+
build=pkg_meta["build"],
206+
filename_stem=base_fname,
207+
filename=f"{base_fname}.tar.{compression_format}",
207208
)
208209

210+
package_url = None
211+
if package_url_factory:
212+
package_url = package_url_factory(pkg_dict)
213+
if package_url is not None:
214+
pkg_dict["url"] = package_url
215+
packages_info.append(pkg_dict)
216+
209217
# save the list of packages
210218
env_meta = {
211219
"prefix": str(relocate_prefix),

tests/test_pack.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,48 @@ def test_pack_env(tmp_path, packages, relocate_prefix):
163163
assert pkg_info["name"] == pkg_meta["name"]
164164

165165

166+
def test_pack_env_with_url(tmp_path):
167+
# create the env at the temporary location
168+
packages = ["python=3.11", "numpy"]
169+
relocate_prefix = "/"
170+
prefix = tmp_path / "env"
171+
172+
create_environment(
173+
prefix=prefix,
174+
packages=packages,
175+
channels=CHANNELS,
176+
relocate_prefix=relocate_prefix,
177+
platform="emscripten-wasm32",
178+
)
179+
180+
def url_factory(pkg):
181+
return f'http://localhost:1234/{pkg["filename"]}'
182+
183+
pack_env(
184+
env_prefix=prefix,
185+
outdir=tmp_path,
186+
use_cache=False,
187+
compression_format="gz",
188+
relocate_prefix=relocate_prefix,
189+
file_filters=FILE_FILTERS,
190+
compresslevel=1,
191+
package_url_factory=url_factory,
192+
)
193+
194+
# check that there is a json with all the packages
195+
env_metadata_json_path = tmp_path / "empack_env_meta.json"
196+
assert env_metadata_json_path.exists()
197+
198+
# check that json file contains all packages
199+
with open(env_metadata_json_path) as f:
200+
env_metadata = json.load(f)
201+
packages_metadata = env_metadata["packages"]
202+
203+
# check that there is a tar.gz file for each package
204+
for pkg_info in packages_metadata:
205+
assert "http://localhost:1234" in pkg_info["url"]
206+
207+
166208
@pytest.mark.parametrize("mount_dir", ["/some", "/some/", "/some/nested", "/"])
167209
def test_pack_directory(tmp_path, mount_dir):
168210
# create a directory with some files

0 commit comments

Comments
 (0)