author | wenzelm |
Tue, 29 Sep 2015 18:39:55 +0200 | |
changeset 61281 | 11c1bf92d61d |
parent 61025 | 636b578bfadd |
child 61282 | 3e578ddef85d |
permissions | -rw-r--r-- |
30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
27919 | 2 |
Author: Makarius |
3 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
4 |
Fundamental Isabelle system environment: quasi-static module with |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
5 |
optional init operation. |
27919 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
55618 | 10 |
|
61281 | 11 |
import java.io.{File => JFile, IOException, BufferedReader, InputStreamReader} |
56477 | 12 |
import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult} |
13 |
import java.nio.file.attribute.BasicFileAttributes |
|
27936 | 14 |
|
61281 | 15 |
import scala.collection.mutable |
16 |
||
27919 | 17 |
|
43514
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
18 |
object Isabelle_System |
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
19 |
{ |
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
20 |
/** bootstrap information **/ |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
21 |
|
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
22 |
def jdk_home(): String = |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
23 |
{ |
53582 | 24 |
val java_home = System.getProperty("java.home", "") |
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
25 |
val home = new JFile(java_home) |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
26 |
val parent = home.getParent |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
27 |
if (home.getName == "jre" && parent != null && |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
28 |
(new JFile(new JFile(parent, "bin"), "javac")).exists) parent |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
29 |
else java_home |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
30 |
} |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
31 |
|
51820 | 32 |
private def find_cygwin_root(cygwin_root0: String = ""): String = |
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
33 |
{ |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
34 |
require(Platform.is_windows) |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
35 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
36 |
val cygwin_root1 = System.getenv("CYGWIN_ROOT") |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
37 |
val cygwin_root2 = System.getProperty("cygwin.root") |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
38 |
|
51820 | 39 |
if (cygwin_root0 != null && cygwin_root0 != "") cygwin_root0 |
40 |
else if (cygwin_root1 != null && cygwin_root1 != "") cygwin_root1 |
|
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
41 |
else if (cygwin_root2 != null && cygwin_root2 != "") cygwin_root2 |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
42 |
else error("Cannot determine Cygwin root directory") |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
43 |
} |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
44 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
45 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
46 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
47 |
/** implicit settings environment **/ |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
48 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
49 |
@volatile private var _settings: Option[Map[String, String]] = None |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
50 |
|
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
51 |
def settings(): Map[String, String] = |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
52 |
{ |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
53 |
if (_settings.isEmpty) init() // unsynchronized check |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
54 |
_settings.get |
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
55 |
} |
31796 | 56 |
|
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
57 |
/* |
51820 | 58 |
Isabelle home precedence: |
59 |
(1) isabelle_home as explicit argument |
|
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
60 |
(2) ISABELLE_HOME process environment variable (e.g. inherited from running isabelle tool) |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
61 |
(3) isabelle.home system property (e.g. via JVM application boot process) |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
62 |
*/ |
61281 | 63 |
def init( |
64 |
isabelle_home: String = "", |
|
65 |
cygwin_root: String = "", |
|
66 |
progress: Progress = Ignore_Progress): Unit = synchronized { |
|
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
67 |
if (_settings.isEmpty) { |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
68 |
import scala.collection.JavaConversions._ |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
69 |
|
51820 | 70 |
def set_cygwin_root() |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
71 |
{ |
51820 | 72 |
if (Platform.is_windows) |
73 |
_settings = Some(_settings.getOrElse(Map.empty) + |
|
74 |
("CYGWIN_ROOT" -> find_cygwin_root(cygwin_root))) |
|
75 |
} |
|
47725
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
76 |
|
51820 | 77 |
set_cygwin_root() |
57411
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
78 |
|
58640
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
79 |
def default(env: Map[String, String], entry: (String, String)): Map[String, String] = |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
80 |
if (env.isDefinedAt(entry._1) || entry._2 == "") env |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
81 |
else env + entry |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
82 |
|
57411
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
83 |
val env = |
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
84 |
{ |
58640
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
85 |
val temp_windows = |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
86 |
{ |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
87 |
val temp = System.getenv("TEMP") |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
88 |
if (temp != null && temp.contains('\\')) temp else "" |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
89 |
} |
57411
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
90 |
val user_home = System.getProperty("user.home", "") |
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
91 |
val isabelle_app = System.getProperty("isabelle.app", "") |
51820 | 92 |
|
58640
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
93 |
default( |
37f852399a32
prefer original TEMP from Windows, e.g. relevant for Isabelle distribution within read-only directory (due to its bundled Cygwin and /tmp inside of it);
wenzelm
parents:
57411
diff
changeset
|
94 |
default( |
60992 | 95 |
default(sys.env + ("ISABELLE_JDK_HOME" -> File.standard_path(jdk_home())), |
60215 | 96 |
"TEMP_WINDOWS" -> temp_windows), |
97 |
"HOME" -> user_home), |
|
98 |
"ISABELLE_APP" -> "true") |
|
57411
9444489766a1
sane environment defaults for Mac OS X, based on former App1/script -- e.g. relevant for MacTeX PATH;
wenzelm
parents:
56871
diff
changeset
|
99 |
} |
29177 | 100 |
|
51820 | 101 |
val system_home = |
102 |
if (isabelle_home != null && isabelle_home != "") isabelle_home |
|
103 |
else |
|
104 |
env.get("ISABELLE_HOME") match { |
|
105 |
case None | Some("") => |
|
53582 | 106 |
val path = System.getProperty("isabelle.home", "") |
107 |
if (path == "") error("Unknown Isabelle home directory") |
|
51820 | 108 |
else path |
109 |
case Some(path) => path |
|
110 |
} |
|
31498 | 111 |
|
51820 | 112 |
val settings = |
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
113 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
114 |
val dump = JFile.createTempFile("settings", null) |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
115 |
dump.deleteOnExit |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
116 |
try { |
51820 | 117 |
val shell_prefix = |
118 |
if (Platform.is_windows) List(find_cygwin_root(cygwin_root) + "\\bin\\bash", "-l") |
|
119 |
else Nil |
|
120 |
val cmdline = |
|
121 |
shell_prefix ::: List(system_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
|
61281 | 122 |
val (output, rc) = process_output(progress, raw_execute(null, env, true, cmdline: _*)) |
51820 | 123 |
if (rc != 0) error(output) |
56477 | 124 |
|
51820 | 125 |
val entries = |
56661
ef623f6f036b
avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents:
56599
diff
changeset
|
126 |
(for (entry <- File.read(dump) split "\u0000" if entry != "") yield { |
51820 | 127 |
val i = entry.indexOf('=') |
60215 | 128 |
if (i <= 0) entry -> "" |
129 |
else entry.substring(0, i) -> entry.substring(i + 1) |
|
51820 | 130 |
}).toMap |
131 |
entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM" |
|
132 |
} |
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
133 |
finally { dump.delete } |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
134 |
} |
50652
ead5714cc480
tuned signature -- eliminated obsolete Standard_System;
wenzelm
parents:
50651
diff
changeset
|
135 |
_settings = Some(settings) |
51820 | 136 |
set_cygwin_root() |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
137 |
} |
27953 | 138 |
} |
27919 | 139 |
|
31796 | 140 |
|
141 |
/* getenv */ |
|
142 |
||
47661
012a887997f3
USER_HOME settings variable points to cross-platform user home directory;
wenzelm
parents:
47113
diff
changeset
|
143 |
def getenv(name: String): String = settings.getOrElse(name, "") |
31498 | 144 |
|
145 |
def getenv_strict(name: String): String = |
|
146 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
147 |
val value = getenv(name) |
60196 | 148 |
if (value != "") value |
149 |
else error("Undefined Isabelle environment variable: " + quote(name)) |
|
27919 | 150 |
} |
151 |
||
51820 | 152 |
def get_cygwin_root(): String = getenv_strict("CYGWIN_ROOT") |
153 |
||
31796 | 154 |
|
54039 | 155 |
|
43606 | 156 |
/** file-system operations **/ |
31796 | 157 |
|
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
158 |
/* source files of Isabelle/ML bootstrap */ |
31436 | 159 |
|
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
160 |
def source_file(path: Path): Option[Path] = |
31498 | 161 |
{ |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
162 |
def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None |
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
163 |
|
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
164 |
if (path.is_absolute || path.is_current) check(path) |
31436 | 165 |
else { |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
166 |
check(Path.explode("~~/src/Pure") + path) orElse |
55876 | 167 |
(if (getenv("ML_SOURCES") == "") None |
168 |
else check(Path.explode("$ML_SOURCES") + path)) |
|
31436 | 169 |
} |
170 |
} |
|
171 |
||
172 |
||
50893
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents:
50854
diff
changeset
|
173 |
/* mkdirs */ |
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents:
50854
diff
changeset
|
174 |
|
60013 | 175 |
def mkdirs(path: Path): Unit = |
60263
2a5dbad75355
more portable mkdirs via perl, e.g. relevant for Windows UNC paths (network shares);
wenzelm
parents:
60215
diff
changeset
|
176 |
if (!path.is_dir) { |
60988 | 177 |
bash("perl -e \"use File::Path make_path; make_path(" + File.shell_path(path) + ");\"") |
178 |
if (!path.is_dir) error("Failed to create directory: " + quote(File.platform_path(path))) |
|
60263
2a5dbad75355
more portable mkdirs via perl, e.g. relevant for Windows UNC paths (network shares);
wenzelm
parents:
60215
diff
changeset
|
179 |
} |
50893
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents:
50854
diff
changeset
|
180 |
|
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents:
50854
diff
changeset
|
181 |
|
32450 | 182 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
183 |
/** external processes **/ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
184 |
|
50651 | 185 |
/* raw execute for bootstrapping */ |
186 |
||
52667 | 187 |
def raw_execute(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process = |
50651 | 188 |
{ |
189 |
val cmdline = new java.util.LinkedList[String] |
|
190 |
for (s <- args) cmdline.add(s) |
|
191 |
||
192 |
val proc = new ProcessBuilder(cmdline) |
|
193 |
if (cwd != null) proc.directory(cwd) |
|
194 |
if (env != null) { |
|
195 |
proc.environment.clear |
|
196 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
197 |
} |
|
198 |
proc.redirectErrorStream(redirect) |
|
199 |
proc.start |
|
200 |
} |
|
201 |
||
61281 | 202 |
def process_output(progress: Progress, proc: Process): (String, Int) = |
50651 | 203 |
{ |
204 |
proc.getOutputStream.close |
|
61281 | 205 |
|
206 |
val output = |
|
207 |
{ |
|
208 |
val lines = new mutable.ListBuffer[String] |
|
209 |
||
210 |
val stdout = new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
211 |
try { |
|
212 |
var line = stdout.readLine |
|
213 |
while (line != null) { |
|
214 |
progress.echo(line) |
|
215 |
lines += line |
|
216 |
line = stdout.readLine |
|
217 |
} |
|
218 |
} |
|
219 |
finally { stdout.close } |
|
220 |
||
221 |
cat_lines(lines.toList) |
|
222 |
} |
|
223 |
||
50651 | 224 |
val rc = |
225 |
try { proc.waitFor } |
|
226 |
finally { |
|
227 |
proc.getInputStream.close |
|
228 |
proc.getErrorStream.close |
|
229 |
proc.destroy |
|
230 |
Thread.interrupted |
|
231 |
} |
|
61281 | 232 |
|
50651 | 233 |
(output, rc) |
234 |
} |
|
235 |
||
236 |
||
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
237 |
/* plain execute */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
238 |
|
48409 | 239 |
def execute_env(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process = |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
240 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
241 |
val cmdline = |
55555 | 242 |
if (Platform.is_windows) List(get_cygwin_root() + "\\bin\\env.exe") ::: args.toList |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
243 |
else args |
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
244 |
val env1 = if (env == null) settings else settings ++ env |
50651 | 245 |
raw_execute(cwd, env1, redirect, cmdline: _*) |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
246 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
247 |
|
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
248 |
def execute(redirect: Boolean, args: String*): Process = |
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
249 |
execute_env(null, null, redirect, args: _*) |
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
250 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
251 |
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
252 |
/* tmp files */ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
253 |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
254 |
private def isabelle_tmp_prefix(): JFile = |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
255 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
256 |
val path = Path.explode("$ISABELLE_TMP_PREFIX") |
60013 | 257 |
path.file.mkdirs // low-level mkdirs |
60988 | 258 |
File.platform_file(path) |
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
259 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
260 |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
261 |
def tmp_file[A](name: String, ext: String = ""): JFile = |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
262 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
263 |
val suffix = if (ext == "") "" else "." + ext |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
264 |
val file = Files.createTempFile(isabelle_tmp_prefix().toPath, name, suffix).toFile |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
265 |
file.deleteOnExit |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
266 |
file |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
267 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
268 |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
269 |
def with_tmp_file[A](name: String, ext: String = "")(body: JFile => A): A = |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
270 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
271 |
val file = tmp_file(name, ext) |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
272 |
try { body(file) } finally { file.delete } |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
273 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
274 |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
275 |
|
56477 | 276 |
/* tmp dirs */ |
277 |
||
278 |
def rm_tree(root: JFile) |
|
279 |
{ |
|
280 |
root.delete |
|
281 |
if (root.isDirectory) { |
|
282 |
Files.walkFileTree(root.toPath, |
|
283 |
new SimpleFileVisitor[JPath] { |
|
284 |
override def visitFile(file: JPath, attrs: BasicFileAttributes): FileVisitResult = |
|
285 |
{ |
|
286 |
Files.delete(file) |
|
287 |
FileVisitResult.CONTINUE |
|
288 |
} |
|
289 |
||
290 |
override def postVisitDirectory(dir: JPath, e: IOException): FileVisitResult = |
|
291 |
{ |
|
292 |
if (e == null) { |
|
293 |
Files.delete(dir) |
|
294 |
FileVisitResult.CONTINUE |
|
295 |
} |
|
296 |
else throw e |
|
297 |
} |
|
298 |
} |
|
299 |
) |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
def tmp_dir(name: String): JFile = |
|
304 |
{ |
|
305 |
val dir = Files.createTempDirectory(isabelle_tmp_prefix().toPath, name).toFile |
|
306 |
dir.deleteOnExit |
|
307 |
dir |
|
308 |
} |
|
309 |
||
310 |
def with_tmp_dir[A](name: String)(body: JFile => A): A = |
|
311 |
{ |
|
312 |
val dir = tmp_dir(name) |
|
313 |
try { body(dir) } finally { rm_tree(dir) } |
|
314 |
} |
|
315 |
||
316 |
||
61025 | 317 |
/* kill */ |
318 |
||
319 |
def kill(signal: String, group_pid: String): (String, Int) = |
|
320 |
{ |
|
321 |
val bash = |
|
322 |
if (Platform.is_windows) List(get_cygwin_root() + "\\bin\\bash.exe") |
|
323 |
else List("/usr/bin/env", "bash") |
|
324 |
val cmdline = bash ::: List("-c", "kill -" + signal + " -" + group_pid) |
|
61281 | 325 |
process_output(Ignore_Progress, raw_execute(null, null, true, cmdline: _*)) |
61025 | 326 |
} |
327 |
||
328 |
||
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
329 |
/* bash */ |
31796 | 330 |
|
60991 | 331 |
private class Limited_Progress(proc: Bash.Process, progress_limit: Option[Long]) |
56666
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
332 |
{ |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
333 |
private var count = 0L |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
334 |
def apply(progress: String => Unit)(line: String): Unit = synchronized { |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
335 |
progress(line) |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
336 |
count = count + line.length + 1 |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
337 |
progress_limit match { |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
338 |
case Some(limit) if count > limit => proc.terminate |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
339 |
case _ => |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
340 |
} |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
341 |
} |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
342 |
} |
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
343 |
|
50845 | 344 |
def bash_env(cwd: JFile, env: Map[String, String], script: String, |
51962
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
345 |
progress_stdout: String => Unit = (_: String) => (), |
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
346 |
progress_stderr: String => Unit = (_: String) => (), |
56871
d06ff36b4fa7
expose interrupts more like ML version, but not in managed bash processes of Build;
wenzelm
parents:
56862
diff
changeset
|
347 |
progress_limit: Option[Long] = None, |
60991 | 348 |
strict: Boolean = true): Bash.Result = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
349 |
{ |
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
350 |
with_tmp_file("isabelle_script") { script_file => |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
351 |
File.write(script_file, script) |
60992 | 352 |
val proc = Bash.process(cwd, env, false, "bash", File.standard_path(script_file)) |
51962
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
353 |
proc.stdin.close |
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
354 |
|
56666
229309cbc508
avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents:
56661
diff
changeset
|
355 |
val limited = new Limited_Progress(proc, progress_limit) |
50845 | 356 |
val (_, stdout) = |
51962
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
357 |
Simple_Thread.future("bash_stdout") { |
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
358 |
File.read_lines(proc.stdout, limited(progress_stdout)) |
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
359 |
} |
50845 | 360 |
val (_, stderr) = |
51962
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
361 |
Simple_Thread.future("bash_stderr") { |
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
362 |
File.read_lines(proc.stderr, limited(progress_stderr)) |
016cb7d8f297
limit build process output, to avoid bombing Isabelle/Scala process by ill-behaved jobs (e.g. Containers in AFP/9025435b29cf);
wenzelm
parents:
51820
diff
changeset
|
363 |
} |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
364 |
|
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
365 |
val rc = |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
366 |
try { proc.join } |
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56666
diff
changeset
|
367 |
catch { case Exn.Interrupt() => proc.terminate; Exn.Interrupt.return_code } |
56871
d06ff36b4fa7
expose interrupts more like ML version, but not in managed bash processes of Build;
wenzelm
parents:
56862
diff
changeset
|
368 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
d06ff36b4fa7
expose interrupts more like ML version, but not in managed bash processes of Build;
wenzelm
parents:
56862
diff
changeset
|
369 |
|
60991 | 370 |
Bash.Result(stdout.join, stderr.join, rc) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
371 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
372 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
373 |
|
60991 | 374 |
def bash(script: String): Bash.Result = bash_env(null, null, script) |
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
375 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
376 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
377 |
/* system tools */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
378 |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
379 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 380 |
{ |
43669 | 381 |
Path.split(getenv_strict("ISABELLE_TOOLS")).find { dir => |
48373 | 382 |
val file = (dir + Path.basic(name)).file |
42124 | 383 |
try { |
384 |
file.isFile && file.canRead && file.canExecute && |
|
385 |
!name.endsWith("~") && !name.endsWith(".orig") |
|
386 |
} |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
387 |
catch { case _: SecurityException => false } |
34200 | 388 |
} match { |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
389 |
case Some(dir) => |
60988 | 390 |
val file = File.standard_path(dir + Path.basic(name)) |
61281 | 391 |
process_output(Ignore_Progress, execute(true, (List(file) ::: args.toList): _*)) |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
392 |
case None => ("Unknown Isabelle tool: " + name, 2) |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
393 |
} |
28063 | 394 |
} |
395 |
||
54690 | 396 |
def open(arg: String): Unit = |
397 |
bash("exec \"$ISABELLE_OPEN\" '" + arg + "' >/dev/null 2>/dev/null &") |
|
398 |
||
399 |
def pdf_viewer(arg: Path): Unit = |
|
60988 | 400 |
bash("exec \"$PDF_VIEWER\" '" + File.standard_path(arg) + "' >/dev/null 2>/dev/null &") |
54690 | 401 |
|
60991 | 402 |
def hg(cmd_line: String, cwd: Path = Path.current): Bash.Result = |
60988 | 403 |
bash("cd " + File.shell_path(cwd) + " && \"${HG:-hg}\" " + cmd_line) |
28063 | 404 |
|
32450 | 405 |
|
31796 | 406 |
/** Isabelle resources **/ |
407 |
||
32328 | 408 |
/* components */ |
409 |
||
43669 | 410 |
def components(): List[Path] = |
411 |
Path.split(getenv_strict("ISABELLE_COMPONENTS")) |
|
32328 | 412 |
|
413 |
||
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
414 |
/* logic images */ |
29152 | 415 |
|
48503 | 416 |
def find_logics_dirs(): List[Path] = |
31498 | 417 |
{ |
48503 | 418 |
val ml_ident = Path.explode("$ML_IDENTIFIER").expand |
419 |
Path.split(getenv_strict("ISABELLE_PATH")).map(_ + ml_ident) |
|
29152 | 420 |
} |
29570 | 421 |
|
48503 | 422 |
def find_logics(): List[String] = |
423 |
(for { |
|
424 |
dir <- find_logics_dirs() |
|
425 |
files = dir.file.listFiles() if files != null |
|
426 |
file <- files.toList if file.isFile } yield file.getName).sorted |
|
427 |
||
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
428 |
def default_logic(args: String*): String = |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
429 |
{ |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
430 |
args.find(_ != "") match { |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
431 |
case Some(logic) => logic |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
432 |
case None => Isabelle_System.getenv_strict("ISABELLE_LOGIC") |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
433 |
} |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
434 |
} |
27919 | 435 |
} |