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