author | wenzelm |
Thu, 10 Mar 2016 09:50:53 +0100 | |
changeset 62584 | 6cd36a0d2a28 |
parent 62574 | src/Pure/Concurrent/bash.scala@ec382bc689e5 |
child 62602 | 96e679f042ec |
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, |
|
32 |
redirect: Boolean = false): Process = |
|
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
|
33 |
new Process(script, cwd, env, redirect) |
60991 | 34 |
|
35 |
class Process private [Bash]( |
|
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
|
36 |
script: String, cwd: JFile, env: Map[String, String], redirect: Boolean) |
60991 | 37 |
extends Prover.System_Process |
38 |
{ |
|
62569 | 39 |
private val timing_file = Isabelle_System.tmp_file("bash_script") |
40 |
private val timing = Synchronized[Option[Timing]](None) |
|
41 |
||
42 |
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
|
43 |
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
|
44 |
|
62296 | 45 |
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
|
46 |
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
|
47 |
File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
62569 | 48 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)) |
60991 | 49 |
|
50 |
||
51 |
// channels |
|
52 |
||
53 |
val stdin: BufferedWriter = |
|
54 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
55 |
||
56 |
val stdout: BufferedReader = |
|
57 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
58 |
||
59 |
val stderr: BufferedReader = |
|
60 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
61 |
||
62 |
||
63 |
// signals |
|
64 |
||
65 |
private val pid = stdout.readLine |
|
66 |
||
62558
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
67 |
def interrupt() |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
68 |
{ Exn.Interrupt.postpone { Isabelle_System.kill("INT", pid) } } |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
69 |
|
60991 | 70 |
private def kill(signal: String): Boolean = |
61025 | 71 |
Exn.Interrupt.postpone { |
72 |
Isabelle_System.kill(signal, pid) |
|
73 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
60991 | 74 |
|
75 |
private def multi_kill(signal: String): Boolean = |
|
76 |
{ |
|
77 |
var running = true |
|
78 |
var count = 10 |
|
79 |
while (running && count > 0) { |
|
80 |
if (kill(signal)) { |
|
81 |
Exn.Interrupt.postpone { |
|
82 |
Thread.sleep(100) |
|
83 |
count -= 1 |
|
84 |
} |
|
85 |
} |
|
86 |
else running = false |
|
87 |
} |
|
88 |
running |
|
89 |
} |
|
90 |
||
62574 | 91 |
def terminate() |
92 |
{ |
|
93 |
multi_kill("INT") && multi_kill("TERM") && kill("KILL") |
|
94 |
proc.destroy |
|
95 |
cleanup() |
|
96 |
} |
|
60991 | 97 |
|
98 |
||
99 |
// JVM shutdown hook |
|
100 |
||
101 |
private val shutdown_hook = new Thread { override def run = terminate() } |
|
102 |
||
103 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
104 |
catch { case _: IllegalStateException => } |
|
105 |
||
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
|
106 |
|
62574 | 107 |
// 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
|
108 |
|
62574 | 109 |
private def 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
|
110 |
{ |
60991 | 111 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
112 |
catch { case _: IllegalStateException => } |
|
113 |
||
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
|
114 |
script_file.delete |
60991 | 115 |
|
62569 | 116 |
timing.change { |
117 |
case None => |
|
62574 | 118 |
if (timing_file.isFile) { |
119 |
val t = |
|
120 |
Word.explode(File.read(timing_file)) match { |
|
121 |
case List(Properties.Value.Long(elapsed), Properties.Value.Long(cpu)) => |
|
122 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
|
123 |
case _ => Timing.zero |
|
124 |
} |
|
125 |
timing_file.delete |
|
126 |
Some(t) |
|
127 |
} |
|
128 |
else Some(Timing.zero) |
|
62569 | 129 |
case some => some |
130 |
} |
|
62574 | 131 |
} |
62569 | 132 |
|
62574 | 133 |
|
134 |
// join |
|
135 |
||
136 |
def join: Int = |
|
137 |
{ |
|
138 |
val rc = proc.waitFor |
|
139 |
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
|
140 |
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
|
141 |
} |
62543 | 142 |
|
143 |
||
62574 | 144 |
// result |
62543 | 145 |
|
146 |
def result( |
|
147 |
progress_stdout: String => Unit = (_: String) => (), |
|
148 |
progress_stderr: String => Unit = (_: String) => (), |
|
149 |
progress_limit: Option[Long] = None, |
|
150 |
strict: Boolean = true): Process_Result = |
|
151 |
{ |
|
152 |
stdin.close |
|
153 |
||
154 |
val limited = new Limited_Progress(this, progress_limit) |
|
155 |
val out_lines = |
|
156 |
Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
157 |
val err_lines = |
|
158 |
Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
159 |
||
160 |
val rc = |
|
161 |
try { join } |
|
62574 | 162 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
62543 | 163 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
164 |
||
62574 | 165 |
Process_Result(rc, out_lines.join, err_lines.join, false, timing.value getOrElse Timing.zero) |
62543 | 166 |
} |
60991 | 167 |
} |
168 |
} |