author | wenzelm |
Fri, 07 Sep 2018 17:08:47 +0200 | |
changeset 68926 | 5129fcc1b6c0 |
parent 68899 | b15b03c13dbb |
child 68927 | 01f46a4b22b4 |
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, |
68365 | 19 |
node_name: Document.Node.Name, |
68758 | 20 |
node_status: Document_Status.Node_Status, |
68365 | 21 |
snapshot: Document.Snapshot) |
68319 | 22 |
{ |
68365 | 23 |
def write(file_name: Path, bytes: Bytes) |
68319 | 24 |
{ |
68347 | 25 |
val path = output_dir + Path.basic(node_name.theory) + file_name |
68319 | 26 |
Isabelle_System.mkdirs(path.dir) |
27 |
Bytes.write(path, bytes) |
|
28 |
} |
|
29 |
||
68365 | 30 |
def write(file_name: Path, text: String): Unit = |
31 |
write(file_name, Bytes(text)) |
|
68332 | 32 |
|
68365 | 33 |
def write(file_name: Path, body: XML.Body): Unit = |
34 |
write(file_name, Symbol.encode(YXML.string_of_body(body))) |
|
68319 | 35 |
} |
68316 | 36 |
|
68347 | 37 |
sealed case class Aspect(name: String, description: String, operation: Aspect_Args => Unit, |
38 |
options: List[String] = Nil) |
|
68345 | 39 |
{ |
40 |
override def toString: String = name |
|
41 |
} |
|
68316 | 42 |
|
68347 | 43 |
val known_aspects = |
68316 | 44 |
List( |
68365 | 45 |
Aspect("markup", "PIDE markup (YXML format)", |
46 |
{ case args => |
|
47 |
args.write(Path.explode("markup.yxml"), |
|
48 |
args.snapshot.markup_to_XML(Text.Range.full, Markup.Elements.full)) |
|
49 |
}), |
|
68319 | 50 |
Aspect("messages", "output messages (YXML format)", |
51 |
{ case args => |
|
68365 | 52 |
args.write(Path.explode("messages.yxml"), |
53 |
args.snapshot.messages.iterator.map(_._1).toList) |
|
68347 | 54 |
}), |
55 |
Aspect("latex", "generated LaTeX source", |
|
56 |
{ case args => |
|
68365 | 57 |
for (entry <- args.snapshot.exports if entry.name == "document.tex") |
58 |
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
|
59 |
}, options = List("editor_presentation", "export_document")), |
68347 | 60 |
Aspect("theory", "foundational theory content", |
61 |
{ case args => |
|
62 |
for { |
|
68365 | 63 |
entry <- args.snapshot.exports |
68347 | 64 |
if entry.name.startsWith(Export_Theory.export_prefix) |
68365 | 65 |
} args.write(Path.explode(entry.name), entry.uncompressed()) |
68347 | 66 |
}, options = List("editor_presentation", "export_theory")) |
68345 | 67 |
).sortBy(_.name) |
68316 | 68 |
|
69 |
def show_aspects: String = |
|
68345 | 70 |
cat_lines(known_aspects.map(aspect => aspect.name + " - " + aspect.description)) |
68316 | 71 |
|
72 |
def the_aspect(name: String): Aspect = |
|
73 |
known_aspects.find(aspect => aspect.name == name) getOrElse |
|
74 |
error("Unknown aspect " + quote(name)) |
|
75 |
||
76 |
||
77 |
/* dump */ |
|
78 |
||
79 |
val default_output_dir = Path.explode("dump") |
|
80 |
||
68416 | 81 |
def make_options(options: Options, aspects: List[Aspect]): Options = |
68895 | 82 |
{ |
68896
e63eaae13165
more robust default options, notably for node consolidation;
wenzelm
parents:
68895
diff
changeset
|
83 |
val options1 = options + "completion_limit=0" + "ML_statistics=false" + "parallel_proofs=0" |
68895 | 84 |
(options1 /: aspects)({ case (opts, aspect) => (opts /: aspect.options)(_ + _) }) |
85 |
} |
|
68416 | 86 |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
87 |
def dump(options: Options, logic: String, |
68316 | 88 |
aspects: List[Aspect] = Nil, |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
89 |
progress: Progress = No_Progress, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
90 |
log: Logger = No_Logger, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
91 |
dirs: List[Path] = Nil, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
92 |
select_dirs: List[Path] = Nil, |
68316 | 93 |
output_dir: Path = default_output_dir, |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
94 |
verbose: Boolean = false, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
95 |
system_mode: Boolean = false, |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
96 |
selection: Sessions.Selection = Sessions.Selection.empty): Process_Result = |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
97 |
{ |
68537 | 98 |
if (Build.build_logic(options, logic, build_heap = true, progress = progress, |
68743 | 99 |
dirs = dirs ::: select_dirs, system_mode = system_mode) != 0) error(logic + " FAILED") |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
100 |
|
68416 | 101 |
val dump_options = make_options(options, aspects) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
102 |
|
68318 | 103 |
|
104 |
/* dependencies */ |
|
105 |
||
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
106 |
val deps = |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
107 |
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
|
108 |
selection_deps(selection) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
109 |
|
68318 | 110 |
val include_sessions = |
111 |
deps.sessions_structure.imports_topological_order |
|
112 |
||
113 |
val use_theories = |
|
114 |
deps.sessions_structure.build_topological_order. |
|
115 |
flatMap(session_name => deps.session_bases(session_name).used_theories.map(_.theory)) |
|
116 |
||
117 |
||
68926 | 118 |
/* dump aspects asynchronously */ |
119 |
||
120 |
object Consumer |
|
121 |
{ |
|
122 |
private val errors = Synchronized[List[String]](Nil) |
|
123 |
||
124 |
private val consumer = |
|
125 |
Consumer_Thread.fork(name = "dump")( |
|
126 |
consume = (args: (Document.Snapshot, Document_Status.Node_Status)) => |
|
127 |
{ |
|
128 |
val (snapshot, node_status) = args |
|
129 |
if (node_status.ok) { |
|
130 |
val aspect_args = |
|
131 |
Aspect_Args(dump_options, progress, deps, output_dir, |
|
132 |
snapshot.node_name, node_status, snapshot) |
|
133 |
aspects.foreach(_.operation(aspect_args)) |
|
134 |
} |
|
135 |
for ((tree, pos) <- snapshot.messages if Protocol.is_error(tree)) { |
|
136 |
val msg = XML.content(Pretty.formatted(List(tree))) |
|
137 |
errors.change(("Error" + Position.here(pos) + ":\n" + msg) :: _) |
|
138 |
} |
|
139 |
true |
|
140 |
}) |
|
141 |
||
142 |
def apply(snapshot: Document.Snapshot, node_status: Document_Status.Node_Status): Unit = |
|
143 |
consumer.send((snapshot, node_status)) |
|
144 |
||
145 |
def shutdown(): List[String] = |
|
146 |
{ |
|
147 |
consumer.shutdown() |
|
148 |
errors.value.reverse |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
||
68318 | 153 |
/* session */ |
154 |
||
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
155 |
val session = |
68743 | 156 |
Thy_Resources.start_session(dump_options, logic, session_dirs = dirs ::: select_dirs, |
68318 | 157 |
include_sessions = include_sessions, progress = progress, log = log) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
158 |
|
68926 | 159 |
val theories_result = |
160 |
session.use_theories(use_theories, progress = progress, commit = Some(Consumer.apply _)) |
|
161 |
||
68320
1d33697199c1
shutdown ML process before output: Theories_Result is timeless/stateless;
wenzelm
parents:
68319
diff
changeset
|
162 |
val session_result = session.stop() |
1d33697199c1
shutdown ML process before output: Theories_Result is timeless/stateless;
wenzelm
parents:
68319
diff
changeset
|
163 |
|
68926 | 164 |
Consumer.shutdown().foreach(progress.echo_error_message(_)) |
68320
1d33697199c1
shutdown ML process before output: Theories_Result is timeless/stateless;
wenzelm
parents:
68319
diff
changeset
|
165 |
|
68557 | 166 |
if (theories_result.ok) session_result |
68926 | 167 |
else session_result.copy(rc = session_result.rc max 1) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
168 |
} |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
169 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
170 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
171 |
/* Isabelle tool wrapper */ |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
172 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
173 |
val isabelle_tool = |
68348 | 174 |
Isabelle_Tool("dump", "dump cumulative PIDE session database", args => |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
175 |
{ |
68345 | 176 |
var aspects: List[Aspect] = known_aspects |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
177 |
var base_sessions: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
178 |
var select_dirs: List[Path] = Nil |
68316 | 179 |
var output_dir = default_output_dir |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
180 |
var requirements = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
181 |
var exclude_session_groups: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
182 |
var all_sessions = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
183 |
var dirs: List[Path] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
184 |
var session_groups: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
185 |
var logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
186 |
var options = Options.init() |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
187 |
var system_mode = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
188 |
var verbose = false |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
189 |
var exclude_sessions: List[String] = Nil |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
190 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
191 |
val getopts = Getopts(""" |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
192 |
Usage: isabelle dump [OPTIONS] [SESSIONS ...] |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
193 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
194 |
Options are: |
68345 | 195 |
-A NAMES dump named aspects (default: """ + known_aspects.mkString("\"", ",", "\"") + """) |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
196 |
-B NAME include session NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
197 |
-D DIR include session directory and select its sessions |
68316 | 198 |
-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
|
199 |
-R operate on requirements of selected sessions |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
200 |
-X NAME exclude sessions from group NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
201 |
-a select all sessions |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
202 |
-d DIR include session directory |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
203 |
-g NAME select session group NAME |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
204 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
205 |
-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
|
206 |
-s system build mode for logic image |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
207 |
-v verbose |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
208 |
-x NAME exclude session NAME and all descendants |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
209 |
|
68348 | 210 |
Dump cumulative PIDE session database, with the following aspects: |
68316 | 211 |
|
212 |
""" + Library.prefix_lines(" ", show_aspects) + "\n", |
|
213 |
"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
|
214 |
"B:" -> (arg => base_sessions = base_sessions ::: List(arg)), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
215 |
"D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))), |
68316 | 216 |
"O:" -> (arg => output_dir = Path.explode(arg)), |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
217 |
"R" -> (_ => requirements = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
218 |
"X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
219 |
"a" -> (_ => all_sessions = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
220 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
68741 | 221 |
"g:" -> (arg => session_groups = session_groups ::: List(arg)), |
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
222 |
"l:" -> (arg => logic = arg), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
223 |
"o:" -> (arg => options = options + arg), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
224 |
"s" -> (_ => system_mode = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
225 |
"v" -> (_ => verbose = true), |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
226 |
"x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg))) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
227 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
228 |
val sessions = getopts(args) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
229 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
230 |
val progress = new Console_Progress(verbose = verbose) |
68899 | 231 |
{ |
232 |
override def theory_percentage(session: String, theory: String, percentage: Int) |
|
233 |
{ |
|
234 |
if (verbose) echo(Progress.theory_message(session, theory) + ": " + percentage + "%") |
|
235 |
} |
|
236 |
} |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
237 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
238 |
val result = |
68331 | 239 |
progress.interrupt_handler { |
240 |
dump(options, logic, |
|
241 |
aspects = aspects, |
|
242 |
progress = progress, |
|
243 |
dirs = dirs, |
|
244 |
select_dirs = select_dirs, |
|
245 |
output_dir = output_dir, |
|
246 |
verbose = verbose, |
|
247 |
selection = Sessions.Selection( |
|
248 |
requirements = requirements, |
|
249 |
all_sessions = all_sessions, |
|
250 |
base_sessions = base_sessions, |
|
251 |
exclude_session_groups = exclude_session_groups, |
|
252 |
exclude_sessions = exclude_sessions, |
|
253 |
session_groups = session_groups, |
|
254 |
sessions = sessions)) |
|
255 |
} |
|
68308
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
256 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
257 |
progress.echo(result.timing.message_resources) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
258 |
|
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
259 |
sys.exit(result.rc) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
260 |
}) |
119fc05f6b00
support to dump build database produced by PIDE session;
wenzelm
parents:
diff
changeset
|
261 |
} |