Skip to content

Commit 415442c

Browse files
GitHKAndrei Neagumergify[bot]
authored
✨ allows ooil to escape legacy format in y*ml files inside .osparc folder (#8085)
Co-authored-by: Andrei Neagu <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent e1d722b commit 415442c

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

packages/service-integration/src/service_integration/cli/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .._meta import __version__
99
from ..settings import AppSettings
10-
from . import _compose_spec, _metadata, _run_creator, _test
10+
from . import _compose_spec, _escaping, _metadata, _run_creator, _test
1111
from ._config import config_app
1212

1313
app = typer.Typer()
@@ -72,6 +72,7 @@ def main(
7272
app.command("compose")(_compose_spec.create_compose)
7373
app.add_typer(config_app, name="config", help="Manage osparc config files")
7474
app.command("test")(_test.run_tests)
75+
app.command("legacy-escape")(_escaping.legacy_escape)
7576
# legacy
7677
app.command("bump-version")(_metadata.bump_version)
7778
app.command("get-version")(_metadata.get_version)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import re
2+
from pathlib import Path
3+
from typing import Annotated
4+
5+
import typer
6+
7+
from ..osparc_config import OSPARC_CONFIG_DIRNAME
8+
9+
10+
def escape_dollar_brace(text: str) -> str:
11+
# the pattern finds '$${' that is not preceded by another '$'.
12+
pattern = r"(?<!\$)\$\${"
13+
replacement = "$$$${"
14+
return re.sub(pattern, replacement, text)
15+
16+
17+
def legacy_escape(
18+
osparc_config_dirname: Annotated[
19+
Path,
20+
typer.Option(
21+
"--osparc-config-dirname",
22+
help="Path to where the .osparc configuration directory is located",
23+
),
24+
] = Path(OSPARC_CONFIG_DIRNAME),
25+
):
26+
"""
27+
Replaces all '$${' sequences with '$$$${' unless they are part of an
28+
existing '$$$${' sequence.
29+
"""
30+
31+
# NOTE: since https://github.com/docker/compose/releases/tag/v2.35.0
32+
# docker-compose was fixed/changed to escape `${}`
33+
# SEE https://github.com/docker/compose/pull/12664
34+
35+
if not osparc_config_dirname.exists():
36+
msg = "Invalid path to metadata file or folder"
37+
raise typer.BadParameter(msg)
38+
39+
for file in osparc_config_dirname.rglob("*.y*ml"):
40+
read_text = file.read_text()
41+
replaced_text = escape_dollar_brace(read_text)
42+
if read_text != replaced_text:
43+
print(f"Escaped sequence in {file}")
44+
file.write_text(replaced_text)
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,56 @@
1+
# pylint:disable=redefined-outer-name
2+
3+
import os
4+
import shutil
5+
import traceback
16
from collections.abc import Callable
7+
from pathlib import Path
28

9+
import pytest
10+
from click.testing import Result
311
from service_integration import __version__
412

513

14+
def _format_cli_error(result: Result) -> str:
15+
assert result.exception
16+
tb_message = "\n".join(traceback.format_tb(result.exception.__traceback__))
17+
return f"Below exception was raised by the cli:\n{tb_message}"
18+
19+
620
def test_cli_help(run_program_with_args: Callable):
721
result = run_program_with_args(
822
"--help",
923
)
10-
assert result.exit_code == 0
24+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
1125

1226

1327
def test_cli_version(run_program_with_args: Callable):
1428
result = run_program_with_args(
1529
"--version",
1630
)
17-
assert result.exit_code == 0
31+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
1832
assert __version__ == result.output.strip()
33+
34+
35+
@pytest.fixture
36+
def copy_tests_data_dir(tests_data_dir: Path, tmp_path: Path) -> Path:
37+
new_dir_path = tmp_path / "copy_tests_data_dir"
38+
new_dir_path.mkdir(exist_ok=True, parents=True)
39+
40+
for item in tests_data_dir.glob("*"):
41+
print(f"Copying {item} to {new_dir_path / item.name}")
42+
shutil.copy2(item, new_dir_path / item.name)
43+
44+
return new_dir_path
45+
46+
47+
def test_cli_legacy_escape(copy_tests_data_dir: Path, run_program_with_args: Callable):
48+
result = run_program_with_args(
49+
"legacy-escape", "--osparc-config-dirname", copy_tests_data_dir
50+
)
51+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
52+
# NOTE only 1 file will have a sequence that will be escaped
53+
assert (
54+
f"Escaped sequence in {copy_tests_data_dir}/docker-compose-meta.yml"
55+
in result.output.strip()
56+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from service_integration.cli._escaping import escape_dollar_brace
3+
4+
5+
@pytest.mark.parametrize(
6+
"to_escape, escaped",
7+
[
8+
("some text", "some text"),
9+
("$${escapes}", "$$$${escapes}"),
10+
("$$${preserves}", "$$${preserves}"),
11+
("$$$${preserves}", "$$$${preserves}"),
12+
("$$$$${preserves}", "$$$$${preserves}"),
13+
(
14+
"$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}",
15+
"$$$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}",
16+
),
17+
],
18+
)
19+
def test_escape_dollar_brace(to_escape: str, escaped: str):
20+
assert escape_dollar_brace(to_escape) == escaped

0 commit comments

Comments
 (0)