2
0
Fork 0
mirror of https://github.com/bazelbuild/bazel-skylib synced 2024-11-27 05:43:25 +00:00
bazel-skylib/lib/shell.bzl

55 lines
1.8 KiB
Python
Raw Normal View History

2017-10-10 14:59:31 +00:00
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Skylib module containing shell utility functions."""
def _array_literal(iterable):
"""Creates a string from a sequence that can be used as a shell array.
2017-10-10 14:59:31 +00:00
For example, `shell.array_literal(["a", "b", "c"])` would return the string
`("a" "b" "c")`, which can be used in a shell script wherever an array
literal is needed.
2017-10-10 14:59:31 +00:00
Note that all elements in the array are quoted (using `shell.quote`) for
safety, even if they do not need to be.
Args:
iterable: A sequence of elements. Elements that are not strings will be
converted to strings first, by calling `str()`.
2017-10-10 14:59:31 +00:00
Returns:
A string that represents the sequence as a shell array; that is,
parentheses containing the quoted elements.
"""
return "(" + " ".join([_quote(str(i)) for i in iterable]) + ")"
2017-10-10 14:59:31 +00:00
def _quote(s):
"""Quotes the given string for use in a shell command.
2017-10-10 14:59:31 +00:00
This function quotes the given string (in case it contains spaces or other
shell metacharacters.)
Args:
s: The string to quote.
2017-10-10 14:59:31 +00:00
Returns:
A quoted version of the string that can be passed to a shell command.
"""
return "'" + s.replace("'", "'\\''") + "'"
2017-10-10 14:59:31 +00:00
shell = struct(
array_literal = _array_literal,
quote = _quote,
2017-10-10 14:59:31 +00:00
)