src/Pure/Thy/sessions.scala
author wenzelm
Wed Apr 13 17:00:02 2016 +0200 (2016-04-13 ago)
changeset 62968 4e4738698db4
parent 62946 9f537dd83677
child 62969 9f394a16c557
permissions -rw-r--r--
clarified syntax;
wenzelm@62631
     1
/*  Title:      Pure/Thy/sessions.scala
wenzelm@62631
     2
    Author:     Makarius
wenzelm@62631
     3
wenzelm@62631
     4
Session information.
wenzelm@62631
     5
*/
wenzelm@62631
     6
wenzelm@62631
     7
package isabelle
wenzelm@62631
     8
wenzelm@62704
     9
import java.nio.ByteBuffer
wenzelm@62704
    10
import java.nio.channels.FileChannel
wenzelm@62704
    11
import java.nio.file.StandardOpenOption
wenzelm@62631
    12
wenzelm@62631
    13
import scala.collection.SortedSet
wenzelm@62631
    14
import scala.collection.mutable
wenzelm@62631
    15
wenzelm@62631
    16
wenzelm@62631
    17
object Sessions
wenzelm@62631
    18
{
wenzelm@62883
    19
  /* Pure */
wenzelm@62883
    20
wenzelm@62883
    21
  def pure_name(name: String): Boolean = name == "Pure"
wenzelm@62883
    22
wenzelm@62902
    23
  def pure_files(resources: Resources, syntax: Outer_Syntax, dir: Path): List[Path] =
wenzelm@62902
    24
  {
wenzelm@62946
    25
    val roots = Thy_Header.ml_roots.map(_._1)
wenzelm@62902
    26
    val loaded_files =
wenzelm@62946
    27
      roots.flatMap(root => resources.loaded_files(syntax, File.read(dir + Path.explode(root))))
wenzelm@62946
    28
    (roots ::: loaded_files).map(file => dir + Path.explode(file))
wenzelm@62902
    29
  }
wenzelm@62883
    30
wenzelm@62883
    31
wenzelm@62883
    32
  /* info */
wenzelm@62631
    33
wenzelm@62631
    34
  sealed case class Info(
wenzelm@62631
    35
    chapter: String,
wenzelm@62631
    36
    select: Boolean,
wenzelm@62631
    37
    pos: Position.T,
wenzelm@62631
    38
    groups: List[String],
wenzelm@62631
    39
    dir: Path,
wenzelm@62631
    40
    parent: Option[String],
wenzelm@62631
    41
    description: String,
wenzelm@62631
    42
    options: Options,
wenzelm@62631
    43
    theories: List[(Boolean, Options, List[Path])],
wenzelm@62631
    44
    files: List[Path],
wenzelm@62631
    45
    document_files: List[(Path, Path)],
wenzelm@62631
    46
    meta_digest: SHA1.Digest)
wenzelm@62631
    47
  {
wenzelm@62631
    48
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
wenzelm@62631
    49
  }
wenzelm@62631
    50
wenzelm@62631
    51
wenzelm@62631
    52
  /* session tree */
wenzelm@62631
    53
wenzelm@62631
    54
  object Tree
wenzelm@62631
    55
  {
wenzelm@62631
    56
    def apply(infos: Seq[(String, Info)]): Tree =
wenzelm@62631
    57
    {
wenzelm@62631
    58
      val graph1 =
wenzelm@62631
    59
        (Graph.string[Info] /: infos) {
wenzelm@62631
    60
          case (graph, (name, info)) =>
wenzelm@62631
    61
            if (graph.defined(name))
wenzelm@62631
    62
              error("Duplicate session " + quote(name) + Position.here(info.pos) +
wenzelm@62631
    63
                Position.here(graph.get_node(name).pos))
wenzelm@62631
    64
            else graph.new_node(name, info)
wenzelm@62631
    65
        }
wenzelm@62631
    66
      val graph2 =
wenzelm@62631
    67
        (graph1 /: graph1.iterator) {
wenzelm@62631
    68
          case (graph, (name, (info, _))) =>
wenzelm@62631
    69
            info.parent match {
wenzelm@62631
    70
              case None => graph
wenzelm@62631
    71
              case Some(parent) =>
wenzelm@62631
    72
                if (!graph.defined(parent))
wenzelm@62631
    73
                  error("Bad parent session " + quote(parent) + " for " +
wenzelm@62631
    74
                    quote(name) + Position.here(info.pos))
wenzelm@62631
    75
wenzelm@62631
    76
                try { graph.add_edge_acyclic(parent, name) }
wenzelm@62631
    77
                catch {
wenzelm@62631
    78
                  case exn: Graph.Cycles[_] =>
wenzelm@62631
    79
                    error(cat_lines(exn.cycles.map(cycle =>
wenzelm@62631
    80
                      "Cyclic session dependency of " +
wenzelm@62631
    81
                        cycle.map(c => quote(c.toString)).mkString(" via "))) +
wenzelm@62631
    82
                          Position.here(info.pos))
wenzelm@62631
    83
                }
wenzelm@62631
    84
            }
wenzelm@62631
    85
        }
wenzelm@62631
    86
      new Tree(graph2)
wenzelm@62631
    87
    }
wenzelm@62631
    88
  }
wenzelm@62631
    89
wenzelm@62631
    90
  final class Tree private(val graph: Graph[String, Info])
wenzelm@62631
    91
    extends PartialFunction[String, Info]
wenzelm@62631
    92
  {
wenzelm@62631
    93
    def apply(name: String): Info = graph.get_node(name)
wenzelm@62631
    94
    def isDefinedAt(name: String): Boolean = graph.defined(name)
wenzelm@62631
    95
wenzelm@62631
    96
    def selection(
wenzelm@62631
    97
      requirements: Boolean = false,
wenzelm@62631
    98
      all_sessions: Boolean = false,
wenzelm@62631
    99
      exclude_session_groups: List[String] = Nil,
wenzelm@62631
   100
      exclude_sessions: List[String] = Nil,
wenzelm@62631
   101
      session_groups: List[String] = Nil,
wenzelm@62631
   102
      sessions: List[String] = Nil): (List[String], Tree) =
wenzelm@62631
   103
    {
wenzelm@62631
   104
      val bad_sessions =
wenzelm@62631
   105
        SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
wenzelm@62631
   106
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
wenzelm@62631
   107
wenzelm@62631
   108
      val excluded =
wenzelm@62631
   109
      {
wenzelm@62631
   110
        val exclude_group = exclude_session_groups.toSet
wenzelm@62631
   111
        val exclude_group_sessions =
wenzelm@62631
   112
          (for {
wenzelm@62631
   113
            (name, (info, _)) <- graph.iterator
wenzelm@62631
   114
            if apply(name).groups.exists(exclude_group)
wenzelm@62631
   115
          } yield name).toList
wenzelm@62631
   116
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
wenzelm@62631
   117
      }
wenzelm@62631
   118
wenzelm@62631
   119
      val pre_selected =
wenzelm@62631
   120
      {
wenzelm@62631
   121
        if (all_sessions) graph.keys
wenzelm@62631
   122
        else {
wenzelm@62631
   123
          val select_group = session_groups.toSet
wenzelm@62631
   124
          val select = sessions.toSet
wenzelm@62631
   125
          (for {
wenzelm@62631
   126
            (name, (info, _)) <- graph.iterator
wenzelm@62631
   127
            if info.select || select(name) || apply(name).groups.exists(select_group)
wenzelm@62631
   128
          } yield name).toList
wenzelm@62631
   129
        }
wenzelm@62631
   130
      }.filterNot(excluded)
wenzelm@62631
   131
wenzelm@62631
   132
      val selected =
wenzelm@62631
   133
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
wenzelm@62631
   134
        else pre_selected
wenzelm@62631
   135
wenzelm@62631
   136
      val graph1 = graph.restrict(graph.all_preds(selected).toSet)
wenzelm@62631
   137
      (selected, new Tree(graph1))
wenzelm@62631
   138
    }
wenzelm@62631
   139
wenzelm@62631
   140
    def ancestors(name: String): List[String] =
wenzelm@62631
   141
      graph.all_preds(List(name)).tail.reverse
wenzelm@62631
   142
wenzelm@62631
   143
    def topological_order: List[(String, Info)] =
wenzelm@62631
   144
      graph.topological_order.map(name => (name, apply(name)))
wenzelm@62631
   145
wenzelm@62631
   146
    override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
wenzelm@62631
   147
  }
wenzelm@62631
   148
wenzelm@62631
   149
wenzelm@62631
   150
  /* parser */
wenzelm@62631
   151
wenzelm@62864
   152
  val ROOT = Path.explode("ROOT")
wenzelm@62864
   153
  val ROOTS = Path.explode("ROOTS")
wenzelm@62864
   154
wenzelm@62631
   155
  private val CHAPTER = "chapter"
wenzelm@62631
   156
  private val SESSION = "session"
wenzelm@62631
   157
  private val IN = "in"
wenzelm@62631
   158
  private val DESCRIPTION = "description"
wenzelm@62631
   159
  private val OPTIONS = "options"
wenzelm@62631
   160
  private val GLOBAL_THEORIES = "global_theories"
wenzelm@62631
   161
  private val THEORIES = "theories"
wenzelm@62631
   162
  private val FILES = "files"
wenzelm@62631
   163
  private val DOCUMENT_FILES = "document_files"
wenzelm@62631
   164
wenzelm@62631
   165
  lazy val root_syntax =
wenzelm@62631
   166
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@62631
   167
      (CHAPTER, Keyword.THY_DECL) + (SESSION, Keyword.THY_DECL) +
wenzelm@62631
   168
      IN + DESCRIPTION + OPTIONS + GLOBAL_THEORIES + THEORIES + FILES + DOCUMENT_FILES
wenzelm@62631
   169
wenzelm@62968
   170
  private object Parser extends Parse.Parser with Options.Parser
wenzelm@62631
   171
  {
wenzelm@62631
   172
    private abstract class Entry
wenzelm@62631
   173
    private sealed case class Chapter(name: String) extends Entry
wenzelm@62631
   174
    private sealed case class Session_Entry(
wenzelm@62631
   175
      pos: Position.T,
wenzelm@62631
   176
      name: String,
wenzelm@62631
   177
      groups: List[String],
wenzelm@62631
   178
      path: String,
wenzelm@62631
   179
      parent: Option[String],
wenzelm@62631
   180
      description: String,
wenzelm@62631
   181
      options: List[Options.Spec],
wenzelm@62631
   182
      theories: List[(Boolean, List[Options.Spec], List[String])],
wenzelm@62631
   183
      files: List[String],
wenzelm@62631
   184
      document_files: List[(String, String)]) extends Entry
wenzelm@62631
   185
wenzelm@62631
   186
    private val chapter: Parser[Chapter] =
wenzelm@62631
   187
    {
wenzelm@62631
   188
      val chapter_name = atom("chapter name", _.is_name)
wenzelm@62631
   189
wenzelm@62631
   190
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
wenzelm@62631
   191
    }
wenzelm@62631
   192
wenzelm@62631
   193
    private val session_entry: Parser[Session_Entry] =
wenzelm@62631
   194
    {
wenzelm@62631
   195
      val session_name = atom("session name", _.is_name)
wenzelm@62631
   196
wenzelm@62631
   197
      val option =
wenzelm@62968
   198
        option_name ~ opt($$$("=") ~! option_value ^^
wenzelm@62968
   199
          { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@62631
   200
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
wenzelm@62631
   201
wenzelm@62631
   202
      val theories =
wenzelm@62631
   203
        ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
wenzelm@62631
   204
          ((options | success(Nil)) ~ rep(theory_xname)) ^^
wenzelm@62631
   205
          { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
wenzelm@62631
   206
wenzelm@62631
   207
      val document_files =
wenzelm@62631
   208
        $$$(DOCUMENT_FILES) ~!
wenzelm@62631
   209
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
wenzelm@62631
   210
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
wenzelm@62631
   211
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
wenzelm@62631
   212
wenzelm@62631
   213
      command(SESSION) ~!
wenzelm@62631
   214
        (position(session_name) ~
wenzelm@62631
   215
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   216
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
wenzelm@62631
   217
          ($$$("=") ~!
wenzelm@62631
   218
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
wenzelm@62631
   219
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
wenzelm@62631
   220
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   221
              rep1(theories) ~
wenzelm@62631
   222
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   223
              (rep(document_files) ^^ (x => x.flatten))))) ^^
wenzelm@62631
   224
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
wenzelm@62631
   225
            Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
wenzelm@62631
   226
    }
wenzelm@62631
   227
wenzelm@62631
   228
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   229
    {
wenzelm@62631
   230
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
wenzelm@62631
   231
      {
wenzelm@62631
   232
        try {
wenzelm@62631
   233
          val name = entry.name
wenzelm@62631
   234
wenzelm@62631
   235
          if (name == "") error("Bad session name")
wenzelm@62883
   236
          if (pure_name(name) && entry.parent.isDefined) error("Illegal parent session")
wenzelm@62883
   237
          if (!pure_name(name) && !entry.parent.isDefined) error("Missing parent session")
wenzelm@62631
   238
wenzelm@62631
   239
          val session_options = options ++ entry.options
wenzelm@62631
   240
wenzelm@62631
   241
          val theories =
wenzelm@62631
   242
            entry.theories.map({ case (global, opts, thys) =>
wenzelm@62631
   243
              (global, session_options ++ opts, thys.map(Path.explode(_))) })
wenzelm@62631
   244
          val files = entry.files.map(Path.explode(_))
wenzelm@62631
   245
          val document_files =
wenzelm@62631
   246
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
wenzelm@62631
   247
wenzelm@62631
   248
          val meta_digest =
wenzelm@62631
   249
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
wenzelm@62631
   250
              entry.theories, entry.files, entry.document_files).toString)
wenzelm@62631
   251
wenzelm@62631
   252
          val info =
wenzelm@62631
   253
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
wenzelm@62631
   254
              entry.parent, entry.description, session_options, theories, files,
wenzelm@62631
   255
              document_files, meta_digest)
wenzelm@62631
   256
wenzelm@62631
   257
          (name, info)
wenzelm@62631
   258
        }
wenzelm@62631
   259
        catch {
wenzelm@62631
   260
          case ERROR(msg) =>
wenzelm@62631
   261
            error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@62631
   262
              quote(entry.name) + Position.here(entry.pos))
wenzelm@62631
   263
        }
wenzelm@62631
   264
      }
wenzelm@62631
   265
wenzelm@62631
   266
      val root = dir + ROOT
wenzelm@62631
   267
      if (root.is_file) {
wenzelm@62631
   268
        val toks = Token.explode(root_syntax.keywords, File.read(root))
wenzelm@62631
   269
        val start = Token.Pos.file(root.implode)
wenzelm@62631
   270
wenzelm@62631
   271
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
wenzelm@62631
   272
          case Success(result, _) =>
wenzelm@62631
   273
            var entry_chapter = "Unsorted"
wenzelm@62631
   274
            val infos = new mutable.ListBuffer[(String, Info)]
wenzelm@62631
   275
            result.foreach {
wenzelm@62631
   276
              case Chapter(name) => entry_chapter = name
wenzelm@62631
   277
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
wenzelm@62631
   278
            }
wenzelm@62631
   279
            infos.toList
wenzelm@62631
   280
          case bad => error(bad.toString)
wenzelm@62631
   281
        }
wenzelm@62631
   282
      }
