src/Pure/System/standard_system.scala
author wenzelm
Mon Sep 27 18:16:36 2010 +0200 (2010-09-27)
changeset 39732 4dbc72759706
parent 39709 1fa4c5c7d534
child 43516 1c4736b9396a
permissions -rw-r--r--
added Standard_System.unzip (for platform file-system);
tuned comments;
wenzelm@34201
     1
/*  Title:      Pure/System/standard_system.scala
wenzelm@34201
     2
    Author:     Makarius
wenzelm@34201
     3
wenzelm@34201
     4
Standard system operations, with basic Cygwin/Posix compatibility.
wenzelm@34201
     5
*/
wenzelm@34201
     6
wenzelm@34201
     7
package isabelle
wenzelm@34201
     8
wenzelm@39732
     9
import java.util.zip.{ZipEntry, ZipInputStream}
wenzelm@34201
    10
import java.util.regex.Pattern
wenzelm@34201
    11
import java.util.Locale
wenzelm@39705
    12
import java.net.URL
wenzelm@39705
    13
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream,
wenzelm@39578
    14
  BufferedInputStream, InputStream, FileInputStream, BufferedReader, InputStreamReader,
wenzelm@34298
    15
  File, FileFilter, IOException}
wenzelm@34201
    16
wenzelm@36011
    17
import scala.io.{Source, Codec}
wenzelm@34201
    18
import scala.util.matching.Regex
wenzelm@34201
    19
import scala.collection.mutable
wenzelm@34201
    20
wenzelm@34201
    21
wenzelm@34201
    22
object Standard_System
wenzelm@34201
    23
{
wenzelm@38264
    24
  /* UTF-8 charset */
wenzelm@38264
    25
wenzelm@34201
    26
  val charset = "UTF-8"
wenzelm@36015
    27
  def codec(): Codec = Codec(charset)
wenzelm@34201
    28
wenzelm@38264
    29
  def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
wenzelm@38264
    30
wenzelm@34201
    31
wenzelm@34201
    32
  /* permissive UTF-8 decoding */
wenzelm@34201
    33
wenzelm@34201
    34
  // see also http://en.wikipedia.org/wiki/UTF-8#Description
wenzelm@34201
    35
  // overlong encodings enable byte-stuffing
wenzelm@34201
    36
wenzelm@34201
    37
  def decode_permissive_utf8(text: CharSequence): String =
wenzelm@34201
    38
  {
wenzelm@34201
    39
    val buf = new java.lang.StringBuilder(text.length)
wenzelm@34201
    40
    var code = -1
wenzelm@34201
    41
    var rest = 0
wenzelm@34201
    42
    def flush()
wenzelm@34201
    43
    {
wenzelm@34201
    44
      if (code != -1) {
wenzelm@34201
    45
        if (rest == 0 && Character.isValidCodePoint(code))
wenzelm@34201
    46
          buf.appendCodePoint(code)
wenzelm@34201
    47
        else buf.append('\uFFFD')
wenzelm@34201
    48
        code = -1
wenzelm@34201
    49
        rest = 0
wenzelm@34201
    50
      }
wenzelm@34201
    51
    }
wenzelm@34201
    52
    def init(x: Int, n: Int)
wenzelm@34201
    53
    {
wenzelm@34201
    54
      flush()
wenzelm@34201
    55
      code = x
wenzelm@34201
    56
      rest = n
wenzelm@34201
    57
    }
wenzelm@34201
    58
    def push(x: Int)
wenzelm@34201
    59
    {
wenzelm@34201
    60
      if (rest <= 0) init(x, -1)
wenzelm@34201
    61
      else {
wenzelm@34201
    62
        code <<= 6
wenzelm@34201
    63
        code += x
wenzelm@34201
    64
        rest -= 1
wenzelm@34201
    65
      }
wenzelm@34201
    66
    }
wenzelm@34201
    67
    for (i <- 0 until text.length) {
wenzelm@34201
    68
      val c = text.charAt(i)
wenzelm@34201
    69
      if (c < 128) { flush(); buf.append(c) }
wenzelm@34201
    70
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
wenzelm@34201
    71
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
wenzelm@34201
    72
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
wenzelm@34201
    73
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
wenzelm@34201
    74
    }
wenzelm@34201
    75
    flush()
wenzelm@34201
    76
    buf.toString
wenzelm@34201
    77
  }
wenzelm@34201
    78
wenzelm@34201
    79
wenzelm@34201
    80
  /* basic file operations */
wenzelm@34201
    81
wenzelm@39578
    82
  def slurp(reader: BufferedReader): String =
wenzelm@34201
    83
  {
wenzelm@39578
    84
    val output = new StringBuilder(100)
wenzelm@39578
    85
    var c = -1
wenzelm@39578
    86
    while ({ c = reader.read; c != -1 }) output += c.toChar
wenzelm@39578
    87
    reader.close
wenzelm@39578
    88
    output.toString
wenzelm@34201
    89
  }
wenzelm@34201
    90
wenzelm@39578
    91
  def slurp(stream: InputStream): String =
wenzelm@39578
    92
    slurp(new BufferedReader(new InputStreamReader(stream, charset)))
wenzelm@39578
    93
wenzelm@39578
    94
  def read_file(file: File): String = slurp(new FileInputStream(file))
wenzelm@34201
    95
wenzelm@34201
    96
  def write_file(file: File, text: CharSequence)
wenzelm@34201
    97
  {
wenzelm@34201
    98
    val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))
