Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit e03f289

Browse files
committed
removed testWindows from Utils.resolveURI and Utils.resolveURIs.
replaced SystemUtils.IS_OS_WINDOWS to Utils.isWindows. removed Utils.formatPath from PythonRunner.scala.
1 parent 84c33d0 commit e03f289

File tree

4 files changed

+51
-48
lines changed

4 files changed

+51
-48
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.spark.deploy
1919

2020
import java.net.URI
21+
import java.io.File
22+
import scala.util.Try
2123

2224
import scala.collection.mutable.ArrayBuffer
2325
import scala.collection.JavaConversions._
@@ -81,16 +83,13 @@ object PythonRunner {
8183
throw new IllegalArgumentException("Launching Python applications through " +
8284
s"spark-submit is currently only supported for local files: $path")
8385
}
84-
val windows = Utils.isWindows || testWindows
85-
var formattedPath = Utils.formatPath(path, windows)
86-
87-
// Strip the URI scheme from the path
88-
formattedPath =
89-
new URI(formattedPath).getScheme match {
90-
case null => formattedPath
91-
case Utils.windowsDrive(d) if windows => formattedPath
92-
case _ => new URI(formattedPath).getPath
93-
}
86+
// get path when scheme is file.
87+
val uri = Try(new URI(path)).getOrElse(new File(path).toURI)
88+
var formattedPath = uri.getScheme match {
89+
case null => path
90+
case "file" | "local" => uri.getPath
91+
case _ => null
92+
}
9493

