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