wenzelm@62631
   283
      else Nil
wenzelm@62631
   284
    }
wenzelm@62631
   285
  }
wenzelm@62631
   286
wenzelm@62631
   287
wenzelm@62635
   288
  /* load sessions from certain directories */
wenzelm@62631
   289
wenzelm@62631
   290
  private def is_session_dir(dir: Path): Boolean =
wenzelm@62631
   291
    (dir + ROOT).is_file || (dir + ROOTS).is_file
wenzelm@62631
   292
wenzelm@62631
   293
  private def check_session_dir(dir: Path): Path =
wenzelm@62631
   294
    if (is_session_dir(dir)) dir
wenzelm@62631
   295
    else error("Bad session root directory: " + dir.toString)
wenzelm@62631
   296
wenzelm@62635
   297
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
wenzelm@62631
   298
  {
wenzelm@62635
   299
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62635
   300
      load_root(select, dir) ::: load_roots(select, dir)
wenzelm@62631
   301
wenzelm@62635
   302
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   303
      Parser.parse(options, select, dir)
wenzelm@62631
   304
wenzelm@62635
   305
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   306
    {
wenzelm@62631
   307
      val roots = dir + ROOTS
wenzelm@62631
   308
      if (roots.is_file) {
wenzelm@62631
   309
        for {
wenzelm@62631
   310
          line <- split_lines(File.read(roots))
wenzelm@62631
   311
          if !(line == "" || line.startsWith("#"))
wenzelm@62631
   312
          dir1 =
wenzelm@62631
   313
            try { check_session_dir(dir + Path.explode(line)) }
wenzelm@62631
   314
            catch {
wenzelm@62631
   315
              case ERROR(msg) =>
wenzelm@62631
   316
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
wenzelm@62631
   317
            }
wenzelm@62635
   318
          info <- load_dir(select, dir1)
wenzelm@62631
   319
        } yield info
wenzelm@62631
   320
      }
wenzelm@62631
   321
      else Nil
wenzelm@62631
   322
    }
wenzelm@62631
   323
wenzelm@62631
   324
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
wenzelm@62631
   325
    dirs.foreach(check_session_dir(_))
wenzelm@62631
   326
    select_dirs.foreach(check_session_dir(_))
wenzelm@62631
   327
wenzelm@62631
   328
    Tree(
wenzelm@62631
   329
      for {
wenzelm@62631
   330
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
wenzelm@62635
   331
        info <- load_dir(select, dir)
wenzelm@62631
   332
      } yield info)
wenzelm@62631
   333
  }
