Parse a ``source=target[,source=target...]`` env var. Both source and target must be absolute paths. Returns an empty list when the env var is unset or blank. Raises ``ValueError`` on malformed entries.
(env_var: str)
| 170 | |
| 171 | |
| 172 | def _parse_path_mapping(env_var: str) -> list[PathMapping]: |
| 173 | """Parse a ``source=target[,source=target...]`` env var. |
| 174 | |
| 175 | Both source and target must be absolute paths. Returns an empty list when |
| 176 | the env var is unset or blank. Raises ``ValueError`` on malformed entries. |
| 177 | """ |
| 178 | raw = os.environ.get(env_var, "") |
| 179 | if not raw.strip(): |
| 180 | return [] |
| 181 | |
| 182 | mappings: list[PathMapping] = [] |
| 183 | for entry in raw.split(","): |
| 184 | entry = entry.strip() |
| 185 | if not entry: |
| 186 | continue |
| 187 | parts = entry.split("=", 1) |
| 188 | if len(parts) != 2 or not parts[0] or not parts[1]: |
| 189 | raise ValueError(f"{env_var}: invalid entry {entry!r}, expected format 'source=target'") |
| 190 | source = Path(parts[0]) |
| 191 | target = Path(parts[1]) |
| 192 | if not source.is_absolute(): |
| 193 | raise ValueError(f"{env_var}: source path must be absolute, got {source!r}") |
| 194 | if not target.is_absolute(): |
| 195 | raise ValueError(f"{env_var}: target path must be absolute, got {target!r}") |
| 196 | mappings.append(PathMapping(source=source.resolve(), target=target.resolve())) |
| 197 | return mappings |
| 198 | |
| 199 | |
| 200 | def _apply_mapping(mappings: list[PathMapping], path: str | Path, reverse: bool = False) -> str: |
no test coverage detected