src/Pure/Thy/export.scala
author wenzelm
Fri Sep 07 17:03:58 2018 +0200 (10 months ago)
changeset 68924 feed46aa1969
parent 68832 9b9fc9ea9dd1
child 69629 e1188d9d616b
permissions -rw-r--r--
tuned;
wenzelm@68092
     1
/*  Title:      Pure/Thy/export.scala
wenzelm@68092
     2
    Author:     Makarius
wenzelm@68092
     3
wenzelm@68102
     4
Manage theory exports: compressed blobs.
wenzelm@68092
     5
*/
wenzelm@68092
     6
wenzelm@68092
     7
package isabelle
wenzelm@68092
     8
wenzelm@68116
     9
wenzelm@68116
    10
import scala.annotation.tailrec
wenzelm@68116
    11
import scala.util.matching.Regex
wenzelm@68116
    12
wenzelm@68116
    13
wenzelm@68092
    14
object Export
wenzelm@68092
    15
{
wenzelm@68092
    16
  /* SQL data model */
wenzelm@68092
    17
wenzelm@68092
    18
  object Data
wenzelm@68092
    19
  {
wenzelm@68092
    20
    val session_name = SQL.Column.string("session_name").make_primary_key
wenzelm@68092
    21
    val theory_name = SQL.Column.string("theory_name").make_primary_key
wenzelm@68092
    22
    val name = SQL.Column.string("name").make_primary_key
wenzelm@68092
    23
    val compressed = SQL.Column.bool("compressed")
wenzelm@68092
    24
    val body = SQL.Column.bytes("body")
wenzelm@68092
    25
wenzelm@68092
    26
    val table =
wenzelm@68092
    27
      SQL.Table("isabelle_exports", List(session_name, theory_name, name, compressed, body))
wenzelm@68092
    28
wenzelm@68116
    29
    def where_equal(session_name: String, theory_name: String = "", name: String = ""): SQL.Source =
wenzelm@68092
    30
      "WHERE " + Data.session_name.equal(session_name) +
wenzelm@68116
    31
        (if (theory_name == "") "" else " AND " + Data.theory_name.equal(theory_name)) +
wenzelm@68116
    32
        (if (name == "") "" else " AND " + Data.name.equal(name))
wenzelm@68116
    33
  }
wenzelm@68116
    34
wenzelm@68116
    35
  def read_name(db: SQL.Database, session_name: String, theory_name: String, name: String): Boolean =
wenzelm@68116
    36
  {
wenzelm@68116
    37
    val select =
wenzelm@68116
    38
      Data.table.select(List(Data.name), Data.where_equal(session_name, theory_name, name))
wenzelm@68116
    39
    db.using_statement(select)(stmt => stmt.execute_query().next())
wenzelm@68092
    40
  }
wenzelm@68092
    41
wenzelm@68092
    42
  def read_names(db: SQL.Database, session_name: String, theory_name: String): List[String] =
wenzelm@68092
    43
  {
wenzelm@68092
    44
    val select = Data.table.select(List(Data.name), Data.where_equal(session_name, theory_name))
wenzelm@68092
    45
    db.using_statement(select)(stmt =>
wenzelm@68092
    46
      stmt.execute_query().iterator(res => res.string(Data.name)).toList)
wenzelm@68092
    47
  }
wenzelm@68092
    48
wenzelm@68222
    49
  def read_theory_names(db: SQL.Database, session_name: String): List[String] =
wenzelm@68222
    50
  {
wenzelm@68222
    51
    val select =
wenzelm@68222
    52
      Data.table.select(List(Data.theory_name), Data.where_equal(session_name), distinct = true)
wenzelm@68222
    53
    db.using_statement(select)(stmt =>
wenzelm@68222
    54
      stmt.execute_query().iterator(_.string(Data.theory_name)).toList)
wenzelm@68222
    55
  }
wenzelm@68222
    56
wenzelm@68222
    57
  def read_theory_exports(db: SQL.Database, session_name: String): List[(String, String)] =
wenzelm@68115
    58
  {
wenzelm@68116
    59
    val select = Data.table.select(List(Data.theory_name, Data.name), Data.where_equal(session_name))
wenzelm@68116
    60
    db.using_statement(select)(stmt =>
wenzelm@68116
    61
      stmt.execute_query().iterator(res =>
wenzelm@68116
    62
        (res.string(Data.theory_name), res.string(Data.name))).toList)
wenzelm@68115
    63
  }
wenzelm@68115
    64
wenzelm@68104
    65
  def message(msg: String, theory_name: String, name: String): String =
wenzelm@68104
    66
    msg + " " + quote(name) + " for theory " + quote(theory_name)
wenzelm@68104
    67
wenzelm@68116
    68
  def compound_name(a: String, b: String): String = a + ":" + b
wenzelm@68116
    69
wenzelm@68092
    70
  sealed case class Entry(
wenzelm@68103
    71
    session_name: String,
wenzelm@68103
    72
    theory_name: String,
wenzelm@68103
    73
    name: String,
wenzelm@68167
    74
    body: Future[(Boolean, Bytes)])
wenzelm@68092
    75
  {
wenzelm@68116
    76
    override def toString: String = compound_name(theory_name, name)
wenzelm@68092
    77
wenzelm@68092
    78
    def write(db: SQL.Database)
wenzelm@68092
    79
    {
wenzelm@68167
    80
      val (compressed, bytes) = body.join
wenzelm@68092
    81
      db.using_statement(Data.table.insert())(stmt =>
wenzelm@68092
    82
      {
wenzelm@68092
    83
        stmt.string(1) = session_name
wenzelm@68092
    84
        stmt.string(2) = theory_name
wenzelm@68092
    85
        stmt.string(3) = name
wenzelm@68092
    86
        stmt.bool(4) = compressed
wenzelm@68103
    87
        stmt.bytes(5) = bytes
wenzelm@68092
    88
        stmt.execute()
wenzelm@68092
    89
      })
wenzelm@68092
    90
    }
wenzelm@68116
    91
wenzelm@68167
    92
    def uncompressed(cache: XZ.Cache = XZ.cache()): Bytes =
wenzelm@68167
    93
    {
wenzelm@68167
    94
      val (compressed, bytes) = body.join
wenzelm@68167
    95
      if (compressed) bytes.uncompress(cache = cache) else bytes
wenzelm@68167
    96
    }
wenzelm@68171
    97
wenzelm@68171
    98
    def uncompressed_yxml(cache: XZ.Cache = XZ.cache()): XML.Body =
wenzelm@68171
    99
      YXML.parse_body(UTF8.decode_permissive(uncompressed(cache = cache)))
wenzelm@68116
   100
  }
wenzelm@68116
   101
wenzelm@68116
   102
  def make_regex(pattern: String): Regex =
wenzelm@68116
   103
  {
wenzelm@68116
   104
    @tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex =
wenzelm@68116
   105
      chs match {
wenzelm@68116
   106
        case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest)
wenzelm@68116
   107
        case '*' :: rest => make("[^:/]*" :: result, depth, rest)
wenzelm@68116
   108
        case '?' :: rest => make("[^:/]" :: result, depth, rest)
wenzelm@68116
   109
        case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest)
wenzelm@68116
   110
        case '{' :: rest => make("(" :: result, depth + 1, rest)
wenzelm@68116
   111
        case ',' :: rest if depth > 0 => make("|" :: result, depth, rest)
wenzelm@68116
   112
        case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest)
