def sub_and_quote()

in scripts/local_cloudbuild.py [0:0]


def sub_and_quote(s, substitutions, substitutions_used):
    """Return a shell-escaped, variable substituted, version of the string s.

    Args:
        s (str): Any string
        subs (dict): Substitution map to apply
        subs_used (set): Updated with names from `subs.keys()` when those
                         substitutions are encountered in `s`
    """

    def sub(match):
        """Perform a single substitution."""
        variable_name = match.group(1)
        if variable_name[0] == '{':
            # Strip curly brackets
            variable_name = variable_name[1:-1]
        if variable_name == '$':
            value = '$'
        elif variable_name not in substitutions:
            # Variables must be set
            raise ValueError(
                'Variable "{}" used without being defined.  Try adding '
                'it to the --substitutions flag'.format(variable_name))
        else:
            value = substitutions.get(variable_name)
        substitutions_used.add(variable_name)
        return value

    substituted_s = re.sub(SUBSTITUTION_REGEX, sub, s)
    quoted_s = shlex.quote(substituted_s)
    return quoted_s