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