src/Pure/Thy/sessions.scala
author wenzelm
Fri Apr 07 15:53:06 2017 +0200 (2017-04-07)
changeset 65428 f8dd71a0791a
parent 65427 7689342a3097
child 65429 fcff401fb609
permissions -rw-r--r--
support for all_known_theories of all sessions;
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@65320
    12
import java.sql.PreparedStatement
wenzelm@62631
    13
wenzelm@62631
    14
import scala.collection.SortedSet
wenzelm@62631
    15
import scala.collection.mutable
wenzelm@62631
    16
wenzelm@62631
    17
wenzelm@62631
    18
object Sessions
wenzelm@62631
    19
{
wenzelm@65360
    20
  /* base info and source dependencies */
wenzelm@62883
    21
wenzelm@65360
    22
  def is_pure(name: String): Boolean = name == Thy_Header.PURE
wenzelm@64856
    23
wenzelm@64856
    24
  object Base
wenzelm@64856
    25
  {
wenzelm@65360
    26
    def pure(options: Options): Base = session_base(options, Thy_Header.PURE)
wenzelm@65360
    27
wenzelm@64856
    28
    lazy val bootstrap: Base =
wenzelm@64856
    29
      Base(keywords = Thy_Header.bootstrap_header, syntax = Thy_Header.bootstrap_syntax)
wenzelm@65427
    30
wenzelm@65427
    31
    private[Sessions] def known_theories(bases: Iterable[Base], names: Iterable[Document.Node.Name])
wenzelm@65427
    32
      : Map[String, Document.Node.Name] =
wenzelm@65427
    33
    {
wenzelm@65427
    34
      val bases_iterator =
wenzelm@65427
    35
        for { base <- bases.iterator; (_, name) <- base.known_theories.iterator }
wenzelm@65427
    36
          yield name
wenzelm@65427
    37
wenzelm@65427
    38
      (Map.empty[String, Document.Node.Name] /: (bases_iterator ++ names.iterator))({
wenzelm@65427
    39
        case (known, name) =>
wenzelm@65427
    40
          known.get(name.theory) match {
wenzelm@65427
    41
            case Some(name1) if name != name1 =>
wenzelm@65427
    42
              error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node))
wenzelm@65427
    43
            case _ =>
wenzelm@65427
    44
              known + (name.theory -> name) +
wenzelm@65427
    45
                (Long_Name.base_name(name.theory) -> name)  // legacy
wenzelm@65427
    46
          }
wenzelm@65427
    47
        })
wenzelm@65427
    48
    }
wenzelm@64856
    49
  }
wenzelm@64856
    50
wenzelm@64856
    51
  sealed case class Base(
wenzelm@65372
    52
    global_theories: Set[String] = Set.empty,
wenzelm@64856
    53
    loaded_theories: Set[String] = Set.empty,
wenzelm@64856
    54
    known_theories: Map[String, Document.Node.Name] = Map.empty,
wenzelm@64856
    55
    keywords: Thy_Header.Keywords = Nil,
wenzelm@64856
    56
    syntax: Outer_Syntax = Outer_Syntax.empty,
wenzelm@64856
    57
    sources: List[(Path, SHA1.Digest)] = Nil,
wenzelm@64856
    58
    session_graph: Graph_Display.Graph = Graph_Display.empty_graph)
wenzelm@65355
    59
  {
wenzelm@65355
    60
    def loaded_theory(name: Document.Node.Name): Boolean =
wenzelm@65355
    61
      loaded_theories.contains(name.theory)
wenzelm@65355
    62
  }
wenzelm@64856
    63
wenzelm@65406
    64
  sealed case class Deps(sessions: Map[String, Base])
wenzelm@65251
    65
  {
wenzelm@65406
    66
    def is_empty: Boolean = sessions.isEmpty
wenzelm@65406
    67
    def apply(name: String): Base = sessions(name)
wenzelm@65406
    68
    def sources(name: String): List[SHA1.Digest] = sessions(name).sources.map(_._2)
wenzelm@65428
    69
wenzelm@65428
    70
    def all_known_theories: Map[String, Document.Node.Name] =
wenzelm@65428
    71
      Base.known_theories(sessions.toList.map(_._2), Nil)
wenzelm@65251
    72
  }
wenzelm@64856
    73
wenzelm@65415
    74
  def deps(sessions: T,
wenzelm@65251
    75
      progress: Progress = No_Progress,
wenzelm@65251
    76
      inlined_files: Boolean = false,
wenzelm@65251
    77
      verbose: Boolean = false,
wenzelm@65251
    78
      list_files: Boolean = false,
wenzelm@65251
    79
      check_keywords: Set[String] = Set.empty,
wenzelm@65406
    80
      global_theories: Set[String] = Set.empty): Deps =
