forked from ThinkBigAnalytics/scalding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun
executable file
·132 lines (115 loc) · 4.45 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env scala
// ------------------------------------------
// run - a simple scala driver for the Scalding workshop.
// usage (*nix): run scaldingscript.scala [options]
// usage (Windows): scala run scaldingscript.scala [options]
//
// Run verifies that you passed the name of a Scalding script (a Scala source file)
// as an argument, compiles it, and invokes Scalding in "local" mode.
// Scalding comes with a more sophisticated driver script called "scald.rb".
// For example, scald.rb handles invoking Scalding scripts as Hadoop jobs.
// This script is simpler and avoids some issues using "scald.rb".
//
// TODO:
// 1. Does not handle invocation from another directory.
// 2. Test that our file separator logic works correctly on Windows!
// 3. The "version" and "scalaVersion" values should be detected automatically.
val version = "0.4.1"
val scalaVersion = "2.11"
// Increase (or decrease) this heap size size value if necessary.
val heapSize = "-Xmx1g"
val separator = java.io.File.separator match {
case "\\" => "\\\\"
case sep => sep
}
val pathSeparator = System.getProperty("path.separator") match {
case null => println("""WARNING: System.getProperty("path.separator") returned null. Using :""")
case sep => sep
}
val help = """
|run - a simple scala driver for the Scalding workshop.
|usage:
| (*nix): run [scalding_options] script.scala [script_options]
| (Windows): scala run [scalding_options] script.scala [script_options]
|where the "scalding_options" are any options supported by scalding itself
|and the "script_options" will be discussed for each exercise.
|""".stripMargin
val assembly = s"target${separator}scala-${scalaVersion}${separator}scalding-workshop-$version.jar"
val libs = s"target${separator}classes"
def extractPathNameScript(str: String): (String,String,String) = {
val fullPathScriptREstr = s"""(.*${separator})([^${separator}]+).scala$$"""
val fullPathScriptRE = fullPathScriptREstr.r
val scriptREstr = s"""([^${separator}]+)(.scala)?$$"""
val scriptRE = scriptREstr.r
val (path, name, script) = str match {
case fullPathScriptRE(path, name) =>
(path, name, s"${path}${name}.scala")
case scriptRE(name, _) =>
(s"scripts${separator}", name, s"scripts${separator}${name}.scala")
case _ =>
println(s"Could not parse input script name: $str")
sys.exit(1)
}
(path, name, script)
}
case class Options (
path: String = "",
name: String = "",
script: String = "",
scalaVersion: String = "2.11",
scaldingOptions: Vector[String] = Vector.empty,
scriptOptions: Vector[String] = Vector.empty)
def processScriptArgs(
argz: List[String],
opts: Options): Options = (argz foldLeft opts) {
(opts, arg) => opts copy (scriptOptions = opts.scriptOptions :+ arg)
}
val scriptNameRE = """.*\.sc(?:ala)?$""".r
def processScaldingArgs(
argz: List[String],
opts: Options): Options = argz match {
case Nil => opts
case head :: tail => head match {
case "-h" | "--help" | "--h" =>
println(help)
sys.exit(0)
case script @ scriptNameRE() =>
val (path, name, script) = extractPathNameScript(head)
processScriptArgs(tail, opts copy (path = path, name = name, script = script))
case _ => processScaldingArgs(tail, opts copy (scaldingOptions = opts.scaldingOptions :+ head))
}
}
val options = processScaldingArgs(args.toList, Options())
if (options.script == "") {
println("Must specify a Scalding script!")
println(help)
sys.exit(1)
}
val script = options.script
val scaldingOpts = options.scaldingOptions.mkString(" ")
val scriptOpts = options.scriptOptions.mkString(" ")
import scala.language.postfixOps
import scala.sys.process._
def mkdir(path: String): Unit = {
val dir = new java.io.File(path)
if (dir.exists == false) {
if (dir.mkdir == false) {
println(s"Could not create a required directory: $dir")
sys.exit(1)
}
}
}
mkdir("classes")
def command(step: String, cmdString: String)(cmd: => Int):Int = {
println(s"-- $step: $script")
val status = cmd
if (status != 0) {
println(s"$step Failed for script $script ($cmdString)")
sys.exit(1)
}
status
}
val compilecmd = s"scalac -deprecation -cp $assembly${pathSeparator}$libs -d classes $script"
val runcmd = s"java $heapSize -cp classes${pathSeparator}$assembly${pathSeparator}$libs com.twitter.scalding.Tool $scaldingOpts ${options.name} --local $scriptOpts"
command("Compile", compilecmd)(compilecmd !)
command("Run", runcmd )(runcmd !)