From b6f8aafd77108d24fae5c9f3697f020db219f1f4 Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Thu, 19 Oct 2023 19:56:33 -0400 Subject: [PATCH 1/5] fix: docker compose ps parsing for version >= 2.21 --- src/algokit/core/sandbox.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/algokit/core/sandbox.py b/src/algokit/core/sandbox.py index c9a5914a..5ed63dae 100644 --- a/src/algokit/core/sandbox.py +++ b/src/algokit/core/sandbox.py @@ -116,7 +116,21 @@ def ps(self, service_name: str | None = None) -> list[dict[str, Any]]: ) if run_results.exit_code != 0: return [] - data = json.loads(run_results.output) + + data = [] + + # docker compose version >= 2.21.0 outputs a blank line when nothing is running + # json.loads with a blank line will throw an error + if len(run_results.output.strip()) > 0: + # docker compose version < 2.21.0 outputs a JSON array, so it will have no newlines + if run_results.output.strip().find("\n") == -1: + data = json.loads(run_results.output) + # docker compose version >= 2.21.0 outputs seperate JSON objects, each on a new line + else: + for line in run_results.output.split("\n"): + if len(line) > 0: + data.append(json.loads(line)) + assert isinstance(data, list) return cast(list[dict[str, Any]], data) From 24e91e3551f19983611206a09067af09bf36f21a Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Thu, 19 Oct 2023 21:52:31 -0400 Subject: [PATCH 2/5] simplify condition for parsing docker compose ps --- src/algokit/core/sandbox.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/algokit/core/sandbox.py b/src/algokit/core/sandbox.py index 5ed63dae..1df59c1a 100644 --- a/src/algokit/core/sandbox.py +++ b/src/algokit/core/sandbox.py @@ -119,17 +119,12 @@ def ps(self, service_name: str | None = None) -> list[dict[str, Any]]: data = [] - # docker compose version >= 2.21.0 outputs a blank line when nothing is running - # json.loads with a blank line will throw an error - if len(run_results.output.strip()) > 0: - # docker compose version < 2.21.0 outputs a JSON array, so it will have no newlines - if run_results.output.strip().find("\n") == -1: - data = json.loads(run_results.output) - # docker compose version >= 2.21.0 outputs seperate JSON objects, each on a new line - else: - for line in run_results.output.split("\n"): - if len(line) > 0: - data.append(json.loads(line)) + # `docker compose ps --format json` on version < 2.21.0 outputs a JSON arary + if run_results.output and run_results.output[0] == "[": + data = json.loads(run_results.output) + # `docker compose ps --format json` on version >= 2.21.0 outputs seperate JSON objects, each on a new line + else: + data = [json.loads(line) for line in run_results.output if line] assert isinstance(data, list) return cast(list[dict[str, Any]], data) From 75e118079a92c39ef3f42be418c867a24ed9885e Mon Sep 17 00:00:00 2001 From: Joe Polny <50534337+joe-p@users.noreply.github.com> Date: Fri, 20 Oct 2023 07:30:18 -0400 Subject: [PATCH 3/5] Use startswith Co-authored-by: Adam Chidlow --- src/algokit/core/sandbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algokit/core/sandbox.py b/src/algokit/core/sandbox.py index 1df59c1a..6488581e 100644 --- a/src/algokit/core/sandbox.py +++ b/src/algokit/core/sandbox.py @@ -120,7 +120,7 @@ def ps(self, service_name: str | None = None) -> list[dict[str, Any]]: data = [] # `docker compose ps --format json` on version < 2.21.0 outputs a JSON arary - if run_results.output and run_results.output[0] == "[": + if run_results.output.startswith("["): data = json.loads(run_results.output) # `docker compose ps --format json` on version >= 2.21.0 outputs seperate JSON objects, each on a new line else: From 9709b8564bc1f4bcc0f958e58fba452745f9ff23 Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Fri, 20 Oct 2023 07:31:31 -0400 Subject: [PATCH 4/5] remove "data = []" --- src/algokit/core/sandbox.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/algokit/core/sandbox.py b/src/algokit/core/sandbox.py index 6488581e..26802eb1 100644 --- a/src/algokit/core/sandbox.py +++ b/src/algokit/core/sandbox.py @@ -117,8 +117,6 @@ def ps(self, service_name: str | None = None) -> list[dict[str, Any]]: if run_results.exit_code != 0: return [] - data = [] - # `docker compose ps --format json` on version < 2.21.0 outputs a JSON arary if run_results.output.startswith("["): data = json.loads(run_results.output) From ec99214a0f2b1e7e3b7f2a76cb901b96e35506ed Mon Sep 17 00:00:00 2001 From: Joe Polny <50534337+joe-p@users.noreply.github.com> Date: Fri, 20 Oct 2023 08:55:26 -0400 Subject: [PATCH 5/5] use splitlines Co-authored-by: Al --- src/algokit/core/sandbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algokit/core/sandbox.py b/src/algokit/core/sandbox.py index 26802eb1..6800075c 100644 --- a/src/algokit/core/sandbox.py +++ b/src/algokit/core/sandbox.py @@ -122,7 +122,7 @@ def ps(self, service_name: str | None = None) -> list[dict[str, Any]]: data = json.loads(run_results.output) # `docker compose ps --format json` on version >= 2.21.0 outputs seperate JSON objects, each on a new line else: - data = [json.loads(line) for line in run_results.output if line] + data = [json.loads(line) for line in run_results.output.splitlines() if line] assert isinstance(data, list) return cast(list[dict[str, Any]], data)