wenzelm@65372
    81
  {
wenzelm@65420
    82
    Deps((Map.empty[String, Base] /: sessions.imports_topological_order)({
wenzelm@65425
    83
      case (sessions, (session_name, info)) =>
wenzelm@65410
    84
        if (progress.stopped) throw Exn.Interrupt()
wenzelm@65251
    85
wenzelm@65410
    86
        try {
wenzelm@65410
    87
          val parent_base =
wenzelm@65410
    88
            info.parent match {
wenzelm@65410
    89
              case None => Base.bootstrap
wenzelm@65410
    90
              case Some(parent) => sessions(parent)
wenzelm@65410
    91
            }
wenzelm@65425
    92
          val resources = new Resources(session_name, parent_base)
wenzelm@65251
    93
wenzelm@65410
    94
          if (verbose || list_files) {
wenzelm@65410
    95
            val groups =
wenzelm@65410
    96
              if (info.groups.isEmpty) ""
wenzelm@65410
    97
              else info.groups.mkString(" (", " ", ")")
wenzelm@65425
    98
            progress.echo("Session " + info.chapter + "/" + session_name + groups)
wenzelm@65410
    99
          }
wenzelm@65251
   100
wenzelm@65410
   101
          val thy_deps =
wenzelm@65410
   102
          {
wenzelm@65410
   103
            val root_theories =
wenzelm@65410
   104
              info.theories.flatMap({ case (_, thys) =>
wenzelm@65410
   105
                thys.map(thy => (resources.import_name(info.dir.implode, thy), info.pos))
wenzelm@65410
   106
              })
wenzelm@65410
   107
            val thy_deps = resources.thy_info.dependencies(root_theories)
wenzelm@65251
   108
wenzelm@65410
   109
            thy_deps.errors match {
wenzelm@65410
   110
              case Nil => thy_deps
wenzelm@65410
   111
              case errs => error(cat_lines(errs))
wenzelm@65251
   112
            }
wenzelm@65410
   113
          }
wenzelm@65251
   114
wenzelm@65410
   115
          val known_theories =
wenzelm@65427
   116
            Base.known_theories(
wenzelm@65427
   117
              parent_base :: info.imports.map(sessions(_)), thy_deps.deps.map(_.name))
wenzelm@65251
   118
wenzelm@65410
   119
          val loaded_theories = thy_deps.loaded_theories
wenzelm@65410
   120
          val keywords = thy_deps.keywords
wenzelm@65410
   121
          val syntax = thy_deps.syntax
wenzelm@65251
   122
wenzelm@65410
   123
          val theory_files = thy_deps.deps.map(dep => Path.explode(dep.name.node))
wenzelm@65410
   124
          val loaded_files =
wenzelm@65410
   125
            if (inlined_files) {
wenzelm@65410
   126
              val pure_files =
wenzelm@65425
   127
                if (is_pure(session_name)) {
wenzelm@65410
   128
                  val roots = Thy_Header.ml_roots.map(p => info.dir + Path.explode(p._1))
wenzelm@65410
   129
                  val files =
wenzelm@65410
   130
                    roots.flatMap(root => resources.loaded_files(syntax, File.read(root))).
wenzelm@65410
   131
                      map(file => info.dir + Path.explode(file))
wenzelm@65410
   132
                  roots ::: files
wenzelm@65410
   133
                }
wenzelm@65410
   134
                else Nil
wenzelm@65410
   135
              pure_files ::: thy_deps.loaded_files
wenzelm@65410
   136
            }
wenzelm@65410
   137
            else Nil
wenzelm@65251
   138
wenzelm@65410
   139
          val all_files =
wenzelm@65410
   140
            (theory_files ::: loaded_files :::
wenzelm@65410
   141
              info.files.map(file => info.dir + file) :::
wenzelm@65410
   142
              info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand)
wenzelm@65251
   143
wenzelm@65410
   144
          if (list_files)
wenzelm@65410
   145
            progress.echo(cat_lines(all_files.map(_.implode).sorted.map("  " + _)))
wenzelm@65251
   146
wenzelm@65410
   147
          if (check_keywords.nonEmpty)
wenzelm@65410
   148
            Check_Keywords.check_keywords(progress, syntax.keywords, check_keywords, theory_files)
wenzelm@65251
   149
wenzelm@65410
   150
          val base =
wenzelm@65410
   151
            Base(global_theories = global_theories,
wenzelm@65410
   152
              loaded_theories = loaded_theories,
wenzelm@65410
   153
              known_theories = known_theories,
wenzelm@65410
   154
              keywords = keywords,
wenzelm@65410
   155
              syntax = syntax,
wenzelm@65410
   156
              sources = all_files.map(p => (p, SHA1.digest(p.file))),
wenzelm@65410
   157
              session_graph = thy_deps.session_graph(info.parent getOrElse "", parent_base))
wenzelm@65251
   158
wenzelm@65425
   159
          sessions + (session_name -> base)
wenzelm@65410
   160
        }
wenzelm@65410
   161
        catch {
wenzelm@65410
   162
          case ERROR(msg) =>
wenzelm@65410
   163
            cat_error(msg, "The error(s) above occurred in session " +
wenzelm@65425
   164
              quote(session_name) + Position.here(info.pos))
wenzelm@65410
   165
        }
wenzelm@65410
   166
    }))
wenzelm@65372
   167
  }
wenzelm@65251
   168
wenzelm@65428
   169
  def session_base(
wenzelm@65428
   170
    options: Options,
wenzelm@65428
   171
    session: String,
wenzelm@65428
   172
    dirs: List[Path] = Nil,
wenzelm@65428
   173
    all_known_theories: Boolean = false): Base =
wenzelm@65251
   174
  {
wenzelm@65415
   175
    val full_sessions = load(options, dirs = dirs)
wenzelm@65428
   176
    val global_theories = full_sessions.global_theories
wenzelm@65428
   177
    val selected_sessions = full_sessions.selection(Selection(sessions = List(session)))._2
wenzelm@65372
   178
wenzelm@65428
   179
    if (all_known_theories) {
wenzelm@65428
   180
      val deps = Sessions.deps(full_sessions, global_theories = global_theories)
wenzelm@65428
   181
      deps(session).copy(known_theories = deps.all_known_theories)
wenzelm@65428
   182
    }
wenzelm@65428
   183
    else
wenzelm@65428
   184
      deps(selected_sessions, global_theories = global_theories)(session)
wenzelm@65251
   185
  }
wenzelm@65251
   186
wenzelm@65251
   187
wenzelm@65415
   188
  /* cumulative session info */
wenzelm@62631
   189
