author | wenzelm |
Sat, 12 Mar 2016 21:46:31 +0100 | |
changeset 62602 | 96e679f042ec |
parent 62584 | 6cd36a0d2a28 |
child 62604 | 7f325faed9f7 |
permissions | -rw-r--r-- |
62584 | 1 |
/* Title: Pure/System/bash.scala |
60991 | 2 |
Author: Makarius |
3 |
||
4 |
GNU bash processes, with propagation of interrupts. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.io.{File => JFile, BufferedReader, InputStreamReader, |
|
11 |
BufferedWriter, OutputStreamWriter} |
|
12 |
||
13 |
||
14 |
object Bash |
|
15 |
{ |
|
62543 | 16 |
private class Limited_Progress(proc: Process, progress_limit: Option[Long]) |
17 |
{ |
|
18 |
private var count = 0L |
|
19 |
def apply(progress: String => Unit)(line: String): Unit = synchronized { |
|
20 |
progress(line) |
|
21 |
count = count + line.length + 1 |
|
22 |
progress_limit match { |
|
23 |
case Some(limit) if count > limit => proc.terminate |
|
24 |
case _ => |
|
25 |
} |
|
26 |
} |
|
27 |
} |
|
28 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
29 |
def process(script: String, |
62573 | 30 |
cwd: JFile = null, |
31 |
env: Map[String, String] = Map.empty, |
|
62602 | 32 |
redirect: Boolean = false, |
33 |
cleanup: () => Unit = () => ()): Process = |
|
34 |
new Process(script, cwd, env, redirect, cleanup) |
|
60991 | 35 |
|
36 |
class Process private [Bash]( |
|
62602 | 37 |
script: String, |
38 |
cwd: JFile, |
|
39 |
env: Map[String, String], |
|
40 |
redirect: Boolean, |
|
41 |
cleanup: () => Unit) |
|
60991 | 42 |
extends Prover.System_Process |
43 |
{ |
|
62569 | 44 |
private val timing_file = Isabelle_System.tmp_file("bash_script") |
45 |
private val timing = Synchronized[Option[Timing]](None) |
|
46 |
||
47 |
private val script_file = Isabelle_System.tmp_file("bash_script") |
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
48 |
File.write(script_file, script) |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
49 |
|
62296 | 50 |
private val proc = |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
51 |
Isabelle_System.process(cwd, Isabelle_System.settings(env), redirect, |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
52 |
File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
62569 | 53 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)) |
60991 | 54 |
|
55 |
||
56 |
// channels |
|
57 |
||
58 |
val stdin: BufferedWriter = |
|
59 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
60 |
||
61 |
val stdout: BufferedReader = |
|
62 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
63 |
||
64 |
val stderr: BufferedReader = |
|
65 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
66 |
||
67 |
||
68 |
// signals |
|
69 |
||
70 |
private val pid = stdout.readLine |
|
71 |
||
62558
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
72 |
def interrupt() |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
73 |
{ Exn.Interrupt.postpone { Isabelle_System.kill("INT", pid) } } |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
74 |
|
60991 | 75 |
private def kill(signal: String): Boolean = |
61025 | 76 |
Exn.Interrupt.postpone { |
77 |
Isabelle_System.kill(signal, pid) |
|
78 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
60991 | 79 |
|
80 |
private def multi_kill(signal: String): Boolean = |
|
81 |
{ |
|
82 |
var running = true |
|
83 |
var count = 10 |
|
84 |
while (running && count > 0) { |
|
85 |
if (kill(signal)) { |
|
86 |
Exn.Interrupt.postpone { |
|
87 |
Thread.sleep(100) |
|
88 |
count -= 1 |
|
89 |
} |
|
90 |
} |
|
91 |
else running = false |
|
92 |
} |
|
93 |
running |
|
94 |
} |
|
95 |
||
62574 | 96 |
def terminate() |
97 |
{ |
|
98 |
multi_kill("INT") && multi_kill("TERM") && kill("KILL") |
|
99 |
proc.destroy |
|
62602 | 100 |
do_cleanup() |
62574 | 101 |
} |
60991 | 102 |
|
103 |
||
104 |
// JVM shutdown hook |
|
105 |
||
106 |
private val shutdown_hook = new Thread { override def run = terminate() } |
|
107 |
||
108 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
109 |
catch { case _: IllegalStateException => } |
|
110 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
111 |
|
62574 | 112 |
// cleanup |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
113 |
|
62602 | 114 |
private def do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
115 |
{ |
60991 | 116 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
117 |
catch { case _: IllegalStateException => } |
|
118 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
119 |
script_file.delete |
60991 | 120 |
|
62569 | 121 |
timing.change { |
122 |
case None => |
|
62574 | 123 |
if (timing_file.isFile) { |
124 |
val t = |
|
125 |
Word.explode(File.read(timing_file)) match { |
|
126 |
case List(Properties.Value.Long(elapsed), Properties.Value.Long(cpu)) => |
|
127 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
|
128 |
case _ => Timing.zero |
|
129 |
} |
|
130 |
timing_file.delete |
|
131 |
Some(t) |
|
132 |
} |
|
133 |
else Some(Timing.zero) |
|
62569 | 134 |
case some => some |
135 |
} |
|
62602 | 136 |
|
137 |
cleanup() |
|
62574 | 138 |
} |
62569 | 139 |
|
62574 | 140 |
|
141 |
// join |
|
142 |
||
143 |
def join: Int = |
|
144 |
{ |
|
145 |
val rc = proc.waitFor |
|
62602 | 146 |
do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
147 |
rc |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
148 |
} |
62543 | 149 |
|
150 |
||
62574 | 151 |
// result |
62543 | 152 |
|
153 |
def result( |
|
154 |
progress_stdout: String => Unit = (_: String) => (), |
|
155 |
progress_stderr: String => Unit = (_: String) => (), |
|
156 |
progress_limit: Option[Long] = None, |
|
157 |
strict: Boolean = true): Process_Result = |
|
158 |
{ |
|
159 |
stdin.close |
|
160 |
||
161 |
val limited = new Limited_Progress(this, progress_limit) |
|
162 |
val out_lines = |
|
163 |
Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
164 |
val err_lines = |
|
165 |
Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
166 |
||
167 |
val rc = |
|
168 |
try { join } |
|
62574 | 169 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
62543 | 170 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
171 |
||
62574 | 172 |
Process_Result(rc, out_lines.join, err_lines.join, false, timing.value getOrElse Timing.zero) |
62543 | 173 |
} |
60991 | 174 |
} |
175 |
} |