Skip to content

Commit c7ba6a7

Browse files
committed
[SPARK-6568] spark-shell.cmd --jars option does not accept the jar that has space in its path
escape spaces in the arguments.
1 parent 1b2aab8 commit c7ba6a7

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

core/src/main/scala/org/apache/spark/deploy/PythonRunner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ object PythonRunner {
8282
s"spark-submit is currently only supported for local files: $path")
8383
}
8484
val windows = Utils.isWindows || testWindows
85-
var formattedPath = if (windows) Utils.formatWindowsPath(path) else path
85+
var formattedPath = Utils.formatPath(path, windows)
8686

8787
// Strip the URI scheme from the path
8888
formattedPath =

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,9 +1659,14 @@ private[spark] object Utils extends Logging {
16591659
val windowsDrive = "([a-zA-Z])".r
16601660

16611661
/**
1662-
* Format a Windows path such that it can be safely passed to a URI.
1662+
* Format a path such that it can be safely passed to a URI.
16631663
*/
1664-
def formatWindowsPath(path: String): String = path.replace("\\", "/")
1664+
def formatPath(path: String, windows: Boolean): String = {
1665+
val formatted = path.replace(" ", "%20")
1666+
1667+
// In Windows, the file separator is a backslash, but this is inconsistent with the URI format
1668+
if (windows) formatted.replace("\\", "/") else formatted
1669+
}
16651670

16661671
/**
16671672
* Indicates whether Spark is currently running unit tests.
@@ -1762,9 +1767,8 @@ private[spark] object Utils extends Logging {
17621767
*/
17631768
def resolveURI(path: String, testWindows: Boolean = false): URI = {
17641769

1765-
// In Windows, the file separator is a backslash, but this is inconsistent with the URI format
17661770
val windows = isWindows || testWindows
1767-
val formattedPath = if (windows) formatWindowsPath(path) else path
1771+
val formattedPath = formatPath(path, windows)
17681772

17691773
val uri = new URI(formattedPath)
17701774
if (uri.getPath == null) {
@@ -1801,7 +1805,7 @@ private[spark] object Utils extends Logging {
18011805
Array.empty
18021806
} else {
18031807
paths.split(",").filter { p =>
1804-
val formattedPath = if (windows) formatWindowsPath(p) else p
1808+
val formattedPath = formatPath(p, windows)
18051809
val uri = new URI(formattedPath)
18061810
Option(uri.getScheme).getOrElse("file") match {
18071811
case windowsDrive(d) if windows => false

core/src/test/scala/org/apache/spark/util/UtilsSuite.scala

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import java.util.Locale
2828
import com.google.common.base.Charsets.UTF_8
2929
import com.google.common.io.Files
3030
import org.scalatest.FunSuite
31+
import org.apache.commons.lang3.SystemUtils
3132

3233
import org.apache.hadoop.conf.Configuration
3334
import org.apache.hadoop.fs.Path
@@ -233,13 +234,16 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
233234
assert(new URI(Utils.resolveURIs(before, testWindows)) === new URI(after))
234235
assert(new URI(Utils.resolveURIs(after, testWindows)) === new URI(after))
235236
}
236-
val cwd = System.getProperty("user.dir")
237+
val rawCwd = System.getProperty("user.dir")
238+
val cwd = if (SystemUtils.IS_OS_WINDOWS) s"/$rawCwd".replace("\\", "/") else rawCwd
237239
assertResolves("hdfs:/root/spark.jar", "hdfs:/root/spark.jar")
238240
assertResolves("hdfs:///root/spark.jar#app.jar", "hdfs:/root/spark.jar#app.jar")
239241
assertResolves("spark.jar", s"file:$cwd/spark.jar")
240242
assertResolves("spark.jar#app.jar", s"file:$cwd/spark.jar#app.jar")
243+
assertResolves("path to/file.txt", s"file:$cwd/path%20to/file.txt")
241244
assertResolves("C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
242245
assertResolves("C:\\path\\to\\file.txt", "file:/C:/path/to/file.txt", testWindows = true)
246+
assertResolves("C:/path to/file.txt", "file:/C:/path%20to/file.txt", testWindows = true)
243247
assertResolves("file:/C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
244248
assertResolves("file:///C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
245249
assertResolves("file:/C:/file.txt#alias.txt", "file:/C:/file.txt#alias.txt", testWindows = true)
@@ -258,14 +262,16 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
258262
assert(resolve(resolve(after)) === after)
259263
assert(resolve(resolve(resolve(after))) === after)
260264
}
261-
val cwd = System.getProperty("user.dir")
265+
val rawCwd = System.getProperty("user.dir")
266+
val cwd = if (SystemUtils.IS_OS_WINDOWS) s"/$rawCwd".replace("\\", "/") else rawCwd
262267
assertResolves("jar1,jar2", s"file:$cwd/jar1,file:$cwd/jar2")
263268
assertResolves("file:/jar1,file:/jar2", "file:/jar1,file:/jar2")
264269
assertResolves("hdfs:/jar1,file:/jar2,jar3", s"hdfs:/jar1,file:/jar2,file:$cwd/jar3")
265-
assertResolves("hdfs:/jar1,file:/jar2,jar3,jar4#jar5",
266-
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:$cwd/jar4#jar5")
267-
assertResolves("hdfs:/jar1,file:/jar2,jar3,C:\\pi.py#py.pi",
268-
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py#py.pi", testWindows = true)
270+
assertResolves("hdfs:/jar1,file:/jar2,jar3,jar4#jar5,path to/jar6",
271+
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:$cwd/jar4#jar5,file:$cwd/path%20to/jar6")
272+
assertResolves("""hdfs:/jar1,file:/jar2,jar3,C:\pi.py#py.pi,C:\path to\jar4""",
273+
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py#py.pi,file:/C:/path%20to/jar4",
274+
testWindows = true)
269275
}
270276

271277
test("nonLocalPaths") {
@@ -280,6 +286,8 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
280286
assert(Utils.nonLocalPaths("local:/spark.jar,file:/smart.jar,family.py") === Array.empty)
281287
assert(Utils.nonLocalPaths("hdfs:/spark.jar,s3:/smart.jar") ===
282288
Array("hdfs:/spark.jar", "s3:/smart.jar"))
289+
assert(Utils.nonLocalPaths("hdfs:/path to/spark.jar,path to/a.jar,s3:/path to/smart.jar") ===
290+
Array("hdfs:/path to/spark.jar", "s3:/path to/smart.jar"))
283291
assert(Utils.nonLocalPaths("hdfs:/spark.jar,s3:/smart.jar,local.py,file:/hello/pi.py") ===
284292
Array("hdfs:/spark.jar", "s3:/smart.jar"))
285293
assert(Utils.nonLocalPaths("local.py,hdfs:/spark.jar,file:/hello/pi.py,s3:/smart.jar") ===
@@ -293,6 +301,11 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
293301
assert(Utils.nonLocalPaths("local:///C:/some/path.jar", testWindows = true) === Array.empty)
294302
assert(Utils.nonLocalPaths("hdfs:/a.jar,C:/my.jar,s3:/another.jar", testWindows = true) ===
295303
Array("hdfs:/a.jar", "s3:/another.jar"))
304+
assert(Utils.nonLocalPaths(
305+
"hdfs:/path to/spark.jar,C:\\path to\\a.jar,s3:/path to/smart.jar"
306+
, testWindows = true
307+
) ===
308+
Array("hdfs:/path to/spark.jar", "s3:/path to/smart.jar"))
296309
assert(Utils.nonLocalPaths("D:/your.jar,hdfs:/a.jar,s3:/another.jar", testWindows = true) ===
297310
Array("hdfs:/a.jar", "s3:/another.jar"))
298311
assert(Utils.nonLocalPaths("hdfs:/a.jar,s3:/another.jar,e:/our.jar", testWindows = true) ===
@@ -392,7 +405,12 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
392405
val targetDir = new File(tempDir, "target-dir")
393406
Files.write("some text", sourceFile, UTF_8)
394407

395-
val path = new Path("file://" + sourceDir.getAbsolutePath)
408+
val path =
409+
if (SystemUtils.IS_OS_WINDOWS) {
410+
new Path("file:/" + sourceDir.getAbsolutePath.replace("\\", "/"))
411+
} else {
412+
new Path("file://" + sourceDir.getAbsolutePath)
413+
}
396414
val conf = new Configuration()
397415
val fs = Utils.getHadoopFileSystem(path.toString, conf)
398416

@@ -412,7 +430,12 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
412430
val destInnerFile = new File(destInnerDir, sourceFile.getName)
413431
assert(destInnerFile.isFile())
414432

415-
val filePath = new Path("file://" + sourceFile.getAbsolutePath)
433+
val filePath =
434+
if (SystemUtils.IS_OS_WINDOWS) {
435+
new Path("file:/" + sourceFile.getAbsolutePath.replace("\\", "/"))
436+
} else {
437+
new Path("file://" + sourceFile.getAbsolutePath)
438+
}
416439
val testFileDir = new File(tempDir, "test-filename")
417440
val testFileName = "testFName"
418441
val testFilefs = Utils.getHadoopFileSystem(filePath.toString, conf)

0 commit comments

Comments
 (0)