wenzelm@62631
   190
  sealed case class Info(
wenzelm@62631
   191
    chapter: String,
wenzelm@62631
   192
    select: Boolean,
wenzelm@62631
   193
    pos: Position.T,
wenzelm@62631
   194
    groups: List[String],
wenzelm@62631
   195
    dir: Path,
wenzelm@62631
   196
    parent: Option[String],
wenzelm@62631
   197
    description: String,
wenzelm@62631
   198
    options: Options,
wenzelm@65420
   199
    imports: List[String],
wenzelm@65392
   200
    theories: List[(Options, List[String])],
wenzelm@65374
   201
    global_theories: List[String],
wenzelm@62631
   202
    files: List[Path],
wenzelm@62631
   203
    document_files: List[(Path, Path)],
wenzelm@62631
   204
    meta_digest: SHA1.Digest)
wenzelm@62631
   205
  {
wenzelm@62631
   206
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
wenzelm@62631
   207
  }
wenzelm@62631
   208
wenzelm@65419
   209
  object Selection
wenzelm@65419
   210
  {
wenzelm@65422
   211
    val empty: Selection = Selection()
wenzelm@65419
   212
  }
wenzelm@65419
   213
wenzelm@65419
   214
  sealed case class Selection(
wenzelm@65419
   215
    requirements: Boolean = false,
wenzelm@65419
   216
    all_sessions: Boolean = false,
wenzelm@65419
   217
    exclude_session_groups: List[String] = Nil,
wenzelm@65419
   218
    exclude_sessions: List[String] = Nil,
wenzelm@65419
   219
    session_groups: List[String] = Nil,
wenzelm@65419
   220
    sessions: List[String] = Nil)
wenzelm@65419
   221
  {
wenzelm@65422
   222
    def + (other: Selection): Selection =
wenzelm@65422
   223
      Selection(
wenzelm@65422
   224
        requirements = requirements || other.requirements,
wenzelm@65422
   225
        all_sessions = all_sessions || other.all_sessions,
wenzelm@65422
   226
        exclude_session_groups = exclude_session_groups ::: other.exclude_session_groups,
wenzelm@65422
   227
        exclude_sessions = exclude_sessions ::: other.exclude_sessions,
wenzelm@65422
   228
        session_groups = session_groups ::: other.session_groups,
wenzelm@65422
   229
        sessions = sessions ::: other.sessions)
wenzelm@65422
   230
wenzelm@65419
   231
    def apply(graph: Graph[String, Info]): (List[String], Graph[String, Info]) =
wenzelm@65419
   232
    {
wenzelm@65419
   233
      val bad_sessions =
wenzelm@65419
   234
        SortedSet((exclude_sessions ::: sessions).filterNot(graph.defined(_)): _*).toList
wenzelm@65419
   235
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
wenzelm@65419
   236
wenzelm@65419
   237
      val excluded =
wenzelm@65419
   238
      {
wenzelm@65419
   239
        val exclude_group = exclude_session_groups.toSet
wenzelm@65419
   240
        val exclude_group_sessions =
wenzelm@65419
   241
          (for {
wenzelm@65419
   242
            (name, (info, _)) <- graph.iterator
wenzelm@65419
   243
            if graph.get_node(name).groups.exists(exclude_group)
wenzelm@65419
   244
          } yield name).toList
wenzelm@65419
   245
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
wenzelm@65419
   246
      }
wenzelm@65419
   247
wenzelm@65419
   248
      val pre_selected =
wenzelm@65419
   249
      {
wenzelm@65419
   250
        if (all_sessions) graph.keys
wenzelm@65419
   251
        else {
wenzelm@65419
   252
          val select_group = session_groups.toSet
wenzelm@65419
   253
          val select = sessions.toSet
wenzelm@65419
   254
          (for {
wenzelm@65419
   255
            (name, (info, _)) <- graph.iterator
wenzelm@65419
   256
            if info.select || select(name) || graph.get_node(name).groups.exists(select_group)
wenzelm@65419
   257
          } yield name).toList
wenzelm@65419
   258
        }
wenzelm@65419
   259
      }.filterNot(excluded)
wenzelm@65419
   260
wenzelm@65419
   261
      val selected =
wenzelm@65419
   262
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
wenzelm@65419
   263
        else pre_selected
wenzelm@65419
   264
wenzelm@65419
   265
      (selected, graph.restrict(graph.all_preds(selected).toSet))
wenzelm@65419
   266
    }
wenzelm@65419
   267
  }
wenzelm@65419
   268
wenzelm@65415
   269
  def make(infos: Traversable[(String, Info)]): T =
wenzelm@62631
   270
  {
wenzelm@65420
   271
    def add_edges(graph: Graph[String, Info], kind: String, edges: Info => Traversable[String])
wenzelm@65420
   272
      : Graph[String, Info] =
wenzelm@65420
   273
    {
wenzelm@65420
   274
      def add_edge(pos: Position.T, name: String, g: Graph[String, Info], parent: String) =
wenzelm@65420
   275
      {
wenzelm@65420
   276
        if (!g.defined(parent))
wenzelm@65420
   277
          error("Bad " + kind + " session " + quote(parent) + " for " +
wenzelm@65420
   278
            quote(name) + Position.here(pos))
wenzelm@65420
   279
wenzelm@65420
   280
        try { g.add_edge_acyclic(parent, name) }
wenzelm@65420
   281
        catch {
wenzelm@65420
   282
          case exn: Graph.Cycles[_] =>
wenzelm@65420
   283
            error(cat_lines(exn.cycles.map(cycle =>
wenzelm@65420
   284
              "Cyclic session dependency of " +
wenzelm@65420
   285
                cycle.map(c => quote(c.toString)).mkString(" via "))) + Position.here(pos))
wenzelm@65420
   286
        }
wenzelm@65420
   287
      }
wenzelm@65420
   288
      (graph /: graph.iterator) {
wenzelm@65420
   289
        case (g, (name, (info, _))) => (g /: edges(info))(add_edge(info.pos, name, _, _))
wenzelm@65420
   290
      }
wenzelm@65420
   291
    }
wenzelm@65420
   292
wenzelm@65420
   293
    val graph0 =
wenzelm@65415
   294
      (Graph.string[Info] /: infos) {
wenzelm@65415
   295
        case (graph, (name, info)) =>
wenzelm@65415
   296
          if (graph.defined(name))
wenzelm@65415
   297
            error("Duplicate session " + quote(name) + Position.here(info.pos) +
wenzelm@65415
   298
              Position.here(graph.get_node(name).pos))
wenzelm@65415
   299
          else graph.new_node(name, info)
wenzelm@65415
   300
      }
wenzelm@65420
   301
    val graph1 = add_edges(graph0, "parent", _.parent)
wenzelm@65420
   302
    val graph2 = add_edges(graph1, "imports", _.imports)
wenzelm@62631
   303
wenzelm@65420
   304
    new T(graph1, graph2)
wenzelm@62631
   305
  }
