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