wenzelm@68116
   113
        case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest)
wenzelm@68116
   114
        case c :: rest => make(c.toString :: result, depth, rest)
wenzelm@68116
   115
        case Nil => result.reverse.mkString.r
wenzelm@68116
   116
      }
wenzelm@68116
   117
    make(Nil, 0, pattern.toList)
wenzelm@68092
   118
  }
wenzelm@68092
   119
wenzelm@68151
   120
  def make_matcher(pattern: String): (String, String) => Boolean =
wenzelm@68151
   121
  {
wenzelm@68151
   122
    val regex = make_regex(pattern)
wenzelm@68151
   123
    (theory_name: String, name: String) =>
wenzelm@68151
   124
      regex.pattern.matcher(compound_name(theory_name, name)).matches
wenzelm@68151
   125
  }
wenzelm@68151
   126
wenzelm@68166
   127
  def make_entry(session_name: String, args: Markup.Export.Args, body: Bytes,
wenzelm@68166
   128
    cache: XZ.Cache = XZ.cache()): Entry =
wenzelm@68101
   129
  {
wenzelm@68167
   130
    Entry(session_name, args.theory_name, args.name,
wenzelm@68167
   131
      if (args.compress) Future.fork(body.maybe_compress(cache = cache))
wenzelm@68167
   132
      else Future.value((false, body)))
wenzelm@68101
   133
  }