wenzelm@62631
   306
wenzelm@65420
   307
  final class T private[Sessions](
wenzelm@65420
   308
      val build_graph: Graph[String, Info],
wenzelm@65420
   309
      val imports_graph: Graph[String, Info])
wenzelm@62631
   310
  {
wenzelm@65420
   311
    def apply(name: String): Info = imports_graph.get_node(name)
wenzelm@65420
   312
    def get(name: String): Option[Info] =
wenzelm@65420
   313
      if (imports_graph.defined(name)) Some(imports_graph.get_node(name)) else None
wenzelm@62631
   314
wenzelm@65372
   315
    def global_theories: Set[String] =
wenzelm@65424
   316
      (Set.empty[String] /:
wenzelm@65424
   317
        (for {
wenzelm@65424
   318
          (_, (info, _)) <- imports_graph.iterator
wenzelm@65424
   319
          thy <- info.global_theories.iterator }
wenzelm@65424
   320
         yield (thy, info.pos)))(
wenzelm@65424
   321
          { case (set, (thy, pos)) =>
wenzelm@65424
   322
             if (set.contains(thy))
wenzelm@65424
   323
               error("Duplicate declaration of global theory " + quote(thy) + Position.here(pos))
wenzelm@65424
   324
             else set + thy
wenzelm@65424
   325
           })
wenzelm@65372
   326
wenzelm@65419
   327
    def selection(select: Selection): (List[String], T) =
wenzelm@62631
   328
    {
wenzelm@65420
   329
      val (_, build_graph1) = select(build_graph)
wenzelm@65420
   330
      val (selected, imports_graph1) = select(imports_graph)
wenzelm@65420
   331
      (selected, new T(build_graph1, imports_graph1))
wenzelm@62631
   332
    }
wenzelm@62631
   333
wenzelm@65420
   334
    def build_ancestors(name: String): List[String] =
wenzelm@65420
   335
      build_graph.all_preds(List(name)).tail.reverse
wenzelm@65420
   336
wenzelm@65420
   337
    def build_descendants(names: List[String]): List[String] =
wenzelm@65420
   338
      build_graph.all_succs(names)
wenzelm@62631
   339
wenzelm@65420
   340
    def build_topological_order: List[(String, Info)] =
wenzelm@65420
   341
      build_graph.topological_order.map(name => (name, apply(name)))
wenzelm@62631
   342
wenzelm@65420
   343
    def imports_topological_order: List[(String, Info)] =
wenzelm@65420
   344
      imports_graph.topological_order.map(name => (name, apply(name)))
wenzelm@65420
   345
wenzelm@65420
   346
    override def toString: String =
wenzelm@65420
   347
      imports_graph.keys_iterator.mkString("Sessions.T(", ", ", ")")
wenzelm@62631
   348
  }
wenzelm@62631
   349
wenzelm@62631
   350
wenzelm@62631
   351
  /* parser */
wenzelm@62631
   352
wenzelm@62864
   353
  val ROOT = Path.explode("ROOT")
wenzelm@62864
   354
  val ROOTS = Path.explode("ROOTS")
wenzelm@62864
   355
wenzelm@62631
   356
  private val CHAPTER = "chapter"
wenzelm@62631
   357
  private val SESSION = "session"
wenzelm@62631
   358
  private val IN = "in"
wenzelm@62631
   359
  private val DESCRIPTION = "description"
wenzelm@62631
   360
  private val OPTIONS = "options"
wenzelm@65420
   361
  private val SESSIONS = "sessions"
wenzelm@62631
   362
  private val THEORIES = "theories"
wenzelm@65374
   363
  private val GLOBAL = "global"
wenzelm@62631
   364
  private val FILES = "files"
wenzelm@62631
   365
  private val DOCUMENT_FILES = "document_files"
wenzelm@62631
   366
wenzelm@62631
   367
  lazy val root_syntax =
wenzelm@65374
   368
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + GLOBAL + IN +
wenzelm@63443
   369
      (CHAPTER, Keyword.THY_DECL) +
wenzelm@63443
   370
      (SESSION, Keyword.THY_DECL) +
wenzelm@63443
   371
      (DESCRIPTION, Keyword.QUASI_COMMAND) +
wenzelm@63443
   372
      (OPTIONS, Keyword.QUASI_COMMAND) +
wenzelm@65420
   373
      (SESSIONS, Keyword.QUASI_COMMAND) +
wenzelm@63443
   374
      (THEORIES, Keyword.QUASI_COMMAND) +
wenzelm@63443
   375
      (FILES, Keyword.QUASI_COMMAND) +
wenzelm@63443
   376
      (DOCUMENT_FILES, Keyword.QUASI_COMMAND)
wenzelm@62631
   377
wenzelm@62968
   378
  private object Parser extends Parse.Parser with Options.Parser
