author | wenzelm |
Thu, 27 Dec 2018 17:15:40 +0100 | |
changeset 69523 | 9403ff523825 |
parent 69522 | 9457d85204f5 |
child 69524 | fa94f2b2a877 |
permissions | -rw-r--r-- |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/Tools/dump.scala |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
3 |
|
68348 | 4 |
Dump cumulative PIDE session database. |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
6 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
8 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
9 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
10 |
object Dump |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
11 |
{ |
68316 | 12 |
/* aspects */ |
13 |
||
14 |
sealed case class Aspect_Args( |
|
68355 | 15 |
options: Options, |
16 |
progress: Progress, |
|
68365 | 17 |
deps: Sessions.Deps, |
68355 | 18 |
output_dir: Path, |
68929 | 19 |
snapshot: Document.Snapshot, |
20 |
node_status: Document_Status.Node_Status) |
|
68319 | 21 |
{ |
68365 | 22 |
def write(file_name: Path, bytes: Bytes) |
68319 | 23 |
{ |
68929 | 24 |
val path = output_dir + Path.basic(snapshot.node_name.theory) + file_name |
68319 | 25 |
Isabelle_System.mkdirs(path.dir) |
26 |
Bytes.write(path, bytes) |
|
27 |
} |
|
28 |
||
68365 | 29 |
def write(file_name: Path, text: String): Unit = |
30 |
write(file_name, Bytes(text)) |
|
68332 | 31 |
|
68365 | 32 |
def write(file_name: Path, body: XML.Body): Unit = |
33 |
write(file_name, Symbol.encode(YXML.string_of_body(body))) |
|
68319 | 34 |
} |
68316 | 35 |
|
68347 | 36 |
sealed case class Aspect(name: String, description: String, operation: Aspect_Args => Unit, |
37 |
options: List[String] = Nil) |
|
68345 | 38 |
{ |
39 |
override def toString: String = name |
|
40 |
} |
|
68316 | 41 |
|
69521 | 42 |
val known_aspects: List[Aspect] = |
68316 | 43 |
List( |
68365 | 44 |
Aspect("markup", "PIDE markup (YXML format)", |
45 |
{ case args => |
|
46 |
args.write(Path.explode("markup.yxml"), |
|
47 |
args.snapshot.markup_to_XML(Text.Range.full, Markup.Elements.full)) |
|
48 |
}), |
|
68319 | 49 |
Aspect("messages", "output messages (YXML format)", |
50 |
{ case args => |
|
68365 | 51 |
args.write(Path.explode("messages.yxml"), |
52 |
args.snapshot.messages.iterator.map(_._1).toList) |
|
68347 | 53 |
}), |
54 |
Aspect("latex", "generated LaTeX source", |
|
55 |
{ case args => |
|
68365 | 56 |
for (entry <- args.snapshot.exports if entry.name == "document.tex") |
57 |
args.write(Path.explode(entry.name), entry.uncompressed()) |
|
68491
f0f83ce0badd
disable export_document by default (presently unused and for demo/testing purposes): avoid spurious IO exception in highly parallel environment;
wenzelm
parents:
68416
diff
changeset
|
58 |
}, options = List("editor_presentation", "export_document")), |
68347 | 59 |
Aspect("theory", "foundational theory content", |
60 |
{ case args => |
|
61 |
for { |
|
68365 | 62 |
entry <- args.snapshot.exports |
68347 | 63 |
if entry.name.startsWith(Export_Theory.export_prefix) |
68365 | 64 |
} args.write(Path.explode(entry.name), entry.uncompressed()) |
68347 | 65 |
}, options = List("editor_presentation", "export_theory")) |
68345 | 66 |
).sortBy(_.name) |
68316 | 67 |
|
68 |
def show_aspects: String = |
|
68345 | 69 |
cat_lines(known_aspects.map(aspect => aspect.name + " - " + aspect.description)) |
68316 | 70 |
|
71 |
def the_aspect(name: String): Aspect = |
|
72 |
known_aspects.find(aspect => aspect.name == name) getOrElse |
|
73 |
error("Unknown aspect " + quote(name)) |
|
74 |
||
75 |
||
69523 | 76 |
/* session */ |
68316 | 77 |
|
69523 | 78 |
def session(dump_options: Options, logic: String, |
79 |
consume: (Sessions.Deps, Document.Snapshot, Document_Status.Node_Status) => Unit, |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
80 |
progress: Progress = No_Progress, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
81 |
log: Logger = No_Logger, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
82 |
dirs: List[Path] = Nil, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
83 |
select_dirs: List[Path] = Nil, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
84 |
system_mode: Boolean = false, |
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
85 |
selection: Sessions.Selection = Sessions.Selection.empty): Boolean = |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
86 |
{ |
68318 | 87 |
/* dependencies */ |
88 |
||
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
89 |
val deps = |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
90 |
Sessions.load_structure(dump_options, dirs = dirs, select_dirs = select_dirs). |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
91 |
selection_deps(selection) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
92 |
|
68318 | 93 |
val include_sessions = |
94 |
deps.sessions_structure.imports_topological_order |
|
95 |
||
69025 | 96 |
val use_theories = |
69026
6e2f9f62aafd
suppress some theories to allow "isabelle dump -o skip_proofs";
wenzelm
parents:
69025
diff
changeset
|
97 |
for { (_, name) <- deps.used_theories_condition(dump_options, progress.echo_warning) } |
69025 | 98 |
yield name.theory |
68318 | 99 |
|
100 |
||
68926 | 101 |
/* dump aspects asynchronously */ |
102 |
||
103 |
object Consumer |
|
104 |
{ |
|
68930 | 105 |
private val consumer_ok = Synchronized(true) |
68926 | 106 |
|
107 |
private val consumer = |
|
108 |
Consumer_Thread.fork(name = "dump")( |
|
109 |
consume = (args: (Document.Snapshot, Document_Status.Node_Status)) => |
|
110 |
{ |
|
111 |
val (snapshot, node_status) = args |
|
69523 | 112 |
if (node_status.ok) consume(deps, snapshot, node_status) |
68930 | 113 |
else { |
114 |
consumer_ok.change(_ => false) |
|
115 |
for ((tree, pos) <- snapshot.messages if Protocol.is_error(tree)) { |
|
116 |
val msg = XML.content(Pretty.formatted(List(tree))) |
|
117 |
progress.echo_error_message("Error" + Position.here(pos) + ":\n" + msg) |
|
118 |
} |
|
68926 | 119 |
} |
120 |
true |
|
121 |
}) |
|
122 |
||
123 |
def apply(snapshot: Document.Snapshot, node_status: Document_Status.Node_Status): Unit = |
|
124 |
consumer.send((snapshot, node_status)) |
|
125 |
||
68930 | 126 |
def shutdown(): Boolean = |
68926 | 127 |
{ |
128 |
consumer.shutdown() |
|
68930 | 129 |
consumer_ok.value |
68926 | 130 |
} |
131 |
} |
|
132 |
||
133 |
||
69523 | 134 |
/* run session */ |
68318 | 135 |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
136 |
val session = |
69012 | 137 |
Headless.start_session(dump_options, logic, session_dirs = dirs ::: select_dirs, |
68318 | 138 |
include_sessions = include_sessions, progress = progress, log = log) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
139 |
|
69013 | 140 |
val use_theories_result = |
69520 | 141 |
session.use_theories(use_theories, progress = progress, commit = Some(Consumer.apply _)) |
68926 | 142 |
|
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
143 |
session.stop() |
68320
1d33697199c1
shutdown ML process before output: Theories_Result is timeless/stateless;
wenzelm
parents:
68319
diff
changeset
|
144 |
|
68930 | 145 |
val consumer_ok = Consumer.shutdown() |
68320
1d33697199c1
shutdown ML process before output: Theories_Result is timeless/stateless;
wenzelm
parents:
68319
diff
changeset
|
146 |
|
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
147 |
use_theories_result.nodes_pending match { |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
148 |
case Nil => |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
149 |
case pending => error("Pending theories " + commas_quote(pending.map(p => p._1.toString))) |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
150 |
} |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
151 |
|
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
152 |
use_theories_result.ok && consumer_ok |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
153 |
} |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
154 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
155 |
|
69523 | 156 |
/* dump */ |
157 |
||
158 |
val default_output_dir: Path = Path.explode("dump") |
|
159 |
||
160 |
def make_options(options: Options, aspects: List[Aspect] = Nil): Options = |
|
161 |
{ |
|
162 |
val options0 = if (NUMA.enabled) NUMA.policy_options(options) else options |
|
163 |
val options1 = |
|
164 |
options0 + "completion_limit=0" + "ML_statistics=false" + |
|
165 |
"parallel_proofs=0" + "editor_tracing_messages=0" |
|
166 |
(options1 /: aspects)({ case (opts, aspect) => (opts /: aspect.options)(_ + _) }) |
|
167 |
} |
|
168 |
||
169 |
def dump(options: Options, logic: String, |
|
170 |
aspects: List[Aspect] = Nil, |
|
171 |
progress: Progress = No_Progress, |
|
172 |
log: Logger = No_Logger, |
|
173 |
dirs: List[Path] = Nil, |
|
174 |
select_dirs: List[Path] = Nil, |
|
175 |
output_dir: Path = default_output_dir, |
|
176 |
system_mode: Boolean = false, |
|
177 |
selection: Sessions.Selection = Sessions.Selection.empty): Boolean = |
|
178 |
{ |
|
179 |
if (Build.build_logic(options, logic, build_heap = true, progress = progress, |
|
180 |
dirs = dirs ::: select_dirs, system_mode = system_mode) != 0) error(logic + " FAILED") |
|
181 |
||
182 |
val dump_options = make_options(options, aspects) |
|
183 |
||
184 |
def consume( |
|
185 |
deps: Sessions.Deps, |
|
186 |
snapshot: Document.Snapshot, |
|
187 |
node_status: Document_Status.Node_Status) |
|
188 |
{ |
|
189 |
val aspect_args = Aspect_Args(dump_options, progress, deps, output_dir, snapshot, node_status) |
|
190 |
aspects.foreach(_.operation(aspect_args)) |
|
191 |
} |
|
192 |
||
193 |
session(dump_options, logic, consume _, |
|
194 |
progress = progress, log = log, dirs = dirs, select_dirs = select_dirs, selection = selection) |
|
195 |
} |
|
196 |
||
197 |
||
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
198 |
/* Isabelle tool wrapper */ |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
199 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
200 |
val isabelle_tool = |
68348 | 201 |
Isabelle_Tool("dump", "dump cumulative PIDE session database", args => |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
202 |
{ |
68345 | 203 |
var aspects: List[Aspect] = known_aspects |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
204 |
var base_sessions: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
205 |
var select_dirs: List[Path] = Nil |
68316 | 206 |
var output_dir = default_output_dir |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
207 |
var requirements = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
208 |
var exclude_session_groups: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
209 |
var all_sessions = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
210 |
var dirs: List[Path] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
211 |
var session_groups: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
212 |
var logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
213 |
var options = Options.init() |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
214 |
var system_mode = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
215 |
var verbose = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
216 |
var exclude_sessions: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
217 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
218 |
val getopts = Getopts(""" |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
219 |
Usage: isabelle dump [OPTIONS] [SESSIONS ...] |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
220 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
221 |
Options are: |
68345 | 222 |
-A NAMES dump named aspects (default: """ + known_aspects.mkString("\"", ",", "\"") + """) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
223 |
-B NAME include session NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
224 |
-D DIR include session directory and select its sessions |
68316 | 225 |
-O DIR output directory for dumped files (default: """ + default_output_dir + """) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
226 |
-R operate on requirements of selected sessions |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
227 |
-X NAME exclude sessions from group NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
228 |
-a select all sessions |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
229 |
-d DIR include session directory |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
230 |
-g NAME select session group NAME |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
231 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
232 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
233 |
-s system build mode for logic image |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
234 |
-v verbose |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
235 |
-x NAME exclude session NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
236 |
|
68348 | 237 |
Dump cumulative PIDE session database, with the following aspects: |
68316 | 238 |
|
239 |
""" + Library.prefix_lines(" ", show_aspects) + "\n", |
|
240 |
"A:" -> (arg => aspects = Library.distinct(space_explode(',', arg)).map(the_aspect(_))), |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
241 |
"B:" -> (arg => base_sessions = base_sessions ::: List(arg)), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
242 |
"D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))), |
68316 | 243 |
"O:" -> (arg => output_dir = Path.explode(arg)), |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
244 |
"R" -> (_ => requirements = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
245 |
"X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
246 |
"a" -> (_ => all_sessions = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
247 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
68741 | 248 |
"g:" -> (arg => session_groups = session_groups ::: List(arg)), |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
249 |
"l:" -> (arg => logic = arg), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
250 |
"o:" -> (arg => options = options + arg), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
251 |
"s" -> (_ => system_mode = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
252 |
"v" -> (_ => verbose = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
253 |
"x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg))) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
254 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
255 |
val sessions = getopts(args) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
256 |
|
68951
a7b1fe2d30ad
more uniform Progress, with theory() for batch-build and theory_percentage() for PIDE session;
wenzelm
parents:
68948
diff
changeset
|
257 |
val progress = new Console_Progress(verbose = verbose) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
258 |
|
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69026
diff
changeset
|
259 |
val ok = |
68331 | 260 |
progress.interrupt_handler { |
261 |
dump(options, logic, |
|
262 |
aspects = aspects, |
|
263 |
progress = progress, |
|
264 |
dirs = dirs, |
|
265 |
select_dirs = select_dirs, |
|
266 |
output_dir = output_dir, |
|
267 |
selection = Sessions.Selection( |
|
268 |
requirements = requirements, |
|
269 |
all_sessions = all_sessions, |
|
270 |
base_sessions = base_sessions, |
|
271 |
exclude_session_groups = exclude_session_groups, |
|
272 |
exclude_sessions = exclude_sessions, |
|
273 |
session_groups = session_groups, |
|
274 |
sessions = sessions)) |
|
275 |
} |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
276 |
|
69033 | 277 |
if (!ok) sys.exit(2) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
278 |
}) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
279 |
} |