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