73718
|
1 |
/* Title: Pure/Thy/document_build.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Build theory document (PDF) from session database.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Document_Build
|
|
11 |
{
|
|
12 |
/* document variants */
|
|
13 |
|
|
14 |
abstract class Document_Name
|
|
15 |
{
|
|
16 |
def name: String
|
|
17 |
def path: Path = Path.basic(name)
|
|
18 |
|
|
19 |
override def toString: String = name
|
|
20 |
}
|
|
21 |
|
|
22 |
object Document_Variant
|
|
23 |
{
|
|
24 |
def parse(name: String, tags: String): Document_Variant =
|
|
25 |
Document_Variant(name, Library.space_explode(',', tags))
|
|
26 |
|
|
27 |
def parse(opt: String): Document_Variant =
|
|
28 |
Library.space_explode('=', opt) match {
|
|
29 |
case List(name) => Document_Variant(name, Nil)
|
|
30 |
case List(name, tags) => parse(name, tags)
|
|
31 |
case _ => error("Malformed document variant: " + quote(opt))
|
|
32 |
}
|
|
33 |
}
|
|
34 |
|
|
35 |
sealed case class Document_Variant(name: String, tags: List[String]) extends Document_Name
|
|
36 |
{
|
|
37 |
def print_tags: String = tags.mkString(",")
|
|
38 |
def print: String = if (tags.isEmpty) name else name + "=" + print_tags
|
|
39 |
|
|
40 |
def latex_sty: String =
|
|
41 |
Library.terminate_lines(
|
|
42 |
tags.map(tag =>
|
|
43 |
tag.toList match {
|
|
44 |
case '/' :: cs => "\\isafoldtag{" + cs.mkString + "}"
|
|
45 |
case '-' :: cs => "\\isadroptag{" + cs.mkString + "}"
|
|
46 |
case '+' :: cs => "\\isakeeptag{" + cs.mkString + "}"
|
|
47 |
case cs => "\\isakeeptag{" + cs.mkString + "}"
|
|
48 |
}))
|
|
49 |
}
|
|
50 |
|
|
51 |
sealed case class Document_Input(name: String, sources: SHA1.Digest)
|
|
52 |
extends Document_Name
|
|
53 |
|
|
54 |
sealed case class Document_Output(name: String, sources: SHA1.Digest, log_xz: Bytes, pdf: Bytes)
|
|
55 |
extends Document_Name
|
|
56 |
{
|
|
57 |
def log: String = log_xz.uncompress().text
|
|
58 |
def log_lines: List[String] = split_lines(log)
|
|
59 |
|
|
60 |
def write(db: SQL.Database, session_name: String): Unit =
|
|
61 |
write_document(db, session_name, this)
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
/* SQL data model */
|
|
66 |
|
|
67 |
object Data
|
|
68 |
{
|
|
69 |
val session_name = SQL.Column.string("session_name").make_primary_key
|
|
70 |
val name = SQL.Column.string("name").make_primary_key
|
|
71 |
val sources = SQL.Column.string("sources")
|
|
72 |
val log_xz = SQL.Column.bytes("log_xz")
|
|
73 |
val pdf = SQL.Column.bytes("pdf")
|
|
74 |
|
|
75 |
val table = SQL.Table("isabelle_documents", List(session_name, name, sources, log_xz, pdf))
|
|
76 |
|
|
77 |
def where_equal(session_name: String, name: String = ""): SQL.Source =
|
|
78 |
"WHERE " + Data.session_name.equal(session_name) +
|
|
79 |
(if (name == "") "" else " AND " + Data.name.equal(name))
|
|
80 |
}
|
|
81 |
|
|
82 |
def read_documents(db: SQL.Database, session_name: String): List[Document_Input] =
|
|
83 |
{
|
|
84 |
val select = Data.table.select(List(Data.name, Data.sources), Data.where_equal(session_name))
|
|
85 |
db.using_statement(select)(stmt =>
|
|
86 |
stmt.execute_query().iterator(res =>
|
|
87 |
{
|
|
88 |
val name = res.string(Data.name)
|
|
89 |
val sources = res.string(Data.sources)
|
|
90 |
Document_Input(name, SHA1.fake(sources))
|
|
91 |
}).toList)
|
|
92 |
}
|
|
93 |
|
|
94 |
def read_document(db: SQL.Database, session_name: String, name: String): Option[Document_Output] =
|
|
95 |
{
|
|
96 |
val select = Data.table.select(sql = Data.where_equal(session_name, name))
|
|
97 |
db.using_statement(select)(stmt =>
|
|
98 |
{
|
|
99 |
val res = stmt.execute_query()
|
|
100 |
if (res.next()) {
|
|
101 |
val name = res.string(Data.name)
|
|
102 |
val sources = res.string(Data.sources)
|
|
103 |
val log_xz = res.bytes(Data.log_xz)
|
|
104 |
val pdf = res.bytes(Data.pdf)
|
|
105 |
Some(Document_Output(name, SHA1.fake(sources), log_xz, pdf))
|
|
106 |
}
|
|
107 |
else None
|
|
108 |
})
|
|
109 |
}
|
|
110 |
|
|
111 |
def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit =
|
|
112 |
{
|
|
113 |
db.using_statement(Data.table.insert())(stmt =>
|
|
114 |
{
|
|
115 |
stmt.string(1) = session_name
|
|
116 |
stmt.string(2) = doc.name
|
|
117 |
stmt.string(3) = doc.sources.toString
|
|
118 |
stmt.bytes(4) = doc.log_xz
|
|
119 |
stmt.bytes(5) = doc.pdf
|
|
120 |
stmt.execute()
|
|
121 |
})
|
|
122 |
}
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
/* build documents */
|
|
127 |
|
|
128 |
val session_tex_path = Path.explode("session.tex")
|
|
129 |
|
|
130 |
def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex"
|
|
131 |
def document_tex_name(name: Document.Node.Name): String = "document/" + tex_name(name)
|
|
132 |
|
|
133 |
class Build_Error(val log_lines: List[String], val message: String)
|
|
134 |
extends Exn.User_Error(message)
|
|
135 |
|
|
136 |
def build_documents(
|
|
137 |
session: String,
|
|
138 |
deps: Sessions.Deps,
|
|
139 |
db_context: Sessions.Database_Context,
|
|
140 |
progress: Progress = new Progress,
|
|
141 |
output_sources: Option[Path] = None,
|
|
142 |
output_pdf: Option[Path] = None,
|
|
143 |
verbose: Boolean = false,
|
|
144 |
verbose_latex: Boolean = false): List[Document_Output] =
|
|
145 |
{
|
|
146 |
/* session info */
|
|
147 |
|
|
148 |
val info = deps.sessions_structure(session)
|
|
149 |
val hierarchy = deps.sessions_structure.hierarchy(session)
|
|
150 |
val options = info.options
|
|
151 |
val base = deps(session)
|
|
152 |
val graph_pdf = graphview.Graph_File.make_pdf(options, base.session_graph_display)
|
|
153 |
|
|
154 |
|
|
155 |
/* prepare document directory */
|
|
156 |
|
|
157 |
lazy val tex_files =
|
|
158 |
for (name <- base.session_theories ::: base.document_theories)
|
|
159 |
yield {
|
|
160 |
val entry = db_context.get_export(hierarchy, name.theory, document_tex_name(name))
|
|
161 |
Path.basic(tex_name(name)) -> entry.uncompressed
|
|
162 |
}
|
|
163 |
|
|
164 |
def prepare_dir1(dir: Path, doc: Document_Variant): (Path, String) =
|
|
165 |
{
|
|
166 |
val doc_dir = dir + Path.basic(doc.name)
|
|
167 |
Isabelle_System.make_directory(doc_dir)
|
|
168 |
|
|
169 |
Isabelle_System.bash("isabelle latex -o sty", cwd = doc_dir.file).check
|
|
170 |
File.write(doc_dir + Path.explode("isabelletags.sty"), doc.latex_sty)
|
|
171 |
for ((base_dir, src) <- info.document_files) {
|
|
172 |
Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir)
|
|
173 |
}
|
|
174 |
|
|
175 |
File.write(doc_dir + session_tex_path,
|
|
176 |
Library.terminate_lines(
|
|
177 |
base.session_theories.map(name => "\\input{" + tex_name(name) + "}")))
|
|
178 |
|
|
179 |
for ((path, tex) <- tex_files) Bytes.write(doc_dir + path, tex)
|
|
180 |
|
|
181 |
val root1 = "root_" + doc.name
|
|
182 |
val root = if ((doc_dir + Path.explode(root1).tex).is_file) root1 else "root"
|
|
183 |
|
|
184 |
(doc_dir, root)
|
|
185 |
}
|
|
186 |
|
|
187 |
def prepare_dir2(dir: Path, doc: Document_Variant): Unit =
|
|
188 |
{
|
|
189 |
val doc_dir = dir + Path.basic(doc.name)
|
|
190 |
|
|
191 |
// non-deterministic, but irrelevant
|
|
192 |
Bytes.write(doc_dir + Presentation.session_graph_path, graph_pdf)
|
|
193 |
}
|
|
194 |
|
|
195 |
|
|
196 |
/* produce documents */
|
|
197 |
|
|
198 |
val documents =
|
|
199 |
for (doc <- info.documents)
|
|
200 |
yield {
|
|
201 |
Isabelle_System.with_tmp_dir("document")(tmp_dir =>
|
|
202 |
{
|
|
203 |
progress.echo("Preparing " + session + "/" + doc.name + " ...")
|
|
204 |
val start = Time.now()
|
|
205 |
|
|
206 |
|
|
207 |
// prepare sources
|
|
208 |
|
|
209 |
val (doc_dir, root) = prepare_dir1(tmp_dir, doc)
|
|
210 |
val digests = File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest)
|
|
211 |
val sources = SHA1.digest_set(digests)
|
|
212 |
prepare_dir2(tmp_dir, doc)
|
|
213 |
|
|
214 |
for (dir <- output_sources) {
|
|
215 |
prepare_dir1(dir, doc)
|
|
216 |
prepare_dir2(dir, doc)
|
|
217 |
}
|
|
218 |
|
|
219 |
|
|
220 |
// old document from database
|
|
221 |
|
|
222 |
val old_document =
|
|
223 |
for {
|
|
224 |
old_doc <- db_context.input_database(session)(read_document(_, _, doc.name))
|
|
225 |
if old_doc.sources == sources
|
|
226 |
}
|
|
227 |
yield {
|
|
228 |
Bytes.write(doc_dir + doc.path.pdf, old_doc.pdf)
|
|
229 |
old_doc
|
|
230 |
}
|
|
231 |
|
|
232 |
old_document getOrElse {
|
|
233 |
// bash scripts
|
|
234 |
|
|
235 |
def root_bash(ext: String): String = Bash.string(root + "." + ext)
|
|
236 |
|
|
237 |
def latex_bash(fmt: String = "pdf", ext: String = "tex"): String =
|
|
238 |
"isabelle latex -o " + Bash.string(fmt) + " " + Bash.string(root + "." + ext)
|
|
239 |
|
|
240 |
def bash(items: String*): Process_Result =
|
|
241 |
progress.bash(items.mkString(" && "), cwd = doc_dir.file,
|
|
242 |
echo = verbose_latex, watchdog = Time.seconds(0.5))
|
|
243 |
|
|
244 |
|
|
245 |
// prepare document
|
|
246 |
|
|
247 |
val result =
|
|
248 |
if ((doc_dir + Path.explode("build")).is_file) {
|
|
249 |
bash("./build pdf " + Bash.string(doc.name))
|
|
250 |
}
|
|
251 |
else {
|
|
252 |
bash(
|
|
253 |
latex_bash(),
|
|
254 |
"{ [ ! -f " + root_bash("bib") + " ] || " + latex_bash("bbl") + "; }",
|
|
255 |
"{ [ ! -f " + root_bash("idx") + " ] || " + latex_bash("idx") + "; }",
|
|
256 |
latex_bash(),
|
|
257 |
latex_bash())
|
|
258 |
}
|
|
259 |
|
|
260 |
|
|
261 |
// result
|
|
262 |
|
|
263 |
val root_pdf = Path.basic(root).pdf
|
|
264 |
val result_path = doc_dir + root_pdf
|
|
265 |
|
|
266 |
val log_lines = result.out_lines ::: result.err_lines
|
|
267 |
|
|
268 |
val errors =
|
|
269 |
(if (result.ok) Nil else List(result.err)) :::
|
|
270 |
Latex.latex_errors(doc_dir, root) :::
|
|
271 |
Bibtex.bibtex_errors(doc_dir, root)
|
|
272 |
|
|
273 |
if (errors.nonEmpty) {
|
|
274 |
val message =
|
|
275 |
Exn.cat_message(
|
|
276 |
(errors ::: List("Failed to build document " + quote(doc.name))):_*)
|
|
277 |
throw new Build_Error(log_lines, message)
|
|
278 |
}
|
|
279 |
else if (!result_path.is_file) {
|
|
280 |
val message = "Bad document result: expected to find " + root_pdf
|
|
281 |
throw new Build_Error(log_lines, message)
|
|
282 |
}
|
|
283 |
else {
|
|
284 |
val stop = Time.now()
|
|
285 |
val timing = stop - start
|
|
286 |
progress.echo("Finished " + session + "/" + doc.name +
|
|
287 |
" (" + timing.message_hms + " elapsed time)")
|
|
288 |
|
|
289 |
val log_xz = Bytes(cat_lines(log_lines)).compress()
|
|
290 |
val pdf = Bytes.read(result_path)
|
|
291 |
Document_Output(doc.name, sources, log_xz, pdf)
|
|
292 |
}
|
|
293 |
}
|
|
294 |
})
|
|
295 |
}
|
|
296 |
|
|
297 |
for (dir <- output_pdf; doc <- documents) {
|
|
298 |
Isabelle_System.make_directory(dir)
|
|
299 |
val path = dir + doc.path.pdf
|
|
300 |
Bytes.write(path, doc.pdf)
|
|
301 |
progress.echo("Document at " + path.absolute)
|
|
302 |
}
|
|
303 |
|
|
304 |
documents
|
|
305 |
}
|
|
306 |
|
|
307 |
|
|
308 |
/* Isabelle tool wrapper */
|
|
309 |
|
|
310 |
val isabelle_tool =
|
|
311 |
Isabelle_Tool("document", "prepare session theory document", Scala_Project.here, args =>
|
|
312 |
{
|
|
313 |
var output_sources: Option[Path] = None
|
|
314 |
var output_pdf: Option[Path] = None
|
|
315 |
var verbose_latex = false
|
|
316 |
var dirs: List[Path] = Nil
|
|
317 |
var options = Options.init()
|
|
318 |
var verbose_build = false
|
|
319 |
|
|
320 |
val getopts = Getopts(
|
|
321 |
"""
|
|
322 |
Usage: isabelle document [OPTIONS] SESSION
|
|
323 |
|
|
324 |
Options are:
|
|
325 |
-O DIR output directory for LaTeX sources and resulting PDF
|
|
326 |
-P DIR output directory for resulting PDF
|
|
327 |
-S DIR output directory for LaTeX sources
|
|
328 |
-V verbose latex
|
|
329 |
-d DIR include session directory
|
|
330 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME)
|
|
331 |
-v verbose build
|
|
332 |
|
|
333 |
Prepare the theory document of a session.
|
|
334 |
""",
|
|
335 |
"O:" -> (arg =>
|
|
336 |
{
|
|
337 |
val dir = Path.explode(arg)
|
|
338 |
output_sources = Some(dir)
|
|
339 |
output_pdf = Some(dir)
|
|
340 |
}),
|
|
341 |
"P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }),
|
|
342 |
"S:" -> (arg => { output_sources = Some(Path.explode(arg)) }),
|
|
343 |
"V" -> (_ => verbose_latex = true),
|
|
344 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
|
|
345 |
"o:" -> (arg => options = options + arg),
|
|
346 |
"v" -> (_ => verbose_build = true))
|
|
347 |
|
|
348 |
val more_args = getopts(args)
|
|
349 |
val session =
|
|
350 |
more_args match {
|
|
351 |
case List(a) => a
|
|
352 |
case _ => getopts.usage()
|
|
353 |
}
|
|
354 |
|
|
355 |
val progress = new Console_Progress(verbose = verbose_build)
|
|
356 |
val store = Sessions.store(options)
|
|
357 |
|
|
358 |
progress.interrupt_handler {
|
|
359 |
val res =
|
|
360 |
Build.build(options, selection = Sessions.Selection.session(session),
|
|
361 |
dirs = dirs, progress = progress, verbose = verbose_build)
|
|
362 |
if (!res.ok) error("Failed to build session " + quote(session))
|
|
363 |
|
|
364 |
val deps =
|
|
365 |
Sessions.load_structure(options + "document=pdf", dirs = dirs).
|
|
366 |
selection_deps(Sessions.Selection.session(session))
|
|
367 |
|
|
368 |
if (output_sources.isEmpty && output_pdf.isEmpty) {
|
|
369 |
progress.echo_warning("No output directory")
|
|
370 |
}
|
|
371 |
|
|
372 |
using(store.open_database_context())(db_context =>
|
|
373 |
build_documents(session, deps, db_context, progress = progress,
|
|
374 |
output_sources = output_sources, output_pdf = output_pdf,
|
|
375 |
verbose = true, verbose_latex = verbose_latex))
|
|
376 |
}
|
|
377 |
})
|
|
378 |
}
|