wenzelm@68101
   134
wenzelm@68202
   135
  def read_entry(db: SQL.Database, session_name: String, theory_name: String, name: String)
wenzelm@68202
   136
    : Option[Entry] =
wenzelm@68092
   137
  {
wenzelm@68092
   138
    val select =
wenzelm@68092
   139
      Data.table.select(List(Data.compressed, Data.body),
wenzelm@68116
   140
        Data.where_equal(session_name, theory_name, name))
wenzelm@68092
   141
    db.using_statement(select)(stmt =>
wenzelm@68092
   142
    {
wenzelm@68092
   143
      val res = stmt.execute_query()
wenzelm@68092
   144
      if (res.next()) {
wenzelm@68092
   145
        val compressed = res.bool(Data.compressed)
wenzelm@68092
   146
        val body = res.bytes(Data.body)
wenzelm@68202
   147
        Some(Entry(session_name, theory_name, name, Future.value(compressed, body)))
wenzelm@68092
   148
      }
wenzelm@68202
   149
      else None
wenzelm@68092
   150
    })
wenzelm@68092
   151
  }
wenzelm@68092
   152
wenzelm@68831
   153
  def read_entry(dir: Path, session_name: String, theory_name: String, name: String): Option[Entry] =
wenzelm@68831
   154
  {
wenzelm@68831
   155
    val path = dir + Path.basic(theory_name) + Path.explode(name)
wenzelm@68831
   156
    if (path.is_file) {
wenzelm@68831
   157
      val uncompressed = Bytes.read(path)
wenzelm@68831
   158
      Some(Entry(session_name, theory_name, name, Future.value((false, uncompressed))))
wenzelm@68831
   159
    }
wenzelm@68831
   160
    else None
wenzelm@68831
   161
  }
wenzelm@68831
   162
wenzelm@68092
   163
wenzelm@68092
   164
  /* database consumer thread */
wenzelm@68092
   165
wenzelm@68289
   166
  def consumer(db: SQL.Database, cache: XZ.Cache = XZ.cache()): Consumer = new Consumer(db, cache)
wenzelm@68092
   167
wenzelm@68289
   168
  class Consumer private[Export](db: SQL.Database, cache: XZ.Cache)
wenzelm@68092
   169
  {
wenzelm@68924
   170
    private val errors = Synchronized[List[String]](Nil)
wenzelm@68092
   171
wenzelm@68092
   172
    private val consumer =
wenzelm@68103
   173
      Consumer_Thread.fork(name = "export")(consume = (entry: Entry) =>
wenzelm@68092
   174
        {
wenzelm@68103
   175
          entry.body.join
wenzelm@68092
   176
          db.transaction {
wenzelm@68115
   177
            if (read_name(db, entry.session_name, entry.theory_name, entry.name)) {
wenzelm@68924
   178
              val msg = message("Duplicate export", entry.theory_name, entry.name)
wenzelm@68924
   179
              errors.change(msg :: _)
wenzelm@68092
   180
            }
wenzelm@68092
   181
            else entry.write(db)
wenzelm@68092
   182
          }
wenzelm@68092
   183
          true
wenzelm@68092
   184
        })
wenzelm@68092
   185
wenzelm@68103
   186
    def apply(session_name: String, args: Markup.Export.Args, body: Bytes): Unit =
wenzelm@68289
   187
      consumer.send(make_entry(session_name, args, body, cache = cache))
wenzelm@68092
   188
wenzelm@68092
   189
    def shutdown(close: Boolean = false): List[String] =
wenzelm@68092
   190
    {
wenzelm@68092
   191
      consumer.shutdown()
wenzelm@68092
   192
      if (close) db.close()
wenzelm@68924
   193
      errors.value.reverse
wenzelm@68092
   194
    }
wenzelm@68092
   195
  }
wenzelm@68116
   196
wenzelm@68116
   197
wenzelm@68418
   198
  /* abstract provider */
wenzelm@68418
   199
wenzelm@68418
   200
  object Provider