wenzelm@62631
   379
  {
wenzelm@62631
   380
    private abstract class Entry
wenzelm@62631
   381
    private sealed case class Chapter(name: String) extends Entry
wenzelm@62631
   382
    private sealed case class Session_Entry(
wenzelm@62631
   383
      pos: Position.T,
wenzelm@62631
   384
      name: String,
wenzelm@62631
   385
      groups: List[String],
wenzelm@62631
   386
      path: String,
wenzelm@62631
   387
      parent: Option[String],
wenzelm@62631
   388
      description: String,
wenzelm@62631
   389
      options: List[Options.Spec],
wenzelm@65420
   390
      imports: List[String],
wenzelm@65374
   391
      theories: List[(List[Options.Spec], List[(String, Boolean)])],
wenzelm@62631
   392
      files: List[String],
wenzelm@62631
   393
      document_files: List[(String, String)]) extends Entry
wenzelm@62631
   394
wenzelm@62631
   395
    private val chapter: Parser[Chapter] =
wenzelm@62631
   396
    {
wenzelm@62631
   397
      val chapter_name = atom("chapter name", _.is_name)
wenzelm@62631
   398
wenzelm@62631
   399
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
wenzelm@62631
   400
    }
wenzelm@62631
   401
wenzelm@62631
   402
    private val session_entry: Parser[Session_Entry] =
wenzelm@62631
   403
    {
wenzelm@62631
   404
      val session_name = atom("session name", _.is_name)
wenzelm@62631
   405
wenzelm@62631
   406
      val option =
wenzelm@62968
   407
        option_name ~ opt($$$("=") ~! option_value ^^
wenzelm@62968
   408
          { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@62631
   409
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
wenzelm@62631
   410
wenzelm@65374
   411
      val global =
wenzelm@65374
   412
        ($$$("(") ~! $$$(GLOBAL) ~ $$$(")")) ^^ { case _ => true } | success(false)
wenzelm@65374
   413
wenzelm@65374
   414
      val theory_entry =
wenzelm@65374
   415
        theory_name ~ global ^^ { case x ~ y => (x, y) }
wenzelm@65374
   416
wenzelm@62631
   417
      val theories =
wenzelm@65374
   418
        $$$(THEORIES) ~!
wenzelm@65374
   419
          ((options | success(Nil)) ~ rep(theory_entry)) ^^
wenzelm@65374
   420
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@62631
   421
wenzelm@62631
   422
      val document_files =
wenzelm@62631
   423
        $$$(DOCUMENT_FILES) ~!
wenzelm@62631
   424
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
wenzelm@62631
   425
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
wenzelm@62631
   426
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
wenzelm@62631
   427
wenzelm@62631
   428
      command(SESSION) ~!
wenzelm@62631
   429
        (position(session_name) ~
wenzelm@62631
   430
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   431
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
wenzelm@62631
   432
          ($$$("=") ~!
wenzelm@62631
   433
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
wenzelm@62631
   434
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
wenzelm@62631
   435
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@65420
   436
              (($$$(SESSIONS) ~! rep(session_name)  ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   437
              rep1(theories) ~
wenzelm@62631
   438
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   439
              (rep(document_files) ^^ (x => x.flatten))))) ^^
wenzelm@65420
   440
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i ~ j))) =>
wenzelm@65420
   441
            Session_Entry(pos, a, b, c, d, e, f, g, h, i, j) }
wenzelm@62631
   442
    }
wenzelm@62631
   443
wenzelm@62631
   444
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   445
    {
wenzelm@62631
   446
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
wenzelm@62631
   447
      {
wenzelm@62631
   448
        try {
wenzelm@62631
   449
          val name = entry.name
wenzelm@62631
   450
wenzelm@62631
   451
          if (name == "") error("Bad session name")
wenzelm@65360
   452
          if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session")
wenzelm@65360
   453
          if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session")
wenzelm@62631
   454
wenzelm@62631
   455
          val session_options = options ++ entry.options
wenzelm@62631
   456
wenzelm@62631
   457
          val theories =
wenzelm@65392
   458
            entry.theories.map({ case (opts, thys) => (session_options ++ opts, thys.map(_._1)) })
wenzelm@65374
   459
wenzelm@65374
   460
          val global_theories =
wenzelm@65374
   461
            for { (_, thys) <- entry.theories; (thy, global) <- thys if global }
wenzelm@65374
   462
            yield {
wenzelm@65374
   463
              val thy_name = Path.explode(thy).expand.base.implode
wenzelm@65374
   464
              if (Long_Name.is_qualified(thy_name))
wenzelm@65374
   465
                error("Bad qualified name for global theory " + quote(thy_name))
wenzelm@65374
   466
              else thy_name
wenzelm@65374
   467
            }
wenzelm@65374
   468
wenzelm@62631
   469
          val files = entry.files.map(Path.explode(_))
wenzelm@62631
   470
          val document_files =
wenzelm@62631
   471
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
wenzelm@62631
   472
wenzelm@62631
   473
          val meta_digest =
wenzelm@62631
   474
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
wenzelm@65420
   475
              entry.imports, entry.theories, entry.files, entry.document_files).toString)
wenzelm@62631
   476
wenzelm@62631
   477
          val info =
wenzelm@62631
   478
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
wenzelm@65420
   479
              entry.parent, entry.description, session_options, entry.imports, theories,
wenzelm@65420
   480
              global_theories, files, document_files, meta_digest)
wenzelm@62631
   481
wenzelm@62631
   482
          (name, info)
wenzelm@62631
   483
        }
wenzelm@62631
   484
        catch {
wenzelm@62631
   485
          case ERROR(msg) =>
wenzelm@62631
   486
            error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@62631
   487
              quote(entry.name) + Position.here(entry.pos))
wenzelm@62631
   488
        }
wenzelm@62631
   489
      }
