| author | wenzelm |
| Sat, 01 Nov 2025 13:56:39 +0100 | |
| changeset 83435 | 0f9bae334ac6 |
| parent 83007 | b9715600883c |
| permissions | -rw-r--r-- |
| 82975 | 1 |
/* Title: Pure/Tools/process_theories.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Process theories within an adhoc session context. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.io.{File => JFile}
|
|
11 |
||
12 |
import scala.collection.mutable |
|
| 82977 | 13 |
import scala.util.matching.Regex |
| 82975 | 14 |
|
15 |
||
16 |
object Process_Theories {
|
|
17 |
/** process theories **/ |
|
18 |
||
19 |
def read_files(path: Path): List[Path] = |
|
20 |
Library.trim_split_lines(File.read(path)).map(Path.explode) |
|
21 |
||
22 |
def process_theories( |
|
23 |
options: Options, |
|
24 |
logic: String, |
|
| 82990 | 25 |
directory: Option[Path] = None, |
| 82980 | 26 |
theories: List[String] = Nil, |
| 82975 | 27 |
files: List[Path] = Nil, |
28 |
dirs: List[Path] = Nil, |
|
| 82977 | 29 |
output_messages: Boolean = false, |
| 82980 | 30 |
message_head: List[Regex] = Nil, |
31 |
message_body: List[Regex] = Nil, |
|
| 82977 | 32 |
margin: Double = Pretty.default_margin, |
33 |
breakgain: Double = Pretty.default_breakgain, |
|
34 |
metric: Pretty.Metric = Symbol.Metric, |
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
35 |
unicode_symbols: Boolean = false, |
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
36 |
export_files: List[(String, Int, List[String])] = Nil, |
| 82975 | 37 |
progress: Progress = new Progress |
38 |
): Build.Results = {
|
|
39 |
Isabelle_System.with_tmp_dir("private") { private_dir =>
|
|
40 |
/* options */ |
|
41 |
||
42 |
val build_engine = Build.Engine(Build.engine_name(options)) |
|
43 |
val build_options = build_engine.build_options(options) |
|
44 |
||
45 |
||
46 |
/* session directory and files */ |
|
47 |
||
| 82977 | 48 |
val private_prefix = private_dir.implode + "/" |
49 |
||
50 |
val session_name = Sessions.DRAFT |
|
| 82990 | 51 |
val session_dir = |
52 |
directory match {
|
|
53 |
case Some(dir) => dir.absolute |
|
54 |
case None => |
|
55 |
val dir = Isabelle_System.make_directory(private_dir + Path.basic(session_name)) |
|
56 |
var seen = Set.empty[JFile] |
|
57 |
for (path0 <- files) {
|
|
58 |
val path = path0.canonical |
|
59 |
val file = path.file |
|
60 |
if (!seen(file)) {
|
|
61 |
seen += file |
|
62 |
val target = dir + path.base |
|
63 |
if (target.is_file) {
|
|
64 |
error("Duplicate session source file " + path.base + " --- from " + path)
|
|
65 |
} |
|
66 |
Isabelle_System.copy_file(path, target) |
|
67 |
} |
|
| 82975 | 68 |
} |
| 82990 | 69 |
dir |
| 82975 | 70 |
} |
71 |
||
72 |
/* session theories */ |
|
73 |
||
74 |
val more_theories = |
|
75 |
for (path <- files; name <- Thy_Header.get_thy_name(path.implode)) |
|
76 |
yield name |
|
77 |
||
78 |
val session_theories = theories ::: more_theories |
|
79 |
||
80 |
||
81 |
/* session imports */ |
|
82 |
||
83 |
val sessions_structure = Sessions.load_structure(build_options, dirs = dirs) |
|
84 |
||
85 |
val session_imports = |
|
86 |
Set.from( |
|
87 |
for {
|
|
88 |
name <- session_theories.iterator |
|
89 |
session = sessions_structure.theory_qualifier(name) |
|
90 |
if session.nonEmpty |
|
91 |
} yield session).toList |
|
92 |
||
93 |
||
|
82993
cae70aa29054
build logic image in persistent store, without private_dir;
wenzelm
parents:
82992
diff
changeset
|
94 |
/* build session */ |
|
cae70aa29054
build logic image in persistent store, without private_dir;
wenzelm
parents:
82992
diff
changeset
|
95 |
|
|
cae70aa29054
build logic image in persistent store, without private_dir;
wenzelm
parents:
82992
diff
changeset
|
96 |
Build.build_logic(options, logic, dirs = dirs, progress = progress, |
|
cae70aa29054
build logic image in persistent store, without private_dir;
wenzelm
parents:
82992
diff
changeset
|
97 |
build_heap = true, strict = true) |
| 82975 | 98 |
|
99 |
val session_entry = |
|
100 |
Sessions.Session_Entry( |
|
101 |
parent = Some(logic), |
|
102 |
theories = session_theories.map(a => (Nil, List(((a, Position.none), false)))), |
|
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
103 |
imports = session_imports, |
|
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
104 |
export_files = export_files |
|
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
105 |
.map({ case (a, b, c) => (Path.explode(a).absolute.implode, b, c) }))
|
| 82975 | 106 |
|
107 |
val session_info = |
|
108 |
Sessions.Info.make(session_entry, draft_session = true, |
|
109 |
dir = session_dir, options = options) |
|
110 |
||
| 82977 | 111 |
def session_setup(setup_session_name: String, session: Session): Unit = {
|
112 |
if (output_messages && setup_session_name == session_name) {
|
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
113 |
def recode(s: String): String = Symbol.output(unicode_symbols, s) |
| 82977 | 114 |
session.all_messages += Session.Consumer[Prover.Message]("process_theories") {
|
115 |
case output: Prover.Output |
|
116 |
if Protocol.is_exported(output.message) || Protocol.is_state(output.message) => |
|
117 |
output.properties match {
|
|
118 |
case Position.Line_File(line, file0) => |
|
119 |
val file = Library.perhaps_unprefix(private_prefix, file0) |
|
120 |
val pos = Position.Line_File(line, file) |
|
121 |
if (Build.print_log_check(pos, output.message, message_head, message_body)) {
|
|
122 |
progress.echo(Protocol.message_text(output.message, heading = true, pos = pos, |
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
123 |
recode = recode, margin = margin, breakgain = breakgain, metric = metric)) |
| 82977 | 124 |
} |
125 |
case _ => |
|
126 |
} |
|
127 |
case _ => |
|
128 |
} |
|
129 |
} |
|
130 |
} |
|
131 |
||
| 82992 | 132 |
Build.build(options, private_dir = Some(private_dir), dirs = dirs, progress = progress, |
| 82977 | 133 |
infos = List(session_info), selection = Sessions.Selection.session(session_name), |
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
134 |
session_setup = session_setup, |
|
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
135 |
export_files = export_files.nonEmpty) |
| 82975 | 136 |
} |
137 |
} |
|
138 |
||
139 |
||
140 |
||
141 |
/** Isabelle tool wrapper **/ |
|
142 |
||
143 |
val isabelle_tool = Isabelle_Tool("process_theories",
|
|
144 |
"process theories within an adhoc session context", |
|
145 |
Scala_Project.here, |
|
146 |
{ args =>
|
|
| 82990 | 147 |
var directory: Option[Path] = None |
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
148 |
val export_files = new mutable.ListBuffer[(String, Int, List[String])] |
| 82977 | 149 |
val message_head = new mutable.ListBuffer[Regex] |
150 |
val message_body = new mutable.ListBuffer[Regex] |
|
151 |
var output_messages = false |
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
152 |
var unicode_symbols = false |
| 82975 | 153 |
val dirs = new mutable.ListBuffer[Path] |
154 |
val files = new mutable.ListBuffer[Path] |
|
|
83435
0f9bae334ac6
more uniform Isabelle_System.default_logic, with subtle change of semantics due to getenv_strict;
wenzelm
parents:
83007
diff
changeset
|
155 |
var logic = Isabelle_System.default_logic() |
| 82977 | 156 |
var margin = Pretty.default_margin |
| 82975 | 157 |
var options = Options.init() |
158 |
var verbose = false |
|
159 |
||
160 |
val getopts = Getopts("""
|
|
161 |
Usage: isabelle process_theories [OPTIONS] [THEORIES...] |
|
162 |
||
163 |
Options are: |
|
| 82990 | 164 |
-D DIR explicit session directory (default: private) |
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
165 |
-E EXPORTS write session export artifacts to file-system |
| 82991 | 166 |
-F FILE include additional session files, listed in FILE |
| 82977 | 167 |
-H REGEX filter messages by matching against head |
168 |
-M REGEX filter messages by matching against body |
|
169 |
-O output messages |
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
170 |
-U output Unicode symbols |
| 82975 | 171 |
-d DIR include session directory |
| 82991 | 172 |
-f FILE include additional session file |
| 82975 | 173 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
| 82977 | 174 |
-m MARGIN margin for pretty printing (default: """ + margin + """) |
| 82975 | 175 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
176 |
-v verbose |
|
177 |
||
178 |
Process theories within an adhoc session context. |
|
179 |
""", |
|
| 82990 | 180 |
"D:" -> (arg => directory = Some(Path.explode(arg))), |
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
181 |
"E:" -> (arg => export_files += Sessions.parse_exports(arg)), |
| 82975 | 182 |
"F:" -> (arg => files ++= read_files(Path.explode(arg))), |
| 82977 | 183 |
"H:" -> (arg => message_head += arg.r), |
184 |
"M:" -> (arg => message_body += arg.r), |
|
185 |
"O" -> (_ => output_messages = true), |
|
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
186 |
"U" -> (_ => unicode_symbols = true), |
| 82975 | 187 |
"d:" -> (arg => dirs += Path.explode(arg)), |
188 |
"f:" -> (arg => files += Path.explode(arg)), |
|
189 |
"l:" -> (arg => logic = arg), |
|
| 82977 | 190 |
"m:" -> (arg => margin = Value.Double.parse(arg)), |
| 82975 | 191 |
"o:" -> (arg => options = options + arg), |
192 |
"v" -> (_ => verbose = true)) |
|
193 |
||
194 |
val theories = getopts(args) |
|
195 |
||
196 |
val progress = new Console_Progress(verbose = verbose) |
|
197 |
||
198 |
val results = |
|
199 |
progress.interrupt_handler {
|
|
| 82990 | 200 |
process_theories(options, logic, directory = directory, theories = theories, |
201 |
files = files.toList, dirs = dirs.toList, output_messages = output_messages, |
|
202 |
message_head = message_head.toList, message_body = message_body.toList, |
|
|
83007
b9715600883c
support for "isabelle process_theories -E EXPORTS";
wenzelm
parents:
82993
diff
changeset
|
203 |
margin = margin, unicode_symbols = unicode_symbols, export_files = export_files.toList, |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
204 |
progress = progress) |
| 82975 | 205 |
} |
206 |
||
207 |
sys.exit(results.rc) |
|
208 |
}) |
|
209 |
} |