Skip to content

Commit 6658aa7

Browse files
authored
Fix handling of channels from prefixes (#328)
* Fix handling of channels from prefixes * Draft * Implement channels extracting behavior * Remove debug log
1 parent 12e8620 commit 6658aa7

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

jupyterlite_xeus/add_on.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from pathlib import Path
66
import shutil
7+
import shlex
78
from tempfile import TemporaryDirectory
89
from urllib.parse import urlparse
910
import warnings
@@ -158,8 +159,7 @@ def post_build(self, manager):
158159
raise ValueError(f"Environment name '{env_name}' used more than once")
159160
self.prefixes[env_name] = prefix
160161
self.specs[env_name] = self.get_environment_specs(prefix)
161-
# For lack of a better way for now
162-
self.channels[env_name] = DEFAULT_CHANNELS
162+
self.channels[env_name] = self.get_environment_channels(prefix)
163163

164164
all_kernels = []
165165
for env_name, prefix in self.prefixes.items():
@@ -196,6 +196,35 @@ def get_environment_specs(self, prefix):
196196
print(f"Error parsing line: {spec_line}{e}")
197197
return specs
198198

199+
def get_environment_channels(self, prefix):
200+
if isinstance(prefix, str):
201+
history_file_path = Path(prefix)
202+
path = history_file_path / "conda-meta" / "history"
203+
channels = []
204+
with open(path, "r") as f:
205+
for line in f:
206+
if line.startswith("# cmd:"):
207+
tokens = shlex.split(line.replace("# cmd:", ""))
208+
i = 0
209+
while i < len(tokens):
210+
tok = tokens[i]
211+
212+
# Handle "-c URL" or "--channel URL"
213+
if tok in ("-c", "--channel"):
214+
if i + 1 < len(tokens):
215+
channels.append(tokens[i + 1])
216+
i += 2
217+
continue
218+
219+
# Handle "--channel=URL"
220+
if tok.startswith("--channel="):
221+
channels.append(tok.split("=", 1)[1])
222+
i += 1
223+
continue
224+
225+
i += 1
226+
return channels
227+
199228
def create_prefix(self, env_file: Path):
200229
# read the environment file
201230
root_prefix = Path(self.cwd_name) / "_env"

tests/test_xeus.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ def test_multiple_envs():
210210
update_action_args = update_cp_env_steps["actions"][0][1]
211211
expected_tmp_path = update_action_args[0]
212212
assert expected_tmp_path.name == "empack_env_meta.json"
213-
assert isinstance(update_action_args[1], dict)
214-
assert "specs" in update_action_args[1]
215213

216214
copy_action_args = update_cp_env_steps["actions"][1][1]
217215
assert copy_action_args[1] == target_path / env_name_tmp / "empack_env_meta.json"

0 commit comments

Comments
 (0)