wenzelm@68418
   201
  {
wenzelm@68418
   202
    def database(db: SQL.Database, session_name: String, theory_name: String): Provider =
wenzelm@68418
   203
      new Provider {
wenzelm@68418
   204
        def apply(export_name: String): Option[Entry] =
wenzelm@68418
   205
          read_entry(db, session_name, theory_name, export_name)
wenzelm@68832
   206
wenzelm@68832
   207
        override def toString: String = db.toString
wenzelm@68418
   208
      }
wenzelm@68418
   209
wenzelm@68418
   210
    def snapshot(snapshot: Document.Snapshot): Provider =
wenzelm@68418
   211
      new Provider {
wenzelm@68418
   212
        def apply(export_name: String): Option[Entry] =
wenzelm@68418
   213
          snapshot.exports_map.get(export_name)
wenzelm@68832
   214
wenzelm@68832
   215
        override def toString: String = snapshot.toString
wenzelm@68418
   216
      }
wenzelm@68831
   217
wenzelm@68831
   218
    def directory(dir: Path, session_name: String, theory_name: String): Provider =
wenzelm@68831
   219
      new Provider {
wenzelm@68831
   220
        def apply(export_name: String): Option[Entry] =
wenzelm@68831
   221
          read_entry(dir, session_name, theory_name, export_name)
wenzelm@68832
   222
wenzelm@68832
   223
        override def toString: String = dir.toString
wenzelm@68831
   224
      }
wenzelm@68418
   225
  }
wenzelm@68418
   226
wenzelm@68418
   227
  trait Provider
wenzelm@68418
   228
  {
wenzelm@68418
   229
    def apply(export_name: String): Option[Entry]
wenzelm@68418
   230
wenzelm@68418
   231
    def uncompressed_yxml(export_name: String, cache: XZ.Cache = XZ.cache()): XML.Body =
wenzelm@68418
   232
      apply(export_name) match {
wenzelm@68418
   233
        case Some(entry) => entry.uncompressed_yxml(cache = cache)
wenzelm@68418
   234
        case None => Nil
wenzelm@68418
   235
      }
wenzelm@68418
   236
  }
wenzelm@68418
   237
wenzelm@68418
   238
wenzelm@68288
   239
  /* export to file-system */
wenzelm@68288
   240
wenzelm@68288
   241
  def export_files(
wenzelm@68288
   242
    store: Sessions.Store,
wenzelm@68288
   243
    session_name: String,
wenzelm@68288
   244
    export_dir: Path,
wenzelm@68288
   245
    progress: Progress = No_Progress,
wenzelm@68288
   246
    export_list: Boolean = false,
wenzelm@68291
   247
    export_patterns: List[String] = Nil,
wenzelm@68291
   248
    export_prefix: String = "")
wenzelm@68288
   249
  {
wenzelm@68288
   250
    using(store.open_database(session_name))(db =>
wenzelm@68288
   251
    {
wenzelm@68288
   252
      db.transaction {
wenzelm@68288
   253
        val export_names = read_theory_exports(db, session_name)
wenzelm@68288
   254
wenzelm@68288
   255
        // list
wenzelm@68288
   256
        if (export_list) {
wenzelm@68288
   257
          (for ((theory_name, name) <- export_names) yield compound_name(theory_name, name)).
wenzelm@68291
   258
            sorted.foreach(progress.echo(_))
wenzelm@68288
   259
        }
wenzelm@68288
   260
wenzelm@68288
   261
        // export
wenzelm@68290
   262
        if (export_patterns.nonEmpty) {
wenzelm@68290
   263
          val exports =
wenzelm@68290
   264
            (for {
wenzelm@68290
   265
              export_pattern <- export_patterns.iterator
wenzelm@68290
   266
              matcher = make_matcher(export_pattern)
wenzelm@68290
   267
              (theory_name, name) <- export_names if matcher(theory_name, name)
wenzelm@68290
   268
            } yield (theory_name, name)).toSet
wenzelm@68288
   269
          for {
wenzelm@68290
   270
            (theory_name, group) <- exports.toList.groupBy(_._1).toList.sortBy(_._1)
wenzelm@68290
   271
            name <- group.map(_._2).sorted
wenzelm@68288
   272
            entry <- read_entry(db, session_name, theory_name, name)
wenzelm@68288
   273
          } {
wenzelm@68288
   274
            val path = export_dir + Path.basic(theory_name) + Path.explode(name)
wenzelm@68291
   275
            progress.echo(export_prefix + "export " + path)
wenzelm@68288
   276
            Isabelle_System.mkdirs(path.dir)
wenzelm@68289
   277
            Bytes.write(path, entry.uncompressed(cache = store.xz_cache))
wenzelm@68288
   278
          }
wenzelm@68288
   279
        }
wenzelm@68288
   280
      }
wenzelm@68288
   281
    })
wenzelm@68288
   282
  }