wenzelm@34201
    99
    try { writer.append(text) }
wenzelm@34201
   100
    finally { writer.close }
wenzelm@34201
   101
  }
wenzelm@34201
   102
wenzelm@39578
   103
  def with_tmp_file[A](prefix: String)(body: File => A): A =
wenzelm@39578
   104
  {
wenzelm@39578
   105
    val file = File.createTempFile(prefix, null)
wenzelm@39582
   106
    file.deleteOnExit
wenzelm@39578
   107
    try { body(file) } finally { file.delete }
wenzelm@39578
   108
  }
wenzelm@39578
   109
wenzelm@34300
   110
  // FIXME handle (potentially cyclic) directory graph
wenzelm@34298
   111
  def find_files(start: File, ok: File => Boolean): List[File] =
wenzelm@34298
   112
  {
wenzelm@34298
   113
    val files = new mutable.ListBuffer[File]
wenzelm@34298
   114
    val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
wenzelm@34298
   115
    def find_entry(entry: File)
wenzelm@34298
   116
    {
wenzelm@34298
   117
      if (ok(entry)) files += entry
wenzelm@34298
   118
      if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
wenzelm@34298
   119
    }
wenzelm@34298
   120
    find_entry(start)
wenzelm@34298
   121
    files.toList
wenzelm@34298
   122
  }
wenzelm@34298
   123
wenzelm@34201
   124
wenzelm@34201
   125
  /* shell processes */
wenzelm@34201
   126
wenzelm@34219
   127
  def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
wenzelm@34201
   128
  {
wenzelm@34201
   129
    val cmdline = new java.util.LinkedList[String]
wenzelm@34201
   130
    for (s <- args) cmdline.add(s)
wenzelm@34201
   131
wenzelm@34201
   132
    val proc = new ProcessBuilder(cmdline)
wenzelm@34219
   133
    if (cwd != null) proc.directory(cwd)
wenzelm@34202
   134
    if (env != null) {
wenzelm@34202
   135
      proc.environment.clear
wenzelm@34202
   136
      for ((x, y) <- env) proc.environment.put(x, y)
wenzelm@34202
   137
    }
wenzelm@34201
   138
    proc.redirectErrorStream(redirect)
wenzelm@39522
   139
    proc.start
wenzelm@34201
   140
  }
wenzelm@34201
   141
wenzelm@34201
   142
  def process_output(proc: Process): (String, Int) =
wenzelm@34201
   143
  {
wenzelm@34201
   144
    proc.getOutputStream.close
wenzelm@39578
   145
    val output = slurp(proc.getInputStream)
wenzelm@34201
   146
    val rc =
wenzelm@34201
   147
      try { proc.waitFor }
wenzelm@34201
   148
      finally {
wenzelm@34201
   149
        proc.getInputStream.close
wenzelm@34201
   150
        proc.getErrorStream.close
wenzelm@34201
   151
        proc.destroy
wenzelm@34201
   152
        Thread.interrupted
wenzelm@34201
   153
      }
wenzelm@34201
   154
    (output, rc)
wenzelm@34201
   155
  }
wenzelm@34258
   156
wenzelm@39522
   157
  def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*)
wenzelm@39522
   158
    : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
wenzelm@39705
   159
wenzelm@39705
   160
wenzelm@39732
   161
  /* unpack zip archive -- platform file-system */
wenzelm@39732
   162
wenzelm@39732
   163
  def unzip(url: URL, root: File)