wenzelm@62632
   334
wenzelm@62632
   335
wenzelm@62637
   336
wenzelm@62704
   337
  /** heap file with SHA1 digest **/
wenzelm@62704
   338
wenzelm@62704
   339
  private val sha1_prefix = "SHA1:"
wenzelm@62704
   340
wenzelm@62704
   341
  def read_heap_digest(heap: Path): Option[String] =
wenzelm@62704
   342
  {
wenzelm@62704
   343
    if (heap.is_file) {
wenzelm@62704
   344
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
wenzelm@62704
   345
      try {
wenzelm@62704
   346
        val len = file.size
wenzelm@62704
   347
        val n = sha1_prefix.length + SHA1.digest_length
wenzelm@62704
   348
        if (len >= n) {
wenzelm@62704
   349
          file.position(len - n)
wenzelm@62704
   350
wenzelm@62704
   351
          val buf = ByteBuffer.allocate(n)
wenzelm@62704
   352
          var i = 0
wenzelm@62704
   353
          var m = 0
wenzelm@62704
   354
          do {
wenzelm@62704
   355
            m = file.read(buf)
wenzelm@62704
   356
            if (m != -1) i += m
wenzelm@62704
   357
          }
wenzelm@62704
   358
          while (m != -1 && n > i)
wenzelm@62704
   359
wenzelm@62704
   360
          if (i == n) {
wenzelm@62704
   361
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
wenzelm@62704
   362
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
wenzelm@62704
   363
            if (prefix == sha1_prefix) Some(s) else None
wenzelm@62704
   364
          }
wenzelm@62704
   365
          else None
wenzelm@62704
   366
        }
wenzelm@62704
   367
        else None
wenzelm@62704
   368
      }
wenzelm@62704
   369
      finally { file.close }
wenzelm@62704
   370
    }
wenzelm@62704
   371
    else None
wenzelm@62704
   372
  }
