From d8fe51ff8fe729b539e9f3caf0d286b2336902fc Mon Sep 17 00:00:00 2001 From: Mykhailo Androsiuk Date: Thu, 19 Oct 2023 12:37:52 +0300 Subject: [PATCH] builders: Added escape of 'env' parameter There is an issue with passing the "EXT_MODULES" parameter from the configuration moulin yaml file to the build.sh context. The 'android_kernel' builder didn't escape double quotes, while wrapping the 'env' parameter in additional double quotes like this: bash -c "...env..." As a result, an incorrect syntax was generated in the build.ninja file. To generate a functional build.ninja file, it was necessary to take this aspect into account in the moulin.yaml file and add additional escaping there. This approach is ambiguous since it requires the user to know the implementation details of each moulin builder. To address this issue, a simple escape function was added to all builders that support the 'env' parameter. It escapes double quotes to ensure proper handling of the 'env' variable. The issue has been resolved for the following builders: - android_kernel builder - android builder - zephyr builder Signed-off-by: Mykhailo Androsiuk --- moulin/builders/android.py | 4 ++++ moulin/builders/android_kernel.py | 4 ++++ moulin/builders/zephyr.py | 3 +++ moulin/utils.py | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/moulin/builders/android.py b/moulin/builders/android.py index 7a133cd..668e526 100644 --- a/moulin/builders/android.py +++ b/moulin/builders/android.py @@ -8,6 +8,7 @@ from typing import List from moulin.yaml_wrapper import YamlValue from moulin import ninja_syntax +from moulin import utils def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str], @@ -57,6 +58,9 @@ def gen_build(self): else: env_values = [] env = " ".join(env_values) + + env = utils.escape(env) + variables = { "build_dir": self.build_dir, "env": env, diff --git a/moulin/builders/android_kernel.py b/moulin/builders/android_kernel.py index fdcd47b..906d72f 100644 --- a/moulin/builders/android_kernel.py +++ b/moulin/builders/android_kernel.py @@ -8,6 +8,7 @@ from typing import List from moulin.yaml_wrapper import YamlValue from moulin import ninja_syntax +from moulin import utils def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str], @@ -55,6 +56,9 @@ def gen_build(self): else: env_values = [] env = " ".join(env_values) + + env = utils.escape(env) + variables = { "build_dir": self.build_dir, "env": env, diff --git a/moulin/builders/zephyr.py b/moulin/builders/zephyr.py index 9c2bf83..9d97585 100644 --- a/moulin/builders/zephyr.py +++ b/moulin/builders/zephyr.py @@ -9,6 +9,7 @@ from moulin.yaml_wrapper import YamlValue from moulin import ninja_syntax from moulin.utils import construct_fetcher_dep_cmd +from moulin import utils def get_builder(conf: YamlValue, name: str, build_dir: str, src_stamps: List[str], @@ -62,6 +63,8 @@ def gen_build(self): env_values = [] env = " ".join(env_values) + env = utils.escape(env) + shields_node = self.conf.get("shields", None) if shields_node: shields_vals = [x.as_str for x in shields_node] diff --git a/moulin/utils.py b/moulin/utils.py index 2a68574..189fe92 100644 --- a/moulin/utils.py +++ b/moulin/utils.py @@ -4,6 +4,7 @@ import os.path import sys +import re def create_stamp_name(*args): @@ -18,3 +19,23 @@ def construct_fetcher_dep_cmd() -> str: this_script = os.path.abspath(sys.argv[0]) args = " ".join(sys.argv[1:]) return f"{this_script} {args} --fetcherdep $name" + + +def escape(val: str) -> str: + """ + Escape special characters in the input string. + + This function takes an input string `val` and escapes special characters by adding escape + sequences to them. The following transformations are applied: + - Double quotes (") are escaped as \\". + - Dollar signs ($) are escaped as $$. + Args: + val (str): The input string to be escaped. + Returns: + str: The escaped string. + """ + result = val + result = result.replace(r"\"", r"\\\"") + result = result.replace("$", "$$") + result = re.sub('(([^\\\\])"|^()")', '\\2\\"', result) + return result