wenzelm@62631
   490
wenzelm@62631
   491
      val root = dir + ROOT
wenzelm@62631
   492
      if (root.is_file) {
wenzelm@62631
   493
        val toks = Token.explode(root_syntax.keywords, File.read(root))
wenzelm@62631
   494
        val start = Token.Pos.file(root.implode)
wenzelm@62631
   495
wenzelm@62631
   496
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
wenzelm@62631
   497
          case Success(result, _) =>
wenzelm@62631
   498
            var entry_chapter = "Unsorted"
wenzelm@62631
   499
            val infos = new mutable.ListBuffer[(String, Info)]
wenzelm@62631
   500
            result.foreach {
wenzelm@62631
   501
              case Chapter(name) => entry_chapter = name
wenzelm@62631
   502
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
wenzelm@62631
   503
            }
wenzelm@62631
   504
            infos.toList
wenzelm@62631
   505
          case bad => error(bad.toString)
wenzelm@62631
   506
        }
wenzelm@62631
   507
      }
wenzelm@62631
   508
      else Nil
wenzelm@62631
   509
    }
wenzelm@62631
   510
  }
wenzelm@62631
   511
wenzelm@62631
   512
wenzelm@62635
   513
  /* load sessions from certain directories */
wenzelm@62631
   514
wenzelm@62631
   515
  private def is_session_dir(dir: Path): Boolean =
wenzelm@62631
   516
    (dir + ROOT).is_file || (dir + ROOTS).is_file
wenzelm@62631
   517
wenzelm@62631
   518
  private def check_session_dir(dir: Path): Path =
wenzelm@62631
   519
    if (is_session_dir(dir)) dir
wenzelm@62631
   520
    else error("Bad session root directory: " + dir.toString)
wenzelm@62631
   521
wenzelm@65415
   522
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): T =
wenzelm@62631
   523
  {
wenzelm@62635
   524
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62635
   525
      load_root(select, dir) ::: load_roots(select, dir)
wenzelm@62631
   526
wenzelm@62635
   527
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   528
      Parser.parse(options, select, dir)
wenzelm@62631
   529
wenzelm@62635
   530
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   531
    {
wenzelm@62631
   532
      val roots = dir + ROOTS
wenzelm@62631
   533
      if (roots.is_file) {
wenzelm@62631
   534
        for {
wenzelm@62631
   535
          line <- split_lines(File.read(roots))
wenzelm@62631
   536
          if !(line == "" || line.startsWith("#"))
wenzelm@62631
   537
          dir1 =
wenzelm@62631
   538
            try { check_session_dir(dir + Path.explode(line)) }
wenzelm@62631
   539
            catch {
wenzelm@62631
   540
              case ERROR(msg) =>
wenzelm@62631
   541
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
wenzelm@62631
   542
            }
wenzelm@62635
   543
          info <- load_dir(select, dir1)
wenzelm@62631
   544
        } yield info
wenzelm@62631
   545
      }
wenzelm@62631
   546
      else Nil
wenzelm@62631
   547
    }
wenzelm@62631
   548
wenzelm@62631
   549
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
wenzelm@62631
   550
    dirs.foreach(check_session_dir(_))
wenzelm@62631
   551
    select_dirs.foreach(check_session_dir(_))
wenzelm@62631
   552
wenzelm@65415
   553
    make(
wenzelm@62631
   554
      for {
wenzelm@62631
   555
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
wenzelm@62635
   556
        info <- load_dir(select, dir)
wenzelm@62631
   557
      } yield info)
wenzelm@62631
   558
  }
wenzelm@62632
   559
wenzelm@62632
   560
wenzelm@62637
   561
wenzelm@62704
   562
  /** heap file with SHA1 digest **/
wenzelm@62704
   563
wenzelm@62704
   564
  private val sha1_prefix = "SHA1:"
wenzelm@62704
   565
wenzelm@62704
   566
  def read_heap_digest(heap: Path): Option[String] =
wenzelm@62704
   567
  {
wenzelm@62704
   568
    if (heap.is_file) {
wenzelm@62704
   569
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
wenzelm@62704
   570
      try {
wenzelm@62704
   571
        val len = file.size
wenzelm@62704
   572
        val n = sha1_prefix.length + SHA1.digest_length
wenzelm@62704
   573
        if (len >= n) {
wenzelm@62704
   574
          file.position(len - n)
wenzelm@62704
   575
wenzelm@62704
   576
          val buf = ByteBuffer.allocate(n)
wenzelm@62704
   577
          var i = 0
wenzelm@62704
   578
          var m = 0
wenzelm@62704
   579
          do {
wenzelm@62704
   580
            m = file.read(buf)
wenzelm@62704
   581
            if (m != -1) i += m
wenzelm@62704
   582
          }
wenzelm@62704
   583
          while (m != -1 && n > i)
wenzelm@62704
   584
wenzelm@62704
   585
          if (i == n) {
wenzelm@62704
   586
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
wenzelm@62704
   587
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
wenzelm@62704
   588
            if (prefix == sha1_prefix) Some(s) else None
wenzelm@62704
   589
          }
wenzelm@62704
   590
          else None
wenzelm@62704
   591
        }
wenzelm@62704
   592
        else None
wenzelm@62704
   593
      }
wenzelm@62704
   594
      finally { file.close }
wenzelm@62704
   595
    }
wenzelm@62704
   596
    else None
wenzelm@62704
   597
  }
wenzelm@62704
   598
wenzelm@62704
   599
  def write_heap_digest(heap: Path): String =
wenzelm@62704
   600
    read_heap_digest(heap) match {
wenzelm@62704
   601
      case None =>
wenzelm@62704
   602
        val s = SHA1.digest(heap).rep
wenzelm@62704
   603
        File.append(heap, sha1_prefix + s)
wenzelm@62704
   604
        s
wenzelm@62704
   605
      case Some(s) => s
wenzelm@62704
   606
    }
