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
|
|
13 |
|
|
14 |
|
|
15 |
object Process_Theories {
|
|
16 |
/** process theories **/
|
|
17 |
|
|
18 |
def read_files(path: Path): List[Path] =
|
|
19 |
Library.trim_split_lines(File.read(path)).map(Path.explode)
|
|
20 |
|
|
21 |
def process_theories(
|
|
22 |
options: Options,
|
|
23 |
logic: String,
|
|
24 |
theories: List[String],
|
|
25 |
files: List[Path] = Nil,
|
|
26 |
dirs: List[Path] = Nil,
|
|
27 |
progress: Progress = new Progress
|
|
28 |
): Build.Results = {
|
|
29 |
Isabelle_System.with_tmp_dir("private") { private_dir =>
|
|
30 |
/* options */
|
|
31 |
|
|
32 |
val build_engine = Build.Engine(Build.engine_name(options))
|
|
33 |
val build_options = build_engine.build_options(options)
|
|
34 |
|
|
35 |
|
|
36 |
/* session directory and files */
|
|
37 |
|
|
38 |
val session_dir = Isabelle_System.make_directory(private_dir + Path.basic("session"))
|
|
39 |
|
|
40 |
{
|
|
41 |
var seen = Set.empty[JFile]
|
|
42 |
for (path0 <- files) {
|
|
43 |
val path = path0.canonical
|
|
44 |
val file = path.file
|
|
45 |
if (!seen(file)) {
|
|
46 |
seen += file
|
|
47 |
val target = session_dir + path.base
|
|
48 |
if (target.is_file) {
|
|
49 |
error("Duplicate session source file " + path.base + " --- from " + path)
|
|
50 |
}
|
|
51 |
Isabelle_System.copy_file(path, target)
|
|
52 |
}
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
/* session theories */
|
|
57 |
|
|
58 |
val more_theories =
|
|
59 |
for (path <- files; name <- Thy_Header.get_thy_name(path.implode))
|
|
60 |
yield name
|
|
61 |
|
|
62 |
val session_theories = theories ::: more_theories
|
|
63 |
|
|
64 |
|
|
65 |
/* session imports */
|
|
66 |
|
|
67 |
val sessions_structure = Sessions.load_structure(build_options, dirs = dirs)
|
|
68 |
|
|
69 |
val session_imports =
|
|
70 |
Set.from(
|
|
71 |
for {
|
|
72 |
name <- session_theories.iterator
|
|
73 |
session = sessions_structure.theory_qualifier(name)
|
|
74 |
if session.nonEmpty
|
|
75 |
} yield session).toList
|
|
76 |
|
|
77 |
|
|
78 |
/* build adhoc session */
|
|
79 |
|
|
80 |
val session_entry =
|
|
81 |
Sessions.Session_Entry(
|
|
82 |
parent = Some(logic),
|
|
83 |
theories = session_theories.map(a => (Nil, List(((a, Position.none), false)))),
|
|
84 |
imports = session_imports)
|
|
85 |
|
|
86 |
val session_info =
|
|
87 |
Sessions.Info.make(session_entry, draft_session = true,
|
|
88 |
dir = session_dir, options = options)
|
|
89 |
|
|
90 |
Build.build(options, private_dir = Some(private_dir), progress = progress,
|
|
91 |
infos = List(session_info), selection = Sessions.Selection.session(session_info.name))
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
/** Isabelle tool wrapper **/
|
|
98 |
|
|
99 |
val isabelle_tool = Isabelle_Tool("process_theories",
|
|
100 |
"process theories within an adhoc session context",
|
|
101 |
Scala_Project.here,
|
|
102 |
{ args =>
|
|
103 |
val dirs = new mutable.ListBuffer[Path]
|
|
104 |
val files = new mutable.ListBuffer[Path]
|
|
105 |
var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
|
|
106 |
var options = Options.init()
|
|
107 |
var verbose = false
|
|
108 |
|
|
109 |
val getopts = Getopts("""
|
|
110 |
Usage: isabelle process_theories [OPTIONS] [THEORIES...]
|
|
111 |
|
|
112 |
Options are:
|
|
113 |
-F FILE include addition session files, listed in FILE
|
|
114 |
-d DIR include session directory
|
|
115 |
-f FILE include addition session files
|
|
116 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
|
|
117 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME)
|
|
118 |
-v verbose
|
|
119 |
|
|
120 |
Process theories within an adhoc session context.
|
|
121 |
""",
|
|
122 |
"F:" -> (arg => files ++= read_files(Path.explode(arg))),
|
|
123 |
"d:" -> (arg => dirs += Path.explode(arg)),
|
|
124 |
"f:" -> (arg => files += Path.explode(arg)),
|
|
125 |
"l:" -> (arg => logic = arg),
|
|
126 |
"o:" -> (arg => options = options + arg),
|
|
127 |
"v" -> (_ => verbose = true))
|
|
128 |
|
|
129 |
val theories = getopts(args)
|
|
130 |
|
|
131 |
val progress = new Console_Progress(verbose = verbose)
|
|
132 |
|
|
133 |
val results =
|
|
134 |
progress.interrupt_handler {
|
|
135 |
process_theories(options, logic, theories, files = files.toList, dirs = dirs.toList,
|
|
136 |
progress = progress)
|
|
137 |
}
|
|
138 |
|
|
139 |
sys.exit(results.rc)
|
|
140 |
})
|
|
141 |
}
|