9594
// Guard against malformed paths potentially throwing NPE
9695
if (formattedPath == null) {
@@ -99,7 +98,9 @@ object PythonRunner {
9998

10099
// In Windows, the drive should not be prefixed with "/"
101100
// For instance, python does not understand "/C:/path/to/sheep.py"
102-
formattedPath = if (windows) formattedPath.stripPrefix("/") else formattedPath
101+
if (formattedPath.matches("/[a-zA-Z]:/.*")) {
102+
formattedPath = formattedPath.stripPrefix("/")
103+
}
103104
formattedPath
104105
}
105106

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ private[spark] object Utils extends Logging {
17651765
* If the supplied path does not contain a scheme, or is a relative path, it will be
17661766
* converted into an absolute path with a file:// scheme.
17671767
*/
1768-
def resolveURI(path: String, testWindows: Boolean = false): URI = {
1768+
def resolveURI(path: String): URI = {
17691769
try {
17701770
val uri = new URI(path)
17711771
if (uri.getScheme() != null) {
@@ -1778,11 +1778,11 @@ private[spark] object Utils extends Logging {
17781778
}
17791779

17801780
/** Resolve a comma-separated list of paths. */
1781-
def resolveURIs(paths: String, testWindows: Boolean = false): String = {
1781+
def resolveURIs(paths: String): String = {
17821782
if (paths == null || paths.trim.isEmpty) {
17831783
""
17841784
} else {
1785-
paths.split(",").map { p => Utils.resolveURI(p, testWindows) }.mkString(",")
1785+
paths.split(",").map { p => Utils.resolveURI(p) }.mkString(",")
17861786
}
17871787
}
17881788

@@ -1793,7 +1793,7 @@ private[spark] object Utils extends Logging {
17931793
Array.empty
17941794
} else {
17951795
paths.split(",").filter { p =>
1796-
val uri = resolveURI(p, windows)
1796+
val uri = resolveURI(p)
17971797
Option(uri.getScheme).getOrElse("file") match {
17981798
case windowsDrive(d) if windows => false
17991799
case "local" | "file" => false

core/src/test/scala/org/apache/spark/deploy/PythonRunnerSuite.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.deploy
1919

2020
import org.scalatest.FunSuite
21+
import org.apache.spark.util.Utils
2122

2223
class PythonRunnerSuite extends FunSuite {
2324

@@ -28,10 +29,13 @@ class PythonRunnerSuite extends FunSuite {
2829
assert(PythonRunner.formatPath("file:///spark.py") === "/spark.py")
2930
assert(PythonRunner.formatPath("local:/spark.py") === "/spark.py")
3031
assert(PythonRunner.formatPath("local:///spark.py") === "/spark.py")
31-
assert(PythonRunner.formatPath("C:/a/b/spark.py", testWindows = true) === "C:/a/b/spark.py")
32-
assert(PythonRunner.formatPath("/C:/a/b/spark.py", testWindows = true) === "C:/a/b/spark.py")
3332
assert(PythonRunner.formatPath("file:/C:/a/b/spark.py", testWindows = true) ===
3433
"C:/a/b/spark.py")
34+
if (Utils.isWindows) {
35+
assert(PythonRunner.formatPath("C:\\a\\b\\spark.py", testWindows = true) === "C:/a/b/spark.py")
36+
assert(PythonRunner.formatPath("C:\\a b\\spark.py", testWindows = true) ===
37+
"C:/a b/spark.py")
38+
}
3539
intercept[IllegalArgumentException] { PythonRunner.formatPath("one:two") }
3640
intercept[IllegalArgumentException] { PythonRunner.formatPath("hdfs:s3:xtremeFS") }
3741
intercept[IllegalArgumentException] { PythonRunner.formatPath("hdfs:/path/to/some.py") }
@@ -45,14 +49,14 @@ class PythonRunnerSuite extends FunSuite {
4549
Array("/app.py", "/spark.py"))
4650
assert(PythonRunner.formatPaths("me.py,file:/you.py,local:/we.py") ===
4751
Array("me.py", "/you.py", "/we.py"))
48-
assert(PythonRunner.formatPaths("C:/a/b/spark.py", testWindows = true) ===
49-
Array("C:/a/b/spark.py"))
50-
assert(PythonRunner.formatPaths("/C:/a/b/spark.py", testWindows = true) ===
51-
Array("C:/a/b/spark.py"))
52-
assert(PythonRunner.formatPaths("C:/free.py,pie.py", testWindows = true) ===
53-
Array("C:/free.py", "pie.py"))
54-
assert(PythonRunner.formatPaths("lovely.py,C:/free.py,file:/d:/fry.py", testWindows = true) ===
55-
Array("lovely.py", "C:/free.py", "d:/fry.py"))
52+
if (Utils.isWindows) {
53+
assert(PythonRunner.formatPaths("C:\\a\\b\\spark.py", testWindows = true) ===
54+
Array("C:/a/b/spark.py"))
55+
assert(PythonRunner.formatPaths("C:\\free.py,pie.py", testWindows = true) ===
56+
Array("C:/free.py", "pie.py"))
57+
assert(PythonRunner.formatPaths("lovely.py,C:\\free.py,file:/d:/fry.py", testWindows = true) ===
58+
Array("lovely.py", "C:/free.py", "d:/fry.py"))
59+
}
5660
intercept[IllegalArgumentException] { PythonRunner.formatPaths("one:two,three") }
5761
intercept[IllegalArgumentException] { PythonRunner.formatPaths("two,three,four:five:six") }
5862
intercept[IllegalArgumentException] { PythonRunner.formatPaths("hdfs:/some.py,foo.py") }

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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
3231

3332
import org.apache.hadoop.conf.Configuration
3433
import org.apache.hadoop.fs.Path
@@ -222,58 +221,57 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
222221
}
223222

224223
test("resolveURI") {
225-
def assertResolves(before: String, after: String, testWindows: Boolean = false): Unit = {
224+
def assertResolves(before: String, after: String): Unit = {
226225
// This should test only single paths
227226
assume(before.split(",").length === 1)
228227
// Repeated invocations of resolveURI should yield the same result
229-
def resolve(uri: String): String = Utils.resolveURI(uri, testWindows).toString
228+
def resolve(uri: String): String = Utils.resolveURI(uri).toString
230229
assert(resolve(after) === after)
231230
assert(resolve(resolve(after)) === after)
232231
assert(resolve(resolve(resolve(after))) === after)
233232
// Also test resolveURIs with single paths
234-
assert(new URI(Utils.resolveURIs(before, testWindows)) === new URI(after))
235-
assert(new URI(Utils.resolveURIs(after, testWindows)) === new URI(after))
233+
assert(new URI(Utils.resolveURIs(before)) === new URI(after))
234+
assert(new URI(Utils.resolveURIs(after)) === new URI(after))
236235
}
237236
val rawCwd = System.getProperty("user.dir")
238-
val cwd = if (SystemUtils.IS_OS_WINDOWS) s"/$rawCwd".replace("\\", "/") else rawCwd
237+
val cwd = if (Utils.isWindows) s"/$rawCwd".replace("\\", "/") else rawCwd
239238
assertResolves("hdfs:/root/spark.jar", "hdfs:/root/spark.jar")
240239
assertResolves("hdfs:///root/spark.jar#app.jar", "hdfs:/root/spark.jar#app.jar")
241240
assertResolves("spark.jar", s"file:$cwd/spark.jar")
242241
assertResolves("spark.jar#app.jar", s"file:$cwd/spark.jar%23app.jar")
243242
assertResolves("path to/file.txt", s"file:$cwd/path%20to/file.txt")
244-
if (SystemUtils.IS_OS_WINDOWS) {
245-
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)
243+
if (Utils.isWindows) {
244+
assertResolves("C:\\path\\to\\file.txt", "file:/C:/path/to/file.txt")
245+
assertResolves("C:\\path to\\file.txt", "file:/C:/path%20to/file.txt")
247246
}
248-
assertResolves("file:/C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
249-
assertResolves("file:///C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
250-
assertResolves("file:/C:/file.txt#alias.txt", "file:/C:/file.txt#alias.txt", testWindows = true)
247+
assertResolves("file:/C:/path/to/file.txt", "file:/C:/path/to/file.txt")
248+
assertResolves("file:///C:/path/to/file.txt", "file:/C:/path/to/file.txt")
249+
assertResolves("file:/C:/file.txt#alias.txt", "file:/C:/file.txt#alias.txt")
251250
assertResolves("file:foo", s"file:foo")
252251
assertResolves("file:foo:baby", s"file:foo:baby")
253252
}
254253

255254
test("resolveURIs with multiple paths") {
256-
def assertResolves(before: String, after: String, testWindows: Boolean = false): Unit = {
255+
def assertResolves(before: String, after: String): Unit = {
257256
assume(before.split(",").length > 1)
258-
assert(Utils.resolveURIs(before, testWindows) === after)
259-
assert(Utils.resolveURIs(after, testWindows) === after)
257+
assert(Utils.resolveURIs(before) === after)
258+
assert(Utils.resolveURIs(after) === after)
260259
// Repeated invocations of resolveURIs should yield the same result
261-
def resolve(uri: String): String = Utils.resolveURIs(uri, testWindows)
260+
def resolve(uri: String): String = Utils.resolveURIs(uri)
262261
assert(resolve(after) === after)
263262
assert(resolve(resolve(after)) === after)
264263
assert(resolve(resolve(resolve(after))) === after)
265264
}
266265
val rawCwd = System.getProperty("user.dir")
267-
val cwd = if (SystemUtils.IS_OS_WINDOWS) s"/$rawCwd".replace("\\", "/") else rawCwd
266+
val cwd = if (Utils.isWindows) s"/$rawCwd".replace("\\", "/") else rawCwd
268267
assertResolves("jar1,jar2", s"file:$cwd/jar1,file:$cwd/jar2")
269268
assertResolves("file:/jar1,file:/jar2", "file:/jar1,file:/jar2")
270269
assertResolves("hdfs:/jar1,file:/jar2,jar3", s"hdfs:/jar1,file:/jar2,file:$cwd/jar3")
271270
assertResolves("hdfs:/jar1,file:/jar2,jar3,jar4#jar5,path to/jar6",
272271
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:$cwd/jar4%23jar5,file:$cwd/path%20to/jar6")
273-
if (SystemUtils.IS_OS_WINDOWS) {
274-
assertResolves( """hdfs:/jar1,file:/jar2,jar3,C:\pi.py#py.pi,C:\path to\jar4""",
275-
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py%23py.pi,file:/C:/path%20to/jar4",
276-
testWindows = true)
272+
if (Utils.isWindows) {
273+
assertResolves("""hdfs:/jar1,file:/jar2,jar3,C:\pi.py#py.pi,C:\path to\jar4""",
274+
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py%23py.pi,file:/C:/path%20to/jar4")
277275
}
278276
}
279277

@@ -404,7 +402,7 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
404402
Files.write("some text", sourceFile, UTF_8)
405403

406404
val path =
407-
if (SystemUtils.IS_OS_WINDOWS) {
405+
if (Utils.isWindows) {
408406
new Path("file:/" + sourceDir.getAbsolutePath.replace("\\", "/"))
409407
} else {
410408
new Path("file://" + sourceDir.getAbsolutePath)
@@ -429,7 +427,7 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
429427
assert(destInnerFile.isFile())
430428

431429
val filePath =
432-
if (SystemUtils.IS_OS_WINDOWS) {
430+
if (Utils.isWindows) {
433431
new Path("file:/" + sourceFile.getAbsolutePath.replace("\\", "/"))
434432
} else {
435433
new Path("file://" + sourceFile.getAbsolutePath)

0 commit comments

Comments
 (0)