|
4 | 4 | import os |
5 | 5 | from pathlib import Path |
6 | 6 | import shutil |
| 7 | +import shlex |
7 | 8 | from tempfile import TemporaryDirectory |
8 | 9 | from urllib.parse import urlparse |
9 | 10 | import warnings |
@@ -158,8 +159,7 @@ def post_build(self, manager): |
158 | 159 | raise ValueError(f"Environment name '{env_name}' used more than once") |
159 | 160 | self.prefixes[env_name] = prefix |
160 | 161 | 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) |
163 | 163 |
|
164 | 164 | all_kernels = [] |
165 | 165 | for env_name, prefix in self.prefixes.items(): |
@@ -196,6 +196,35 @@ def get_environment_specs(self, prefix): |
196 | 196 | print(f"Error parsing line: {spec_line} — {e}") |
197 | 197 | return specs |
198 | 198 |
|
| 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 | + |
199 | 228 | def create_prefix(self, env_file: Path): |
200 | 229 | # read the environment file |
201 | 230 | root_prefix = Path(self.cwd_name) / "_env" |
|
0 commit comments