wenzelm@62704
   607
wenzelm@62704
   608
wenzelm@62704
   609
wenzelm@62637
   610
  /** persistent store **/
wenzelm@62632
   611
wenzelm@65296
   612
  object Session_Info
wenzelm@65296
   613
  {
wenzelm@65326
   614
    val session_name = SQL.Column.string("session_name", primary_key = true)
wenzelm@65326
   615
wenzelm@65296
   616
    // Build_Log.Session_Info
wenzelm@65296
   617
    val session_timing = SQL.Column.bytes("session_timing")
wenzelm@65296
   618
    val command_timings = SQL.Column.bytes("command_timings")
wenzelm@65296
   619
    val ml_statistics = SQL.Column.bytes("ml_statistics")
wenzelm@65296
   620
    val task_statistics = SQL.Column.bytes("task_statistics")
wenzelm@65296
   621
    val build_log_columns =
wenzelm@65296
   622
      List(session_name, session_timing, command_timings, ml_statistics, task_statistics)
wenzelm@65296
   623
wenzelm@65296
   624
    // Build.Session_Info
wenzelm@65296
   625
    val sources = SQL.Column.string("sources")
wenzelm@65296
   626
    val input_heaps = SQL.Column.string("input_heaps")
wenzelm@65296
   627
    val output_heap = SQL.Column.string("output_heap")
wenzelm@65296
   628
    val return_code = SQL.Column.int("return_code")
wenzelm@65296
   629
    val build_columns = List(sources, input_heaps, output_heap, return_code)
wenzelm@65296
   630
wenzelm@65296
   631
    val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns)
wenzelm@65320
   632
wenzelm@65320
   633
    def where_session_name(name: String): String =
wenzelm@65322
   634
      "WHERE " + session_name.sql_name + " = " + SQL.quote_string(name)
wenzelm@65320
   635
wenzelm@65320
   636
    def select_statement(db: SQL.Database, name: String, columns: List[SQL.Column])
wenzelm@65320
   637
        : PreparedStatement =
wenzelm@65320
   638
      db.select_statement(table, columns, where_session_name(name))
wenzelm@65320
   639
wenzelm@65320
   640
    def delete_statement(db: SQL.Database, name: String): PreparedStatement =
wenzelm@65320
   641
      db.delete_statement(table, where_session_name(name))
wenzelm@65296
   642
  }
wenzelm@65296
   643
wenzelm@62632
   644
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
wenzelm@62632
   645
wenzelm@63996
   646
  class Store private[Sessions](system_mode: Boolean)
wenzelm@62632
   647
  {
wenzelm@65278
   648
    /* file names */
wenzelm@65278
   649
wenzelm@65281
   650
    def database(name: String): Path = Path.basic("log") + Path.basic(name).ext("db")
wenzelm@65278
   651
    def log(name: String): Path = Path.basic("log") + Path.basic(name)
wenzelm@65278
   652
    def log_gz(name: String): Path = log(name).ext("gz")
wenzelm@65278
   653
wenzelm@65278
   654
wenzelm@65286
   655
    /* SQL database content */
wenzelm@65283
   656
wenzelm@65283
   657
    val xml_cache: XML.Cache = new XML.Cache()
wenzelm@65283
   658
wenzelm@65283
   659
    def encode_properties(ps: Properties.T): Bytes =
wenzelm@65283
   660
      Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
wenzelm@65283
   661
wenzelm@65283
   662
    def decode_properties(bs: Bytes): Properties.T =
wenzelm@65283
   663
      xml_cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
wenzelm@65283
   664
wenzelm@65283
   665
    def compress_properties(ps: List[Properties.T], options: XZ.Options = XZ.options()): Bytes =
wenzelm@65283
   666
    {
wenzelm@65283
   667
      if (ps.isEmpty) Bytes.empty
wenzelm@65283
   668
      else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
wenzelm@65283
   669
    }
wenzelm@65283
   670
wenzelm@65283
   671
    def uncompress_properties(bs: Bytes): List[Properties.T] =
wenzelm@65283
   672
    {
wenzelm@65283
   673
      if (bs.isEmpty) Nil
wenzelm@65283
   674
      else
wenzelm@65283
   675
        XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text)).
wenzelm@65283
   676
          map(xml_cache.props(_))
wenzelm@65283
   677
    }
wenzelm@65283
   678
wenzelm@65320
   679
    def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes =
wenzelm@65320
   680
      using(Session_Info.select_statement(db, name, List(column)))(stmt =>
wenzelm@65291
   681
      {
wenzelm@65291
   682
        val rs = stmt.executeQuery
wenzelm@65324
   683
        if (!rs.next) Bytes.empty else db.bytes(rs, column)
wenzelm@65291
   684
      })
wenzelm@65285
   685
wenzelm@65320
   686
    def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] =
wenzelm@65320
   687
      uncompress_properties(read_bytes(db, name, column))
wenzelm@65286
   688
wenzelm@65283
   689
wenzelm@62637
   690
    /* output */
wenzelm@62632
   691
wenzelm@62632
   692
    val browser_info: Path =
wenzelm@62632
   693
      if (system_mode) Path.explode("~~/browser_info")
wenzelm@62632
   694
      else Path.explode("$ISABELLE_BROWSER_INFO")
wenzelm@62632
   695
wenzelm@62637
   696
    val output_dir: Path =
wenzelm@62637
   697
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
wenzelm@62637
   698
      else Path.explode("$ISABELLE_OUTPUT")
wenzelm@62637
   699
wenzelm@65298
   700
    override def toString: String = "Store(output_dir = " + output_dir.expand + ")"
wenzelm@65298
   701
wenzelm@62637
   702
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
wenzelm@62637
   703
