src/Pure/Thy/export.scala
author wenzelm
Sat, 20 Aug 2022 12:39:37 +0200
changeset 75918 16a53676ebbd
parent 75901 475fedc02737
child 75920 27bf2533f4a4
permissions -rw-r--r--
tuned whitespace;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/export.scala
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     3
68102
813b5d0904c6 clarified signature;
wenzelm
parents: 68101
diff changeset
     4
Manage theory exports: compressed blobs.
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     5
*/
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     6
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     7
package isabelle
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
     8
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
     9
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    10
import scala.annotation.tailrec
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    11
import scala.util.matching.Regex
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    12
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
    14
object Export {
72691
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
    15
  /* artefact names */
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
    16
72844
240c8a0f6337 clarified exports;
wenzelm
parents: 72816
diff changeset
    17
  val DOCUMENT_ID = "PIDE/document_id"
240c8a0f6337 clarified exports;
wenzelm
parents: 72816
diff changeset
    18
  val FILES = "PIDE/files"
72702
79a19657c170 clarified names;
wenzelm
parents: 72691
diff changeset
    19
  val MARKUP = "PIDE/markup"
79a19657c170 clarified names;
wenzelm
parents: 72691
diff changeset
    20
  val MESSAGES = "PIDE/messages"
72691
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
    21
  val DOCUMENT_PREFIX = "document/"
73785
b5fb99b985b4 clarified document export names;
wenzelm
parents: 73693
diff changeset
    22
  val DOCUMENT_LATEX = DOCUMENT_PREFIX + "latex"
b5fb99b985b4 clarified document export names;
wenzelm
parents: 73693
diff changeset
    23
  val DOCUMENT_CITATIONS = DOCUMENT_PREFIX + "citations"
72691
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
    24
  val THEORY_PREFIX: String = "theory/"
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
    25
  val PROOFS_PREFIX: String = "proofs/"
69634
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
    26
69756
1907222d974e clarified modules;
wenzelm
parents: 69671
diff changeset
    27
  def explode_name(s: String): List[String] = space_explode('/', s)
1907222d974e clarified modules;
wenzelm
parents: 69671
diff changeset
    28
  def implode_name(elems: Iterable[String]): String = elems.mkString("/")
69634
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
    29
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
    30
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    31
  /* SQL data model */
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    32
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
    33
  object Data {
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    34
    val session_name = SQL.Column.string("session_name").make_primary_key
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    35
    val theory_name = SQL.Column.string("theory_name").make_primary_key
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    36
    val name = SQL.Column.string("name").make_primary_key
69788
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
    37
    val executable = SQL.Column.bool("executable")
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    38
    val compressed = SQL.Column.bool("compressed")
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    39
    val body = SQL.Column.bytes("body")
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    40
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    41
    val table =
69788
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
    42
      SQL.Table("isabelle_exports",
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
    43
        List(session_name, theory_name, name, executable, compressed, body))
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    44
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    45
    def where_equal(session_name: String, theory_name: String = "", name: String = ""): SQL.Source =
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    46
      "WHERE " + Data.session_name.equal(session_name) +
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    47
        (if (theory_name == "") "" else " AND " + Data.theory_name.equal(theory_name)) +
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    48
        (if (name == "") "" else " AND " + Data.name.equal(name))
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    49
  }
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
    50
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    51
  def compound_name(a: String, b: String): String =
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    52
    if (a.isEmpty) b else a + ":" + b
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    53
75672
88598f7c9614 clarified signature;
wenzelm
parents: 75671
diff changeset
    54
  sealed case class Entry_Name(session: String = "", theory: String = "", name: String = "") {
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    55
    val compound_name: String = Export.compound_name(theory, name)
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    56
75680
605f4b6b5785 clarified signature;
wenzelm
parents: 75675
diff changeset
    57
    def make_path(prune: Int = 0): Path = {
75675
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    58
      val elems = theory :: space_explode('/', name)
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    59
      if (elems.length < prune + 1) {
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    60
        error("Cannot prune path by " + prune + " element(s): " + Path.make(elems))
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    61
      }
75680
605f4b6b5785 clarified signature;
wenzelm
parents: 75675
diff changeset
    62
      else Path.make(elems.drop(prune))
75675
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    63
    }
abd4db50ff1e clarified signature;
wenzelm
parents: 75674
diff changeset
    64
75671
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    65
    def readable(db: SQL.Database): Boolean = {
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    66
      val select = Data.table.select(List(Data.name), Data.where_equal(session, theory, name))
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    67
      db.using_statement(select)(stmt => stmt.execute_query().next())
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    68
    }
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    69
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    70
    def read(db: SQL.Database, cache: XML.Cache): Option[Entry] = {
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    71
      val select =
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    72
        Data.table.select(List(Data.executable, Data.compressed, Data.body),
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    73
          Data.where_equal(session, theory, name))
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    74
      db.using_statement(select) { stmt =>
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    75
        val res = stmt.execute_query()
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    76
        if (res.next()) {
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    77
          val executable = res.bool(Data.executable)
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    78
          val compressed = res.bool(Data.compressed)
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    79
          val bytes = res.bytes(Data.body)
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    80
          val body = Future.value(compressed, bytes)
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    81
          Some(Entry(this, executable, body, cache))
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    82
        }
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    83
        else None
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    84
      }
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
    85
    }
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    86
  }
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
    87
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
    88
  def read_theory_names(db: SQL.Database, session_name: String): List[String] = {
68222
3c1a716e7f59 tuned queries;
wenzelm
parents: 68221
diff changeset
    89
    val select =
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
    90
      Data.table.select(List(Data.theory_name), Data.where_equal(session_name), distinct = true) +
75767
87f9748b214a clarified signature: persistent theory_names in lexical order;
wenzelm
parents: 75766
diff changeset
    91
      " ORDER BY " + Data.theory_name
68222
3c1a716e7f59 tuned queries;
wenzelm
parents: 68221
diff changeset
    92
    db.using_statement(select)(stmt =>
3c1a716e7f59 tuned queries;
wenzelm
parents: 68221
diff changeset
    93
      stmt.execute_query().iterator(_.string(Data.theory_name)).toList)
3c1a716e7f59 tuned queries;
wenzelm
parents: 68221
diff changeset
    94
  }
3c1a716e7f59 tuned queries;
wenzelm
parents: 68221
diff changeset
    95
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
    96
  def read_entry_names(db: SQL.Database, session_name: String): List[Entry_Name] = {
75658
5905c7f484b3 clarified signature: read_theory_exports is already ordered;
wenzelm
parents: 75394
diff changeset
    97
    val select =
5905c7f484b3 clarified signature: read_theory_exports is already ordered;
wenzelm
parents: 75394
diff changeset
    98
      Data.table.select(List(Data.theory_name, Data.name), Data.where_equal(session_name)) +
5905c7f484b3 clarified signature: read_theory_exports is already ordered;
wenzelm
parents: 75394
diff changeset
    99
      " ORDER BY " + Data.theory_name + ", " + Data.name
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   100
    db.using_statement(select)(stmt =>
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   101
      stmt.execute_query().iterator(res =>
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   102
        Entry_Name(session = session_name,
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   103
          theory = res.string(Data.theory_name),
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   104
          name = res.string(Data.name))).toList)
68115
23c6ae3dd3a0 more efficient query;
wenzelm
parents: 68104
diff changeset
   105
  }
23c6ae3dd3a0 more efficient query;
wenzelm
parents: 68104
diff changeset
   106
68104
wenzelm
parents: 68103
diff changeset
   107
  def message(msg: String, theory_name: String, name: String): String =
wenzelm
parents: 68103
diff changeset
   108
    msg + " " + quote(name) + " for theory " + quote(theory_name)
wenzelm
parents: 68103
diff changeset
   109
72854
6c660f05f70c clarified signature;
wenzelm
parents: 72847
diff changeset
   110
  def empty_entry(theory_name: String, name: String): Entry =
75672
88598f7c9614 clarified signature;
wenzelm
parents: 75671
diff changeset
   111
    Entry(Entry_Name(theory = theory_name, name = name),
88598f7c9614 clarified signature;
wenzelm
parents: 75671
diff changeset
   112
      false, Future.value(false, Bytes.empty), XML.Cache.none)
72634
5cea0993ee4f clarified access to single database server vs. collection of database files;
wenzelm
parents: 72375
diff changeset
   113
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   114
  sealed case class Entry(
75671
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   115
    entry_name: Entry_Name,
69788
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
   116
    executable: Boolean,
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   117
    body: Future[(Boolean, Bytes)],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   118
    cache: XML.Cache
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   119
  ) {
75671
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   120
    def session_name: String = entry_name.session
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   121
    def theory_name: String = entry_name.theory
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   122
    def name: String = entry_name.name
69635
95dc926fa39c clarified output (again);
wenzelm
parents: 69634
diff changeset
   123
    override def toString: String = name
69630
aaa0b5f571e8 clarified output;
wenzelm
parents: 69629
diff changeset
   124
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   125
    def compound_name: String = entry_name.compound_name
71141
b1c555d3cd71 tuned signature -- more operations;
wenzelm
parents: 71014
diff changeset
   126
72691
2126cf946086 clarified signature;
wenzelm
parents: 72683
diff changeset
   127
    def name_has_prefix(s: String): Boolean = name.startsWith(s)
69634
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
   128
    val name_elems: List[String] = explode_name(name)
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
   129
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
   130
    def name_extends(elems: List[String]): Boolean =
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
   131
      name_elems.startsWith(elems) && name_elems != elems
70f1994988d4 more operations;
wenzelm
parents: 69630
diff changeset
   132
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   133
    def text: String = uncompressed.text
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   134
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   135
    def uncompressed: Bytes = {
69629
wenzelm
parents: 68924
diff changeset
   136
      val (compressed, bytes) = body.join
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
   137
      if (compressed) bytes.uncompress(cache = cache.xz) else bytes
69629
wenzelm
parents: 68924
diff changeset
   138
    }
wenzelm
parents: 68924
diff changeset
   139
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   140
    def uncompressed_yxml: XML.Body =
73033
d2690444c00a clarified caching;
wenzelm
parents: 73031
diff changeset
   141
      YXML.parse_body(UTF8.decode_permissive(uncompressed), cache = cache)
69629
wenzelm
parents: 68924
diff changeset
   142
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   143
    def write(db: SQL.Database): Unit = {
68167
327bb0f5f768 clarified implicit compression;
wenzelm
parents: 68166
diff changeset
   144
      val (compressed, bytes) = body.join
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   145
      db.using_statement(Data.table.insert()) { stmt =>
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   146
        stmt.string(1) = session_name
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   147
        stmt.string(2) = theory_name
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   148
        stmt.string(3) = name
69788
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
   149
        stmt.bool(4) = executable
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
   150
        stmt.bool(5) = compressed
c175499a7537 added executable flag for exports;
wenzelm
parents: 69756
diff changeset
   151
        stmt.bytes(6) = bytes
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   152
        stmt.execute()
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   153
      }
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   154
    }
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   155
  }
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   156
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   157
  def make_regex(pattern: String): Regex = {
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   158
    @tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex =
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   159
      chs match {
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   160
        case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   161
        case '*' :: rest => make("[^:/]*" :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   162
        case '?' :: rest => make("[^:/]" :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   163
        case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   164
        case '{' :: rest => make("(" :: result, depth + 1, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   165
        case ',' :: rest if depth > 0 => make("|" :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   166
        case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   167
        case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   168
        case c :: rest => make(c.toString :: result, depth, rest)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   169
        case Nil => result.reverse.mkString.r
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   170
      }
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   171
    make(Nil, 0, pattern.toList)
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   172
  }
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   173
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   174
  def make_matcher(pats: List[String]): Entry_Name => Boolean = {
75658
5905c7f484b3 clarified signature: read_theory_exports is already ordered;
wenzelm
parents: 75394
diff changeset
   175
    val regs = pats.map(make_regex)
75674
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   176
    (entry_name: Entry_Name) =>
c8e6078fee73 clarified signature;
wenzelm
parents: 75673
diff changeset
   177
      regs.exists(_.pattern.matcher(entry_name.compound_name).matches)
68151
wenzelm
parents: 68116
diff changeset
   178
  }
wenzelm
parents: 68116
diff changeset
   179
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   180
  def make_entry(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   181
    session_name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   182
    args: Protocol.Export.Args,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   183
    bytes: Bytes,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   184
    cache: XML.Cache
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   185
  ): Entry = {
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   186
    val body =
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
   187
      if (args.compress) Future.fork(bytes.maybe_compress(cache = cache.xz))
72847
9dda93a753b1 clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents: 72844
diff changeset
   188
      else Future.value((false, bytes))
75672
88598f7c9614 clarified signature;
wenzelm
parents: 75671
diff changeset
   189
    val entry_name = Entry_Name(session = session_name, theory = args.theory_name, name = args.name)
75671
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   190
    Entry(entry_name, args.executable, body, cache)
68831
a6c69599ab99 Export.Provider for "isabelle dump" output_dir;
wenzelm
parents: 68418
diff changeset
   191
  }
a6c69599ab99 Export.Provider for "isabelle dump" output_dir;
wenzelm
parents: 68418
diff changeset
   192
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   193
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   194
  /* database consumer thread */
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   195
74255
e117e0c29204 more reactive interrupt;
wenzelm
parents: 74215
diff changeset
   196
  def consumer(db: SQL.Database, cache: XML.Cache, progress: Progress = new Progress): Consumer =
e117e0c29204 more reactive interrupt;
wenzelm
parents: 74215
diff changeset
   197
    new Consumer(db, cache, progress)
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   198
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   199
  class Consumer private[Export](db: SQL.Database, cache: XML.Cache, progress: Progress) {
68924
wenzelm
parents: 68832
diff changeset
   200
    private val errors = Synchronized[List[String]](Nil)
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   201
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   202
    private val consumer =
71145
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   203
      Consumer_Thread.fork_bulk[(Entry, Boolean)](name = "export")(
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   204
        bulk = { case (entry, _) => entry.body.is_finished },
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   205
        consume =
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   206
          { (args: List[(Entry, Boolean)]) =>
71145
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   207
            val results =
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   208
              db.transaction {
74256
0ba3952f409a more robust: retain length of results;
wenzelm
parents: 74255
diff changeset
   209
                for ((entry, strict) <- args)
71145
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   210
                yield {
74257
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   211
                  if (progress.stopped) {
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   212
                    entry.body.cancel()
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   213
                    Exn.Res(())
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   214
                  }
75671
ca8ae1ffd2b8 tuned signature: more explicit types;
wenzelm
parents: 75658
diff changeset
   215
                  else if (entry.entry_name.readable(db)) {
71145
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   216
                    if (strict) {
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   217
                      val msg = message("Duplicate export", entry.theory_name, entry.name)
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   218
                      errors.change(msg :: _)
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   219
                    }
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   220
                    Exn.Res(())
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   221
                  }
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   222
                  else Exn.capture { entry.write(db) }
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   223
                }
70499
f389019024ce allow duplicate exports via strict = false;
wenzelm
parents: 69854
diff changeset
   224
              }
71145
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   225
            (results, true)
2f782d5f5d5a improved performance of session exports via bulk transactions;
wenzelm
parents: 71141
diff changeset
   226
          })
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   227
75762
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75759
diff changeset
   228
    def make_entry(session_name: String, args: Protocol.Export.Args, body: Bytes): Unit = {
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75759
diff changeset
   229
      if (!progress.stopped && !body.is_empty) {
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75759
diff changeset
   230
        consumer.send(Export.make_entry(session_name, args, body, cache) -> args.strict)
74257
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   231
      }
bda7a7b3bd41 more reactive interrupt;
wenzelm
parents: 74256
diff changeset
   232
    }
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   233
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   234
    def shutdown(close: Boolean = false): List[String] = {
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   235
      consumer.shutdown()
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   236
      if (close) db.close()
74255
e117e0c29204 more reactive interrupt;
wenzelm
parents: 74215
diff changeset
   237
      errors.value.reverse ::: (if (progress.stopped) List("Export stopped") else Nil)
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   238
    }
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   239
  }
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   240
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   241
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   242
  /* context for database access */
75772
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   243
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   244
  def open_database_context(store: Sessions.Store): Database_Context = {
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   245
    val database_server = if (store.database_server) Some(store.open_database_server()) else None
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   246
    new Database_Context(store, database_server)
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   247
  }
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   248
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   249
  def open_session_context0(store: Sessions.Store, session: String): Session_Context =
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   250
    open_database_context(store).open_session0(session, close_database_context = true)
75772
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   251
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   252
  def open_session_context(
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   253
    store: Sessions.Store,
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   254
    session_base_info: Sessions.Base_Info,
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   255
    document_snapshot: Option[Document.Snapshot] = None
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   256
  ): Session_Context = {
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   257
    open_database_context(store).open_session(
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   258
      session_base_info, document_snapshot = document_snapshot, close_database_context = true)
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   259
  }
75771
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   260
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   261
  class Database_Context private[Export](
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   262
    val store: Sessions.Store,
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   263
    val database_server: Option[SQL.Database]
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   264
  ) extends AutoCloseable {
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   265
    database_context =>
75782
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   266
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   267
    override def toString: String = {
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   268
      val s =
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   269
        database_server match {
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   270
          case Some(db) => db.toString
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   271
          case None => "input_dirs = " + store.input_dirs.map(_.absolute).mkString(", ")
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   272
        }
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   273
      "Database_Context(" + s + ")"
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   274
    }
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   275
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   276
    def cache: Term.Cache = store.cache
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   277
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   278
    def close(): Unit = database_server.foreach(_.close())
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   279
75787
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   280
    def open_output_database(session: String): Session_Database =
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   281
      database_server match {
75787
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   282
        case Some(db) => new Session_Database(session, db)
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   283
        case None =>
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   284
          new Session_Database(session, store.open_database(session, output = true)) {
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   285
            override def close(): Unit = db.close()
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   286
          }
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   287
      }
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   288
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   289
    def open_session0(session: String, close_database_context: Boolean = false): Session_Context =
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   290
      open_session(Sessions.base_info0(session), close_database_context = close_database_context)
75779
5470c67bd772 clarified signature: prefer Export.Session_Context;
wenzelm
parents: 75778
diff changeset
   291
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   292
    def open_session(
75773
11b2bf6f90d8 clarified signature: less redundant -- Sessions.Base_Info already specifies the main session;
wenzelm
parents: 75772
diff changeset
   293
      session_base_info: Sessions.Base_Info,
75772
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   294
      document_snapshot: Option[Document.Snapshot] = None,
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   295
      close_database_context: Boolean = false
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   296
    ): Session_Context = {
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   297
      val session_name = session_base_info.check.base.session_name
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   298
      val session_hierarchy = session_base_info.sessions_structure.build_hierarchy(session_name)
75773
11b2bf6f90d8 clarified signature: less redundant -- Sessions.Base_Info already specifies the main session;
wenzelm
parents: 75772
diff changeset
   299
      val session_databases =
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   300
        database_server match {
75771
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   301
          case Some(db) => session_hierarchy.map(name => new Session_Database(name, db))
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   302
          case None =>
75775
70a65ee4a738 clarified signature: more robust treatment of server;
wenzelm
parents: 75774
diff changeset
   303
            val attempts =
70a65ee4a738 clarified signature: more robust treatment of server;
wenzelm
parents: 75774
diff changeset
   304
              session_hierarchy.map(name => name -> store.try_open_database(name, server = false))
75771
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   305
            attempts.collectFirst({ case (name, None) => name }) match {
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   306
              case Some(bad) =>
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   307
                for ((_, Some(db)) <- attempts) db.close()
75791
fb12433208aa tuned signature;
wenzelm
parents: 75790
diff changeset
   308
                store.error_database(bad)
75764
07e097f60b85 clarified signature: more robust close operation;
wenzelm
parents: 75762
diff changeset
   309
              case None =>
75771
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   310
                for ((name, Some(db)) <- attempts) yield {
75764
07e097f60b85 clarified signature: more robust close operation;
wenzelm
parents: 75762
diff changeset
   311
                  new Session_Database(name, db) { override def close(): Unit = this.db.close() }
07e097f60b85 clarified signature: more robust close operation;
wenzelm
parents: 75762
diff changeset
   312
                }
07e097f60b85 clarified signature: more robust close operation;
wenzelm
parents: 75762
diff changeset
   313
            }
75771
26b71e1dd262 misc tuning and clarification;
wenzelm
parents: 75770
diff changeset
   314
        }
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   315
      new Session_Context(database_context, session_base_info, session_databases, document_snapshot) {
75772
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   316
        override def close(): Unit = {
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   317
          session_databases.foreach(_.close())
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   318
          if (close_database_context) database_context.close()
75772
9dbcc4c66e1c tuned signature: more operations;
wenzelm
parents: 75771
diff changeset
   319
        }
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   320
      }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   321
    }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   322
  }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   323
75789
wenzelm
parents: 75788
diff changeset
   324
  class Session_Database private[Export](val session: String, val db: SQL.Database)
wenzelm
parents: 75788
diff changeset
   325
  extends AutoCloseable {
wenzelm
parents: 75788
diff changeset
   326
    def close(): Unit = ()
wenzelm
parents: 75788
diff changeset
   327
wenzelm
parents: 75788
diff changeset
   328
    lazy private [Export] val theory_names: List[String] = read_theory_names(db, session)
wenzelm
parents: 75788
diff changeset
   329
    lazy private [Export] val entry_names: List[Entry_Name] = read_entry_names(db, session)
wenzelm
parents: 75788
diff changeset
   330
  }
wenzelm
parents: 75788
diff changeset
   331
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   332
  class Session_Context private[Export](
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   333
    val database_context: Database_Context,
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   334
    session_base_info: Sessions.Base_Info,
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   335
    db_hierarchy: List[Session_Database],
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   336
    document_snapshot: Option[Document.Snapshot]
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   337
  ) extends AutoCloseable {
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   338
    session_context =>
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   339
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   340
    def close(): Unit = ()
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   341
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75784
diff changeset
   342
    def cache: Term.Cache = database_context.cache
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   343
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   344
    def sessions_structure: Sessions.Structure = session_base_info.sessions_structure
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   345
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   346
    def session_base: Sessions.Base = session_base_info.base
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   347
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   348
    def session_name: String =
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   349
      if (document_snapshot.isDefined) Sessions.DRAFT
75773
11b2bf6f90d8 clarified signature: less redundant -- Sessions.Base_Info already specifies the main session;
wenzelm
parents: 75772
diff changeset
   350
      else session_base.session_name
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   351
75780
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   352
    def session_database(session: String = session_name): Option[Session_Database] =
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   353
      db_hierarchy.find(_.session == session)
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   354
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   355
    def session_db(session: String = session_name): Option[SQL.Database] =
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   356
      session_database(session = session).map(_.db)
f49c4f160b84 clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents: 75779
diff changeset
   357
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   358
    def session_stack: List[String] =
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   359
      ((if (document_snapshot.isDefined) List(session_name) else Nil) :::
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   360
        db_hierarchy.map(_.session)).reverse
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   361
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   362
    private def select[A](
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   363
      session: String,
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   364
      select: Session_Database => List[A],
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   365
      project: Entry_Name => A,
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   366
      sort_key: A => String
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   367
    ): List[A] = {
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   368
      def result(name: String): List[A] =
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   369
        if (name == Sessions.DRAFT) {
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   370
          (for {
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   371
            snapshot <- document_snapshot.iterator
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   372
            entry_name <- snapshot.all_exports.keysIterator
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   373
          } yield project(entry_name)).toSet.toList.sortBy(sort_key)
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   374
        }
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   375
        else session_database(name).map(select).getOrElse(Nil)
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   376
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   377
      if (session.nonEmpty) result(session) else session_stack.flatMap(result)
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   378
    }
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   379
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   380
    def entry_names(session: String = session_name): List[Entry_Name] =
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   381
      select(session, _.entry_names, identity, _.compound_name)
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   382
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   383
    def theory_names(session: String = session_name): List[String] =
75860
2b2c09f4e7b5 proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents: 75825
diff changeset
   384
      select(session, _.theory_names, _.theory, identity)
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   385
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   386
    def get(theory: String, name: String): Option[Entry] =
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   387
    {
75784
wenzelm
parents: 75782
diff changeset
   388
      def snapshot_entry: Option[Entry] =
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   389
        for {
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   390
          snapshot <- document_snapshot
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   391
          entry_name = Entry_Name(session = Sessions.DRAFT, theory = theory, name = name)
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   392
          entry <- snapshot.all_exports.get(entry_name)
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   393
        } yield entry
75784
wenzelm
parents: 75782
diff changeset
   394
      def db_entry: Option[Entry] =
wenzelm
parents: 75782
diff changeset
   395
        db_hierarchy.view.map(database =>
wenzelm
parents: 75782
diff changeset
   396
          Export.Entry_Name(session = database.session, theory = theory, name = name)
wenzelm
parents: 75782
diff changeset
   397
            .read(database.db, cache))
75764
07e097f60b85 clarified signature: more robust close operation;
wenzelm
parents: 75762
diff changeset
   398
          .collectFirst({ case Some(entry) => entry })
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   399
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   400
      snapshot_entry orElse db_entry
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   401
    }
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   402
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   403
    def apply(theory: String, name: String, permissive: Boolean = false): Entry =
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   404
      get(theory, name) match {
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   405
        case None if permissive => empty_entry(theory, name)
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   406
        case None => error("Missing export entry " + quote(compound_name(theory, name)))
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   407
        case Some(entry) => entry
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   408
      }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   409
75790
0ab8a9177e41 clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents: 75789
diff changeset
   410
    def theory(theory: String, other_cache: Option[Term.Cache] = None): Theory_Context =
0ab8a9177e41 clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents: 75789
diff changeset
   411
      new Theory_Context(session_context, theory, other_cache)
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   412
75825
ad00fbf64bff clarified signature --- simplified types;
wenzelm
parents: 75824
diff changeset
   413
    def classpath(): List[File.Content] = {
75782
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   414
      (for {
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   415
        session <- session_stack.iterator
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   416
        info <- sessions_structure.get(session).iterator
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   417
        if info.export_classpath.nonEmpty
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   418
        matcher = make_matcher(info.export_classpath)
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   419
        entry_name <- entry_names(session = session).iterator
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   420
        if matcher(entry_name)
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   421
        entry <- get(entry_name.theory, entry_name.name).iterator
75824
a2b2e8964e1a tuned signature;
wenzelm
parents: 75791
diff changeset
   422
      } yield File.content(entry.entry_name.make_path(), entry.uncompressed)).toList
75918
16a53676ebbd tuned whitespace;
wenzelm
parents: 75901
diff changeset
   423
    }
75782
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   424
75918
16a53676ebbd tuned whitespace;
wenzelm
parents: 75901
diff changeset
   425
    override def toString: String =
75770
62e2c6f65f9a clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents: 75769
diff changeset
   426
      "Export.Session_Context(" + commas_quote(session_stack) + ")"
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   427
  }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   428
75774
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   429
  class Theory_Context private[Export](
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   430
    val session_context: Session_Context,
75790
0ab8a9177e41 clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents: 75789
diff changeset
   431
    val theory: String,
0ab8a9177e41 clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents: 75789
diff changeset
   432
    other_cache: Option[Term.Cache]
75774
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   433
  ) {
75790
0ab8a9177e41 clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents: 75789
diff changeset
   434
    def cache: Term.Cache = other_cache getOrElse session_context.cache
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   435
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   436
    def get(name: String): Option[Entry] = session_context.get(theory, name)
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   437
    def apply(name: String, permissive: Boolean = false): Entry =
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   438
      session_context.apply(theory, name, permissive = permissive)
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   439
75774
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   440
    def uncompressed_yxml(name: String): XML.Body =
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   441
      get(name) match {
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   442
        case Some(entry) => entry.uncompressed_yxml
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   443
        case None => Nil
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   444
      }
efc25bf4b795 discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents: 75773
diff changeset
   445
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   446
    def document_id(): Option[Long] =
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   447
      apply(DOCUMENT_ID, permissive = true).text match {
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   448
        case Value.Long(id) => Some(id)
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   449
        case _ => None
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   450
      }
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   451
75901
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   452
    def files0(permissive: Boolean = false): List[String] =
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   453
      split_lines(apply(FILES, permissive = permissive).text)
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   454
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   455
    def files(permissive: Boolean = false): Option[(String, List[String])] =
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   456
      files0(permissive = permissive) match {
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   457
        case Nil => None
75901
475fedc02737 clarified signature;
wenzelm
parents: 75860
diff changeset
   458
        case a :: bs => Some((a, bs))
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   459
      }
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75775
diff changeset
   460
75759
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   461
    override def toString: String = "Export.Theory_Context(" + quote(theory) + ")"
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   462
  }
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   463
0cdccd0d1699 clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents: 75757
diff changeset
   464
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   465
  /* export to file-system */
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   466
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   467
  def export_files(
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   468
    store: Sessions.Store,
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   469
    session_name: String,
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   470
    export_dir: Path,
71726
a5fda30edae2 clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents: 71624
diff changeset
   471
    progress: Progress = new Progress,
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69635
diff changeset
   472
    export_prune: Int = 0,
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   473
    export_list: Boolean = false,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   474
    export_patterns: List[String] = Nil
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74686
diff changeset
   475
  ): Unit = {
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   476
    using(store.open_database(session_name)) { db =>
75739
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   477
      val entry_names = read_entry_names(db, session_name)
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   478
75739
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   479
      // list
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   480
      if (export_list) {
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   481
        for (entry_name <- entry_names) progress.echo(entry_name.compound_name)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   482
      }
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   483
75739
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   484
      // export
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   485
      if (export_patterns.nonEmpty) {
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   486
        val matcher = make_matcher(export_patterns)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   487
        for {
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   488
          entry_name <- entry_names if matcher(entry_name)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   489
          entry <- entry_name.read(db, store.cache)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   490
        } {
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   491
          val path = export_dir + entry_name.make_path(prune = export_prune)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   492
          progress.echo("export " + path + (if (entry.executable) " (executable)" else ""))
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   493
          Isabelle_System.make_directory(path.dir)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   494
          val bytes = entry.uncompressed
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   495
          if (!path.is_file || Bytes.read(path) != bytes) Bytes.write(path, bytes)
5b37466c1463 removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents: 75736
diff changeset
   496
          File.set_executable(path, entry.executable)
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   497
        }
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   498
      }
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   499
    }
68288
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   500
  }
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   501
d20770229f99 tuned signature;
wenzelm
parents: 68222
diff changeset
   502
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   503
  /* Isabelle tool wrapper */
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   504
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71145
diff changeset
   505
  val default_export_dir: Path = Path.explode("export")
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   506
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   507
  val isabelle_tool =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   508
    Isabelle_Tool("export", "retrieve theory exports", Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   509
      { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   510
        /* arguments */
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   511
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   512
        var export_dir = default_export_dir
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   513
        var dirs: List[Path] = Nil
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   514
        var export_list = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   515
        var no_build = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   516
        var options = Options.init()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   517
        var export_prune = 0
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   518
        var export_patterns: List[String] = Nil
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   519
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   520
        val getopts = Getopts("""
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   521
Usage: isabelle export [OPTIONS] SESSION
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   522
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   523
  Options are:
68314
2acbf8129d8b clarified option -O: avoid conflict with build/dump option -D;
wenzelm
parents: 68305
diff changeset
   524
    -O DIR       output directory for exported files (default: """ + default_export_dir + """)
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   525
    -d DIR       include session directory
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   526
    -l           list exports
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   527
    -n           no build of session
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   528
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69635
diff changeset
   529
    -p NUM       prune path of exported files by NUM elements
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   530
    -x PATTERN   extract files matching pattern (e.g. "*:**" for all)
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   531
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   532
  List or export theory exports for SESSION: named blobs produced by
68290
f1f5ccc85b25 support multiple patterns;
wenzelm
parents: 68289
diff changeset
   533
  isabelle build. Option -l or -x is required; option -x may be repeated.
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   534
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   535
  The PATTERN language resembles glob patterns in the shell, with ? and *
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   536
  (both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc],
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   537
  and variants {pattern1,pattern2,pattern3}.
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   538
""",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   539
          "O:" -> (arg => export_dir = Path.explode(arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   540
          "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   541
          "l" -> (_ => export_list = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   542
          "n" -> (_ => no_build = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   543
          "o:" -> (arg => options = options + arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   544
          "p:" -> (arg => export_prune = Value.Int.parse(arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   545
          "x:" -> (arg => export_patterns ::= arg))
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   546
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   547
        val more_args = getopts(args)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   548
        val session_name =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   549
          more_args match {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   550
            case List(session_name) if export_list || export_patterns.nonEmpty => session_name
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   551
            case _ => getopts.usage()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   552
          }
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   553
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   554
        val progress = new Console_Progress()
68305
5321218147d3 clarified signature;
wenzelm
parents: 68291
diff changeset
   555
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   556
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   557
        /* build */
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   558
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   559
        if (!no_build) {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   560
          val rc =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   561
            progress.interrupt_handler {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   562
              Build.build_logic(options, session_name, progress = progress, dirs = dirs)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   563
            }
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   564
          if (rc != Process_Result.RC.ok) sys.exit(rc)
68331
7eaaa8f48331 clarified outermost progress.interrupt_handler;
wenzelm
parents: 68314
diff changeset
   565
        }
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   566
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   567
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   568
        /* export files */
68116
ac82ee617a75 command-line tool "isabelle export";
wenzelm
parents: 68115
diff changeset
   569
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   570
        val store = Sessions.store(options)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   571
        export_files(store, session_name, export_dir, progress = progress, export_prune = export_prune,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   572
          export_list = export_list, export_patterns = export_patterns)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   573
      })
68092
888d35a19866 store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff changeset
   574
}