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