Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions dbuild/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
_GLOBAL_CONFIG_PATH: Path | None = None



def _split_compose(entry: str) -> list[str]:
"""Split a short-form compose volume or port on ``:``, but not inside
``${...}``: ``${DIR:-/srv/x}:/x:ro`` is source ``${DIR:-/srv/x}``,
target ``/x``, options ``ro``."""
parts, cur, depth = [], "", 0
i = 0
while i < len(entry):
if entry.startswith("${", i):
depth += 1
cur += "${"
i += 2
continue
c = entry[i]
if c == "}" and depth:
depth -= 1
if c == ":" and not depth:
parts.append(cur)
cur = ""
else:
cur += c
i += 1
parts.append(cur)
return parts

def _confident_prefix() -> Path | None:
"""Return the install prefix only when it can be determined reliably.

Expand Down Expand Up @@ -636,7 +661,7 @@ def _parse_service_data(
# Volumes
for v in service.get("volumes", []):
if isinstance(v, str):
parts = v.split(":")
parts = _split_compose(v)
src = parts[0]
tgt = parts[1] if len(parts) > 1 else parts[0]
opts = parts[2] if len(parts) > 2 else ""
Expand All @@ -649,7 +674,7 @@ def _parse_service_data(
# Ports
for p in service.get("ports", []):
if isinstance(p, str):
parts = p.split(":")
parts = _split_compose(p)
pub = parts[0]
tgt = parts[1] if len(parts) > 1 else parts[0]
proto = "tcp"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,13 @@ def test_local_architectures_override_global(self, _mock_org):

if __name__ == "__main__":
unittest.main()


def test_split_compose_keeps_env_defaults_whole():
from dbuild.config import _split_compose

assert _split_compose("${RP_PODMAN_DIR:-/path/to/containers/reverse-proxy/podman}:/var/run/podman:ro") == [
"${RP_PODMAN_DIR:-/path/to/containers/reverse-proxy/podman}", "/var/run/podman", "ro"]
assert _split_compose("/a:/b:ro") == ["/a", "/b", "ro"]
assert _split_compose("${P:-80}:80") == ["${P:-80}", "80"]
assert _split_compose("127.0.0.1:53:53/udp") == ["127.0.0.1", "53", "53/udp"]
Loading