wenzelm@62637
   704
wenzelm@62637
   705
    /* input */
wenzelm@62637
   706
wenzelm@62637
   707
    private val input_dirs =
wenzelm@62632
   708
      if (system_mode) List(output_dir)
wenzelm@62633
   709
      else {
wenzelm@62633
   710
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
wenzelm@62633
   711
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
wenzelm@62633
   712
      }
wenzelm@62632
   713
wenzelm@65291
   714
    def find_database_heap(name: String): Option[(Path, Option[String])] =
wenzelm@65291
   715
      input_dirs.find(dir => (dir + database(name)).is_file).map(dir =>
wenzelm@65291
   716
        (dir + database(name), read_heap_digest(dir + Path.basic(name))))
wenzelm@62632
   717
wenzelm@65281
   718
    def find_database(name: String): Option[Path] =
wenzelm@65281
   719
      input_dirs.map(_ + database(name)).find(_.is_file)
wenzelm@65281
   720
wenzelm@62637
   721
    def heap(name: String): Path =
wenzelm@65288
   722
      input_dirs.map(_ + Path.basic(name)).find(_.is_file) getOrElse
wenzelm@62637
   723
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
wenzelm@62769
   724
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
wenzelm@65287
   725
wenzelm@65287
   726
wenzelm@65296
   727
    /* session info */
wenzelm@65281
   728
wenzelm@65296
   729
    def write_session_info(
wenzelm@65318
   730
      db: SQL.Database,
wenzelm@65320
   731
      name: String,
wenzelm@65318
   732
      build_log: Build_Log.Session_Info,
wenzelm@65318
   733
      build: Build.Session_Info)
wenzelm@65281
   734
    {
wenzelm@65281
   735
      db.transaction {
wenzelm@65296
   736
        db.create_table(Session_Info.table)
wenzelm@65320
   737
        using(Session_Info.delete_statement(db, name))(_.execute)
wenzelm@65296
   738
        using(db.insert_statement(Session_Info.table))(stmt =>
wenzelm@65281
   739
        {
wenzelm@65320
   740
          db.set_string(stmt, 1, name)
wenzelm@65296
   741
          db.set_bytes(stmt, 2, encode_properties(build_log.session_timing))
wenzelm@65296
   742
          db.set_bytes(stmt, 3, compress_properties(build_log.command_timings))
wenzelm@65296
   743
          db.set_bytes(stmt, 4, compress_properties(build_log.ml_statistics))
wenzelm@65296
   744
          db.set_bytes(stmt, 5, compress_properties(build_log.task_statistics))
wenzelm@65284
   745
          db.set_string(stmt, 6, cat_lines(build.sources))
wenzelm@65284
   746
          db.set_string(stmt, 7, cat_lines(build.input_heaps))
wenzelm@65291
   747
          db.set_string(stmt, 8, build.output_heap getOrElse "")
wenzelm@65283
   748
          db.set_int(stmt, 9, build.return_code)
wenzelm@65281
   749
          stmt.execute()
wenzelm@65281
   750
        })
wenzelm@65281
   751
      }
wenzelm@65281
   752
    }
wenzelm@65281
   753
wenzelm@65320
   754
    def read_session_timing(db: SQL.Database, name: String): Properties.T =
wenzelm@65320
   755
      decode_properties(read_bytes(db, name, Session_Info.session_timing))
wenzelm@65286
   756
wenzelm@65320
   757
    def read_command_timings(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   758
      read_properties(db, name, Session_Info.command_timings)
wenzelm@65286
   759
wenzelm@65320
   760
    def read_ml_statistics(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   761
      read_properties(db, name, Session_Info.ml_statistics)
wenzelm@65286
   762
wenzelm@65320
   763
    def read_task_statistics(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   764
      read_properties(db, name, Session_Info.task_statistics)
wenzelm@65286
   765
wenzelm@65320
   766
    def read_build_log(db: SQL.Database, name: String,
wenzelm@65291
   767
      command_timings: Boolean = false,
wenzelm@65291
   768
      ml_statistics: Boolean = false,
wenzelm@65291
   769
      task_statistics: Boolean = false): Build_Log.Session_Info =
wenzelm@65291
   770
    {
wenzelm@65291
   771
      Build_Log.Session_Info(
wenzelm@65320
   772
        session_timing = read_session_timing(db, name),
wenzelm@65320
   773
        command_timings = if (command_timings) read_command_timings(db, name) else Nil,
wenzelm@65320
   774
        ml_statistics = if (ml_statistics) read_ml_statistics(db, name) else Nil,
wenzelm@65320
   775
        task_statistics = if (task_statistics) read_task_statistics(db, name) else Nil)
wenzelm@65291
   776
    }
wenzelm@65285
   777
wenzelm@65320
   778
    def read_build(db: SQL.Database, name: String): Option[Build.Session_Info] =
wenzelm@65320
   779
      using(Session_Info.select_statement(db, name, Session_Info.build_columns))(stmt =>
wenzelm@65285
   780
      {
wenzelm@65285
   781
        val rs = stmt.executeQuery
wenzelm@65285
   782
        if (!rs.next) None
wenzelm@65285
   783
        else {
wenzelm@65285
   784
          Some(
wenzelm@65281
   785
            Build.Session_Info(
wenzelm@65324
   786
              split_lines(db.string(rs, Session_Info.sources)),
wenzelm@65324
   787
              split_lines(db.string(rs, Session_Info.input_heaps)),
wenzelm@65324
   788
              db.string(rs, Session_Info.output_heap) match { case "" => None case s => Some(s) },
wenzelm@65324
   789
              db.int(rs, Session_Info.return_code)))
wenzelm@65281
   790
        }
wenzelm@65281
   791
      })
wenzelm@65281
   792
  }
wenzelm@62631
   793
}