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