wenzelm@62704
   373
wenzelm@62704
   374
  def write_heap_digest(heap: Path): String =
wenzelm@62704
   375
    read_heap_digest(heap) match {
wenzelm@62704
   376
      case None =>
wenzelm@62704
   377
        val s = SHA1.digest(heap).rep
wenzelm@62704
   378
        File.append(heap, sha1_prefix + s)
wenzelm@62704
   379
        s
wenzelm@62704
   380
      case Some(s) => s
wenzelm@62704
   381
    }
wenzelm@62704
   382
wenzelm@62704
   383
wenzelm@62704
   384
wenzelm@62637
   385
  /** persistent store **/
wenzelm@62632
   386
wenzelm@62632
   387
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
wenzelm@62632
   388
  def log_gz(name: String): Path = log(name).ext("gz")
wenzelm@62632
   389
wenzelm@62632
   390
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
wenzelm@62632
   391
wenzelm@62632
   392
  class Store private [Sessions](system_mode: Boolean)
wenzelm@62632
   393
  {
wenzelm@62637
   394
    /* output */
wenzelm@62632
   395
wenzelm@62632
   396
    val browser_info: Path =
wenzelm@62632
   397
      if (system_mode) Path.explode("~~/browser_info")
wenzelm@62632
   398
      else Path.explode("$ISABELLE_BROWSER_INFO")
wenzelm@62632
   399
wenzelm@62637
   400
    val output_dir: Path =
wenzelm@62637
   401
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
wenzelm@62637
   402
      else Path.explode("$ISABELLE_OUTPUT")
wenzelm@62637
   403
wenzelm@62637
   404
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
wenzelm@62637
   405
wenzelm@62637
   406
wenzelm@62637
   407
    /* input */
wenzelm@62637
   408
wenzelm@62637
   409
    private val input_dirs =
wenzelm@62632
   410
      if (system_mode) List(output_dir)
wenzelm@62633
   411
      else {
wenzelm@62633
   412
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
wenzelm@62633
   413
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
wenzelm@62633
   414
      }
wenzelm@62632
   415
wenzelm@62636
   416
    def find(name: String): Option[(Path, Option[String])] =
wenzelm@62632
   417
      input_dirs.find(dir => (dir + log_gz(name)).is_file).map(dir =>
wenzelm@62704
   418
        (dir + log_gz(name), read_heap_digest(dir + Path.basic(name))))
wenzelm@62632
   419
wenzelm@62633
   420
    def find_log(name: String): Option[Path] =
wenzelm@62633
   421
      input_dirs.map(_ + log(name)).find(_.is_file)
wenzelm@62633
   422
wenzelm@62633
   423
    def find_log_gz(name: String): Option[Path] =
wenzelm@62633
   424
      input_dirs.map(_ + log_gz(name)).find(_.is_file)
wenzelm@62632
   425
wenzelm@62637
   426
    def find_heap(name: String): Option[Path] =
wenzelm@62637
   427
      input_dirs.map(_ + Path.basic(name)).find(_.is_file)
wenzelm@62637
   428
wenzelm@62637
   429
    def heap(name: String): Path =
wenzelm@62637
   430
      find_heap(name) getOrElse
wenzelm@62637
   431
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
wenzelm@62769
   432
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
wenzelm@62632
   433
  }
wenzelm@62631
   434
}