wenzelm@39732
   164
  {
wenzelm@39732
   165
    import scala.collection.JavaConversions._
wenzelm@39732
   166
wenzelm@39732
   167
    val buffer = new Array[Byte](4096)
wenzelm@39732
   168
wenzelm@39732
   169
    val zip_stream = new ZipInputStream(new BufferedInputStream(url.openStream))
wenzelm@39732
   170
    var entry: ZipEntry = null
wenzelm@39732
   171
    try {
wenzelm@39732
   172
      while ({ entry = zip_stream.getNextEntry; entry != null }) {
wenzelm@39732
   173
        val file = new File(root, entry.getName.replace('/', File.separatorChar))
wenzelm@39732
   174
        val dir = file.getParentFile
wenzelm@39732
   175
        if (dir != null && !dir.isDirectory && !dir.mkdirs)
wenzelm@39732
   176
          error("Failed to create directory: " + dir)
wenzelm@39732
   177
wenzelm@39732
   178
        var len = 0
wenzelm@39732
   179
        val out_stream = new BufferedOutputStream(new FileOutputStream(file))
wenzelm@39732
   180
        try {
wenzelm@39732
   181
          while ({ len = zip_stream.read(buffer); len != -1 })
wenzelm@39732
   182
            out_stream.write(buffer, 0, len)
wenzelm@39732
   183
        }
wenzelm@39732
   184
        finally { out_stream.close }
wenzelm@39732
   185
      }
wenzelm@39732
   186
    }
wenzelm@39732
   187
    finally { zip_stream.close }
wenzelm@39732
   188
  }
wenzelm@39732
   189
wenzelm@39732
   190
wenzelm@39732
   191
  /* unpack tar archive -- POSIX file-system */
wenzelm@39705
   192
wenzelm@39708
   193
  def posix_untar(url: URL, root: File, gunzip: Boolean = false,
wenzelm@39709
   194
    tar: String = "tar", gzip: String = "", progress: Int => Unit = _ => ()): String =
wenzelm@39705
   195
  {
wenzelm@39708
   196
    if (!root.isDirectory && !root.mkdirs)
wenzelm@39708
   197
      error("Failed to create root directory: " + root)
wenzelm@39705
   198
wenzelm@39705
   199
    val connection = url.openConnection
wenzelm@39705
   200
wenzelm@39705
   201
    val length = connection.getContentLength.toLong
wenzelm@39705
   202
    require(length >= 0L)
wenzelm@39705
   203
wenzelm@39708
   204
    val stream = new BufferedInputStream(connection.getInputStream)
wenzelm@39705
   205
    val progress_stream = new InputStream {
wenzelm@39705
   206
      private val total = length max 1L
wenzelm@39705
   207
      private var index = 0L
wenzelm@39705
   208
      private var percentage = 0L
wenzelm@39705
   209
      override def read(): Int =
wenzelm@39705
   210
      {
wenzelm@39708
   211
        val c = stream.read
wenzelm@39705
   212
        if (c != -1) {
wenzelm@39705
   213
          index += 100
wenzelm@39705
   214
          val p = index / total
wenzelm@39705
   215
          if (percentage != p) { percentage = p; progress(percentage.toInt) }
wenzelm@39705
   216
        }
wenzelm@39705
   217
        c
wenzelm@39705
   218
      }
wenzelm@39708
   219
      override def available(): Int = stream.available
wenzelm@39708
   220
      override def close() { stream.close }
wenzelm@39705
   221
    }
wenzelm@39705
   222
wenzelm@39709
   223
    val cmdline =
wenzelm@39709
   224
      List(tar, "-o", "-x", "-f-") :::
wenzelm@39709
   225
        (if (!gunzip) Nil else if (gzip == "") List("-z") else List("-I", gzip))
wenzelm@39709
   226
wenzelm@39709
   227
    val proc = raw_execute(root, null, false, cmdline:_*)
wenzelm@39705
   228
    val stdout = Simple_Thread.future("tar_stdout") { slurp(proc.getInputStream) }
wenzelm@39705
   229
    val stderr = Simple_Thread.future("tar_stderr") { slurp(proc.getErrorStream) }
wenzelm@39705
   230
    val stdin = new BufferedOutputStream(proc.getOutputStream)
wenzelm@39705
   231
wenzelm@39705
   232
    try {
wenzelm@39705
   233
      var c = -1
wenzelm@39706
   234
      val io_err =
wenzelm@39708
   235
        try { while ({ c = progress_stream.read; c != -1 }) stdin.write(c); false }
wenzelm@39706
   236
        catch { case e: IOException => true }
wenzelm@39705
   237
      stdin.close
wenzelm@39705
   238
wenzelm@39705
   239
      val rc = try { proc.waitFor } finally { Thread.interrupted }
wenzelm@39706
   240
      if (io_err || rc != 0) error(stderr.join.trim) else stdout.join
wenzelm@39705
   241
    }
wenzelm@39705
   242
    finally {
wenzelm@39708
   243
      progress_stream.close
wenzelm@39705
   244
      stdin.close
wenzelm@39705
   245
      proc.destroy
wenzelm@39705
   246
    }
wenzelm@39705
   247
  }
