src/Pure/System/isabelle_system.scala
author wenzelm
Tue Nov 03 16:35:38 2015 +0100 (2015-11-03)
changeset 61559 313eca3fa847
parent 61556 0d4ee4168e41
child 62291 98df25a6e2ac
permissions -rw-r--r--
more direct task future implementation, with proper cancel operation;
more uniform Future.thread;
     1 /*  Title:      Pure/System/isabelle_system.scala
     2     Author:     Makarius
     3 
     4 Fundamental Isabelle system environment: quasi-static module with
     5 optional init operation.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import java.io.{File => JFile, IOException, BufferedReader, InputStreamReader}
    12 import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult}
    13 import java.nio.file.attribute.BasicFileAttributes
    14 
    15 import scala.collection.mutable
    16 
    17 
    18 object Isabelle_System
    19 {
    20   /** bootstrap information **/
    21 
    22   def jdk_home(): String =
    23   {
    24     val java_home = System.getProperty("java.home", "")
    25     val home = new JFile(java_home)
    26     val parent = home.getParent
    27     if (home.getName == "jre" && parent != null &&
    28         (new JFile(new JFile(parent, "bin"), "javac")).exists) parent
    29     else java_home
    30   }
    31 
    32   def bootstrap_directory(
    33     preference: String, envar: String, property: String, description: String): String =
    34   {
    35     def check(s: String): Option[String] =
    36       if (s != null && s != "") Some(s) else None
    37 
    38     val value =
    39       check(preference) orElse  // explicit argument
    40       check(System.getenv(envar)) orElse  // e.g. inherited from running isabelle tool
    41       check(System.getProperty(property)) getOrElse  // e.g. via JVM application boot process
    42       error("Unknown " + description + " directory")
    43 
    44     if ((new JFile(value)).isDirectory) value
    45     else error("Bad " + description + " directory " + quote(value))
    46   }
    47 
    48 
    49 
    50   /** implicit settings environment **/
    51 
    52   @volatile private var _settings: Option[Map[String, String]] = None
    53 
    54   def settings(): Map[String, String] =
    55   {
    56     if (_settings.isEmpty) init()  // unsynchronized check
    57     _settings.get
    58   }
    59 
    60   def init(isabelle_root: String = "", cygwin_root: String = ""): Unit = synchronized {
    61     if (_settings.isEmpty) {
    62       import scala.collection.JavaConversions._
    63 
    64       val isabelle_root1 =
    65         bootstrap_directory(isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root")
    66 
    67       val cygwin_root1 =
    68         if (Platform.is_windows)
    69           bootstrap_directory(cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root")
    70         else ""
    71 
    72       if (Platform.is_windows) Cygwin.init(isabelle_root1, cygwin_root1)
    73 
    74       def set_cygwin_root()
    75       {
    76         if (Platform.is_windows)
    77           _settings = Some(_settings.getOrElse(Map.empty) + ("CYGWIN_ROOT" -> cygwin_root1))
    78       }
    79 
    80       set_cygwin_root()
    81 
    82       def default(env: Map[String, String], entry: (String, String)): Map[String, String] =
    83         if (env.isDefinedAt(entry._1) || entry._2 == "") env
    84         else env + entry
    85 
    86       val env =
    87       {
    88         val temp_windows =
    89         {
    90           val temp = if (Platform.is_windows) System.getenv("TEMP") else null
    91           if (temp != null && temp.contains('\\')) temp else ""
    92         }
    93         val user_home = System.getProperty("user.home", "")
    94         val isabelle_app = System.getProperty("isabelle.app", "")
    95 
    96         default(
    97           default(
    98             default(sys.env + ("ISABELLE_JDK_HOME" -> File.standard_path(jdk_home())),
    99               "TEMP_WINDOWS" -> temp_windows),
   100             "HOME" -> user_home),
   101           "ISABELLE_APP" -> "true")
   102       }
   103 
   104       val settings =
   105       {
   106         val dump = JFile.createTempFile("settings", null)
   107         dump.deleteOnExit
   108         try {
   109           val cmd1 =
   110             if (Platform.is_windows) List(cygwin_root1 + "\\bin\\bash", "-l") else Nil
   111           val cmd2 =
   112             List(isabelle_root1 + JFile.separator + "bin" + JFile.separator + "isabelle",
   113               "getenv", "-d", dump.toString)
   114 
   115           val (output, rc) = process_output(raw_execute(null, env, true, (cmd1 ::: cmd2): _*))
   116           if (rc != 0) error(output)
   117 
   118           val entries =
   119             (for (entry <- File.read(dump) split "\u0000" if entry != "") yield {
   120               val i = entry.indexOf('=')
   121               if (i <= 0) entry -> ""
   122               else entry.substring(0, i) -> entry.substring(i + 1)
   123             }).toMap
   124           entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM"
   125         }
   126         finally { dump.delete }
   127       }
   128       _settings = Some(settings)
   129       set_cygwin_root()
   130     }
   131   }
   132 
   133 
   134   /* getenv */
   135 
   136   def getenv(name: String): String = settings.getOrElse(name, "")
   137 
   138   def getenv_strict(name: String): String =
   139   {
   140     val value = getenv(name)
   141     if (value != "") value
   142     else error("Undefined Isabelle environment variable: " + quote(name))
   143   }
   144 
   145   def cygwin_root(): String = getenv_strict("CYGWIN_ROOT")
   146 
   147 
   148 
   149   /** file-system operations **/
   150 
   151   /* source files of Isabelle/ML bootstrap */
   152 
   153   def source_file(path: Path): Option[Path] =
   154   {
   155     def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
   156 
   157     if (path.is_absolute || path.is_current) check(path)
   158     else {
   159       check(Path.explode("~~/src/Pure") + path) orElse
   160         (if (getenv("ML_SOURCES") == "") None
   161          else check(Path.explode("$ML_SOURCES") + path))
   162     }
   163   }
   164 
   165 
   166   /* mkdirs */
   167 
   168   def mkdirs(path: Path): Unit =
   169     if (!path.is_dir) {
   170       bash("perl -e \"use File::Path make_path; make_path(" + File.shell_path(path) + ");\"")
   171       if (!path.is_dir) error("Failed to create directory: " + quote(File.platform_path(path)))
   172     }
   173 
   174 
   175 
   176   /** external processes **/
   177 
   178   /* raw execute for bootstrapping */
   179 
   180   def raw_execute(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process =
   181   {
   182     val cmdline = new java.util.LinkedList[String]
   183     for (s <- args) cmdline.add(s)
   184 
   185     val proc = new ProcessBuilder(cmdline)
   186     if (cwd != null) proc.directory(cwd)
   187     if (env != null) {
   188       proc.environment.clear
   189       for ((x, y) <- env) proc.environment.put(x, y)
   190     }
   191     proc.redirectErrorStream(redirect)
   192     proc.start
   193   }
   194 
   195   def process_output(proc: Process): (String, Int) =
   196   {
   197     proc.getOutputStream.close
   198 
   199     val output = File.read_stream(proc.getInputStream)
   200     val rc =
   201       try { proc.waitFor }
   202       finally {
   203         proc.getInputStream.close
   204         proc.getErrorStream.close
   205         proc.destroy
   206         Thread.interrupted
   207       }
   208     (output, rc)
   209   }
   210 
   211 
   212   /* plain execute */
   213 
   214   def execute_env(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process =
   215   {
   216     val cmdline =
   217       if (Platform.is_windows) List(cygwin_root() + "\\bin\\env.exe") ::: args.toList
   218       else args
   219     val env1 = if (env == null) settings else settings ++ env
   220     raw_execute(cwd, env1, redirect, cmdline: _*)
   221   }
   222 
   223   def execute(redirect: Boolean, args: String*): Process =
   224     execute_env(null, null, redirect, args: _*)
   225 
   226 
   227   /* tmp files */
   228 
   229   private def isabelle_tmp_prefix(): JFile =
   230   {
   231     val path = Path.explode("$ISABELLE_TMP_PREFIX")
   232     path.file.mkdirs  // low-level mkdirs
   233     File.platform_file(path)
   234   }
   235 
   236   def tmp_file[A](name: String, ext: String = ""): JFile =
   237   {
   238     val suffix = if (ext == "") "" else "." + ext
   239     val file = Files.createTempFile(isabelle_tmp_prefix().toPath, name, suffix).toFile
   240     file.deleteOnExit
   241     file
   242   }
   243 
   244   def with_tmp_file[A](name: String, ext: String = "")(body: JFile => A): A =
   245   {
   246     val file = tmp_file(name, ext)
   247     try { body(file) } finally { file.delete }
   248   }
   249 
   250 
   251   /* tmp dirs */
   252 
   253   def rm_tree(root: JFile)
   254   {
   255     root.delete
   256     if (root.isDirectory) {
   257       Files.walkFileTree(root.toPath,
   258         new SimpleFileVisitor[JPath] {
   259           override def visitFile(file: JPath, attrs: BasicFileAttributes): FileVisitResult =
   260           {
   261             Files.delete(file)
   262             FileVisitResult.CONTINUE
   263           }
   264 
   265           override def postVisitDirectory(dir: JPath, e: IOException): FileVisitResult =
   266           {
   267             if (e == null) {
   268               Files.delete(dir)
   269               FileVisitResult.CONTINUE
   270             }
   271             else throw e
   272           }
   273         }
   274       )
   275     }
   276   }
   277 
   278   def tmp_dir(name: String): JFile =
   279   {
   280     val dir = Files.createTempDirectory(isabelle_tmp_prefix().toPath, name).toFile
   281     dir.deleteOnExit
   282     dir
   283   }
   284 
   285   def with_tmp_dir[A](name: String)(body: JFile => A): A =
   286   {
   287     val dir = tmp_dir(name)
   288     try { body(dir) } finally { rm_tree(dir) }
   289   }
   290 
   291 
   292   /* kill */
   293 
   294   def kill(signal: String, group_pid: String): (String, Int) =
   295   {
   296     val bash =
   297       if (Platform.is_windows) List(cygwin_root() + "\\bin\\bash.exe")
   298       else List("/usr/bin/env", "bash")
   299     val cmdline = bash ::: List("-c", "kill -" + signal + " -" + group_pid)
   300     process_output(raw_execute(null, null, true, cmdline: _*))
   301   }
   302 
   303 
   304   /* bash */
   305 
   306   private class Limited_Progress(proc: Bash.Process, progress_limit: Option[Long])
   307   {
   308     private var count = 0L
   309     def apply(progress: String => Unit)(line: String): Unit = synchronized {
   310       progress(line)
   311       count = count + line.length + 1
   312       progress_limit match {
   313         case Some(limit) if count > limit => proc.terminate
   314         case _ =>
   315       }
   316     }
   317   }
   318 
   319   def bash_env(cwd: JFile, env: Map[String, String], script: String,
   320     progress_stdout: String => Unit = (_: String) => (),
   321     progress_stderr: String => Unit = (_: String) => (),
   322     progress_limit: Option[Long] = None,
   323     strict: Boolean = true): Bash.Result =
   324   {
   325     with_tmp_file("isabelle_script") { script_file =>
   326       File.write(script_file, script)
   327       val proc = Bash.process(cwd, env, false, "bash", File.standard_path(script_file))
   328       proc.stdin.close
   329 
   330       val limited = new Limited_Progress(proc, progress_limit)
   331       val stdout =
   332         Future.thread("bash_stdout") { File.read_lines(proc.stdout, limited(progress_stdout)) }
   333       val stderr =
   334         Future.thread("bash_stderr") { File.read_lines(proc.stderr, limited(progress_stderr)) }
   335 
   336       val rc =
   337         try { proc.join }
   338         catch { case Exn.Interrupt() => proc.terminate; Exn.Interrupt.return_code }
   339       if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt()
   340 
   341       Bash.Result(stdout.join, stderr.join, rc)
   342     }
   343   }
   344 
   345   def bash(script: String): Bash.Result = bash_env(null, null, script)
   346 
   347 
   348   /* system tools */
   349 
   350   def isabelle_tool(name: String, args: String*): (String, Int) =
   351   {
   352     Path.split(getenv_strict("ISABELLE_TOOLS")).find { dir =>
   353       val file = (dir + Path.basic(name)).file
   354       try {
   355         file.isFile && file.canRead && file.canExecute &&
   356           !name.endsWith("~") && !name.endsWith(".orig")
   357       }
   358       catch { case _: SecurityException => false }
   359     } match {
   360       case Some(dir) =>
   361         val file = File.standard_path(dir + Path.basic(name))
   362         process_output(execute(true, (List(file) ::: args.toList): _*))
   363       case None => ("Unknown Isabelle tool: " + name, 2)
   364     }
   365   }
   366 
   367   def open(arg: String): Unit =
   368     bash("exec \"$ISABELLE_OPEN\" '" + arg + "' >/dev/null 2>/dev/null &")
   369 
   370   def pdf_viewer(arg: Path): Unit =
   371     bash("exec \"$PDF_VIEWER\" '" + File.standard_path(arg) + "' >/dev/null 2>/dev/null &")
   372 
   373   def hg(cmd_line: String, cwd: Path = Path.current): Bash.Result =
   374     bash("cd " + File.shell_path(cwd) + " && \"${HG:-hg}\" " + cmd_line)
   375 
   376 
   377   /** Isabelle resources **/
   378 
   379   /* components */
   380 
   381   def components(): List[Path] =
   382     Path.split(getenv_strict("ISABELLE_COMPONENTS"))
   383 
   384 
   385   /* logic images */
   386 
   387   def find_logics_dirs(): List[Path] =
   388   {
   389     val ml_ident = Path.explode("$ML_IDENTIFIER").expand
   390     Path.split(getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
   391   }
   392 
   393   def find_logics(): List[String] =
   394     (for {
   395       dir <- find_logics_dirs()
   396       files = dir.file.listFiles() if files != null
   397       file <- files.toList if file.isFile } yield file.getName).sorted
   398 
   399   def default_logic(args: String*): String =
   400   {
   401     args.find(_ != "") match {
   402       case Some(logic) => logic
   403       case None => Isabelle_System.getenv_strict("ISABELLE_LOGIC")
   404     }
   405   }
   406 }