Skip to content

Commit

Permalink
Print spark-class command properly
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewor14 committed Aug 7, 2014
1 parent a4df3c4 commit de765c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bin/spark-class
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ fi
export CLASSPATH

if [ "$SPARK_PRINT_LAUNCH_COMMAND" == "1" ]; then
# Put quotes around system properties in case they contain spaces
# This exports the resulting list of java opts into QUOTED_JAVA_OPTS
quote_java_property "${ESCAPED_JAVA_OPTS[@]}"
echo -n "Spark Command: " 1>&2
echo "$RUNNER" -cp "$CLASSPATH" "${ESCAPED_JAVA_OPTS[@]}" "$@" 1>&2
echo "$RUNNER" -cp "$CLASSPATH" "${QUOTED_JAVA_OPTS[@]}" "$@" 1>&2
echo -e "========================================\n" 1>&2
fi

Expand Down
28 changes: 25 additions & 3 deletions bin/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
# limitations under the License.
#

# * ---------------------------------------------------- *
# | Utility functions for launching Spark applications |
# * ---------------------------------------------------- *

# Parse the value of a config from a java properties file according to the specifications in
# http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader).
# This accepts the name of the config and returns the value through JAVA_PROPERTY_VALUE.
# This currently does not support multi-line configs.
parse_java_property() {
JAVA_PROPERTY_VALUE=$( \
sed "/^[#!]/ d" "conf/spark-defaults.conf" | \
Expand All @@ -30,22 +35,24 @@ parse_java_property() {
export JAVA_PROPERTY_VALUE
}

# Properly escape java options, dealing with whitespace, double quotes and backslashes
# This accepts a string, and returns the escaped list through ESCAPED_JAVA_OPTS.
# Properly escape java options, dealing with whitespace, double quotes and backslashes.
# This accepts a string and returns the escaped list through ESCAPED_JAVA_OPTS.
escape_java_options() {
ESCAPED_JAVA_OPTS=() # return value
option_buffer="" # buffer for collecting parts of an option
opened_quotes=0 # whether we are expecting a closing double quotes
for word in $1; do
contains_quote=$(echo "$word" | grep \" | grep -v \\\\\")
if [ -n "$contains_quote" ]; then
if [[ -n "$contains_quote" ]]; then
# Flip the bit
opened_quotes=$(((opened_quotes + 1) % 2))
fi
if [[ $opened_quotes == 0 ]]; then
# Remove all non-escaped quotes around the value
ESCAPED_JAVA_OPTS+=("$(echo "$option_buffer $word" | sed "s/^[[:space:]]*//" | sed "s/\([^\\]\)\"/\1/g")")
option_buffer=""
else
# We are expecting a closing double quote, so keep buffering
option_buffer="$option_buffer $word"
fi
done
Expand All @@ -57,3 +64,18 @@ escape_java_options() {
export ESCAPED_JAVA_OPTS
}

# Put double quotes around each of the given java options that is a system property.
# This accepts a list and returns the quoted list through QUOTED_JAVA_OPTS
quote_java_property() {
QUOTED_JAVA_OPTS=()
for opt in "$@"; do
is_system_property=$(echo "$opt" | grep -e "^-D")
if [[ -n "$is_system_property" ]]; then
QUOTED_JAVA_OPTS+=(\"$opt\")
else
QUOTED_JAVA_OPTS+=("$opt")
fi
done
export QUOTED_JAVA_OPTS
}

0 comments on commit de765c9

Please sign in to comment.