wenzelm@34201
   248
}
wenzelm@34201
   249
wenzelm@34201
   250
wenzelm@34201
   251
class Standard_System
wenzelm@34201
   252
{
wenzelm@34201
   253
  val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
wenzelm@34201
   254
  override def toString = platform_root
wenzelm@34201
   255
wenzelm@34201
   256
wenzelm@34201
   257
  /* jvm_path */
wenzelm@34201
   258
wenzelm@34201
   259
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
wenzelm@36136
   260
  private val Named_Root = new Regex("//+([^/]*)(.*)")
wenzelm@34201
   261
wenzelm@34201
   262
  def jvm_path(posix_path: String): String =
wenzelm@34201
   263
    if (Platform.is_windows) {
wenzelm@34201
   264
      val result_path = new StringBuilder
wenzelm@34201
   265
      val rest =
wenzelm@34201
   266
        posix_path match {
wenzelm@34201
   267
          case Cygdrive(drive, rest) =>
wenzelm@34201
   268
            result_path ++= (drive + ":" + File.separator)
wenzelm@34201
   269
            rest
wenzelm@36136
   270
          case Named_Root(root, rest) =>
wenzelm@36136
   271
            result_path ++= File.separator
wenzelm@36136
   272
            result_path ++= File.separator
wenzelm@36136
   273
            result_path ++= root
wenzelm@36136
   274
            rest
wenzelm@34201
   275
          case path if path.startsWith("/") =>
wenzelm@34201
   276
            result_path ++= platform_root
wenzelm@34201
   277
            path
wenzelm@34201
   278
          case path => path
wenzelm@34201
   279
        }
wenzelm@34201
   280
      for (p <- rest.split("/") if p != "") {
wenzelm@34201
   281
        val len = result_path.length
wenzelm@34201
   282
        if (len > 0 && result_path(len - 1) != File.separatorChar)
wenzelm@34201
   283
          result_path += File.separatorChar
wenzelm@34201
   284
        result_path ++= p
wenzelm@34201
   285
      }
wenzelm@34201
   286
      result_path.toString
wenzelm@34201
   287
    }
wenzelm@34201
   288
    else posix_path
wenzelm@34201
   289
wenzelm@34201
   290
wenzelm@34201
   291
  /* posix_path */
wenzelm@34201
   292
wenzelm@34201
   293
  private val Platform_Root = new Regex("(?i)" +
wenzelm@34201
   294
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
wenzelm@34201
   295
wenzelm@34201
   296
  private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
wenzelm@34201
   297
wenzelm@34201
   298
  def posix_path(jvm_path: String): String =
wenzelm@34201
   299
    if (Platform.is_windows) {
wenzelm@34201
   300
      jvm_path.replace('/', '\\') match {
wenzelm@34201
   301
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
wenzelm@34201
   302
        case Drive(letter, rest) =>
wenzelm@34201
   303
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
wenzelm@34201
   304
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
wenzelm@34201
   305
        case path => path.replace('\\', '/')
wenzelm@34201
   306
      }
wenzelm@34201
   307
    }
wenzelm@34201
   308
    else jvm_path
wenzelm@36193
   309
wenzelm@36193
   310
wenzelm@36193
   311
  /* this_java executable */
wenzelm@36193
   312
wenzelm@36193
   313
  def this_java(): String =
wenzelm@36193
   314
  {
wenzelm@36193
   315
    val java_home = System.getProperty("java.home")
wenzelm@36193
   316
    val java_exe =
wenzelm@36193
   317
      if (Platform.is_windows) new File(java_home + "\\bin\\java.exe")
wenzelm@36193
   318
      else new File(java_home + "/bin/java")
wenzelm@36193
   319
    if (!java_exe.isFile) error("Expected this Java executable: " + java_exe.toString)
wenzelm@36193
   320
    posix_path(java_exe.getAbsolutePath)
wenzelm@36193
   321
  }
wenzelm@34201
   322
}