author | wenzelm |
Mon, 07 Mar 2016 18:20:22 +0100 | |
changeset 62545 | 8ebffdaf2ce2 |
parent 62543 | 57f379ef662f |
child 62558 | c46418f12ee1 |
permissions | -rw-r--r-- |
60991 | 1 |
/* Title: Pure/Concurrent/bash.scala |
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, |
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
|
30 |
cwd: JFile = null, env: Map[String, String] = null, redirect: Boolean = false): Process = |
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
|
31 |
new Process(script, cwd, env, redirect) |
60991 | 32 |
|
33 |
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
|
34 |
script: String, cwd: JFile, env: Map[String, String], redirect: Boolean) |
60991 | 35 |
extends Prover.System_Process |
36 |
{ |
|
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
|
37 |
val script_file = Isabelle_System.tmp_file("bash_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
|
38 |
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
|
39 |
|
62296 | 40 |
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
|
41 |
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
|
42 |
File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
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 |
"bash", File.standard_path(script_file)) |
60991 | 44 |
|
45 |
||
46 |
// channels |
|
47 |
||
48 |
val stdin: BufferedWriter = |
|
49 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
50 |
||
51 |
val stdout: BufferedReader = |
|
52 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
53 |
||
54 |
val stderr: BufferedReader = |
|
55 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
56 |
||
57 |
||
58 |
// signals |
|
59 |
||
60 |
private val pid = stdout.readLine |
|
61 |
||
62 |
private def kill(signal: String): Boolean = |
|
61025 | 63 |
Exn.Interrupt.postpone { |
64 |
Isabelle_System.kill(signal, pid) |
|
65 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
60991 | 66 |
|
67 |
private def multi_kill(signal: String): Boolean = |
|
68 |
{ |
|
69 |
var running = true |
|
70 |
var count = 10 |
|
71 |
while (running && count > 0) { |
|
72 |
if (kill(signal)) { |
|
73 |
Exn.Interrupt.postpone { |
|
74 |
Thread.sleep(100) |
|
75 |
count -= 1 |
|
76 |
} |
|
77 |
} |
|
78 |
else running = false |
|
79 |
} |
|
80 |
running |
|
81 |
} |
|
82 |
||
83 |
def interrupt() { multi_kill("INT") } |
|
84 |
def terminate() { multi_kill("INT") && multi_kill("TERM") && kill("KILL"); proc.destroy } |
|
85 |
||
86 |
||
87 |
// JVM shutdown hook |
|
88 |
||
89 |
private val shutdown_hook = new Thread { override def run = terminate() } |
|
90 |
||
91 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
92 |
catch { case _: IllegalStateException => } |
|
93 |
||
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
|
94 |
|
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
|
95 |
/* join */ |
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
|
96 |
|
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
|
97 |
def join: Int = |
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
|
98 |
{ |
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
|
99 |
val rc = proc.waitFor |
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
|
100 |
|
60991 | 101 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
102 |
catch { case _: IllegalStateException => } |
|
103 |
||
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
|
104 |
script_file.delete |
60991 | 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 |
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
|
107 |
} |
62543 | 108 |
|
109 |
||
110 |
/* result */ |
|
111 |
||
112 |
def result( |
|
113 |
progress_stdout: String => Unit = (_: String) => (), |
|
114 |
progress_stderr: String => Unit = (_: String) => (), |
|
115 |
progress_limit: Option[Long] = None, |
|
116 |
strict: Boolean = true): Process_Result = |
|
117 |
{ |
|
118 |
stdin.close |
|
119 |
||
120 |
val limited = new Limited_Progress(this, progress_limit) |
|
121 |
val out_lines = |
|
122 |
Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
123 |
val err_lines = |
|
124 |
Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
125 |
||
126 |
val rc = |
|
127 |
try { join } |
|
128 |
catch { case Exn.Interrupt() => terminate; Exn.Interrupt.return_code } |
|
129 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
|
130 |
||
131 |
Process_Result(rc, out_lines.join, err_lines.join) |
|
132 |
} |
|
60991 | 133 |
} |
134 |
} |