wenzelm@68288
   283
wenzelm@68288
   284
wenzelm@68116
   285
  /* Isabelle tool wrapper */
wenzelm@68116
   286
wenzelm@68116
   287
  val default_export_dir = Path.explode("export")
wenzelm@68116
   288
wenzelm@68116
   289
  val isabelle_tool = Isabelle_Tool("export", "retrieve theory exports", args =>
wenzelm@68116
   290
  {
wenzelm@68116
   291
    /* arguments */
wenzelm@68116
   292
wenzelm@68116
   293
    var export_dir = default_export_dir
wenzelm@68116
   294
    var dirs: List[Path] = Nil
wenzelm@68116
   295
    var export_list = false
wenzelm@68116
   296
    var no_build = false
wenzelm@68116
   297
    var options = Options.init()
wenzelm@68116
   298
    var system_mode = false
wenzelm@68290
   299
    var export_patterns: List[String] = Nil
wenzelm@68116
   300
wenzelm@68116
   301
    val getopts = Getopts("""
wenzelm@68116
   302
Usage: isabelle export [OPTIONS] SESSION
wenzelm@68116
   303
wenzelm@68116
   304
  Options are:
wenzelm@68314
   305
    -O DIR       output directory for exported files (default: """ + default_export_dir + """)
wenzelm@68116
   306
    -d DIR       include session directory
wenzelm@68116
   307
    -l           list exports
wenzelm@68116
   308
    -n           no build of session
wenzelm@68116
   309
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@68116
   310
    -s           system build mode for session image
wenzelm@68116
   311
    -x PATTERN   extract files matching pattern (e.g. "*:**" for all)
wenzelm@68116
   312
wenzelm@68116
   313
  List or export theory exports for SESSION: named blobs produced by
wenzelm@68290
   314
  isabelle build. Option -l or -x is required; option -x may be repeated.
wenzelm@68116
   315
wenzelm@68116
   316
  The PATTERN language resembles glob patterns in the shell, with ? and *
wenzelm@68116
   317
  (both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc],
wenzelm@68116
   318
  and variants {pattern1,pattern2,pattern3}.
wenzelm@68116
   319
""",
wenzelm@68314
   320
      "O:" -> (arg => export_dir = Path.explode(arg)),
wenzelm@68116
   321
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@68116
   322
      "l" -> (_ => export_list = true),
wenzelm@68116
   323
      "n" -> (_ => no_build = true),
wenzelm@68116
   324
      "o:" -> (arg => options = options + arg),
wenzelm@68116
   325
      "s" -> (_ => system_mode = true),
wenzelm@68290
   326
      "x:" -> (arg => export_patterns ::= arg))
wenzelm@68116
   327
wenzelm@68116
   328
    val more_args = getopts(args)
wenzelm@68116
   329
    val session_name =
wenzelm@68116
   330
      more_args match {
wenzelm@68290
   331
        case List(session_name) if export_list || export_patterns.nonEmpty => session_name
wenzelm@68116
   332
        case _ => getopts.usage()
wenzelm@68116
   333
      }
wenzelm@68116
   334
wenzelm@68305
   335
    val progress = new Console_Progress()
wenzelm@68305
   336
wenzelm@68116
   337
wenzelm@68116
   338
    /* build */
wenzelm@68116
   339
wenzelm@68305
   340
    if (!no_build) {
wenzelm@68305
   341
      val rc =
wenzelm@68331
   342
        progress.interrupt_handler {
wenzelm@68331
   343
          Build.build_logic(options, session_name, progress = progress,
wenzelm@68331
   344
            dirs = dirs, system_mode = system_mode)
wenzelm@68331
   345
        }
wenzelm@68305
   346
      if (rc != 0) sys.exit(rc)
wenzelm@68116
   347
    }
wenzelm@68116
   348
wenzelm@68116
   349
wenzelm@68288
   350
    /* export files */
wenzelm@68116
   351
wenzelm@68209
   352
    val store = Sessions.store(options, system_mode)
wenzelm@68288
   353
    export_files(store, session_name, export_dir, progress = progress,
wenzelm@68290
   354
      export_list = export_list, export_patterns = export_patterns)
wenzelm@68116
   355
  })
wenzelm@68092
   356
}