| author | wenzelm |
| Mon, 06 Feb 2023 16:29:19 +0100 | |
| changeset 77218 | 86217697863c |
| parent 77079 | 395a0701a125 |
| child 77373 | eaf234b0c849 |
| permissions | -rw-r--r-- |
| 30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
| 27919 | 2 |
Author: Makarius |
3 |
||
| 73890 | 4 |
Miscellaneous Isabelle system operations. |
| 27919 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
| 55618 | 9 |
|
| 74146 | 10 |
import java.util.{Map => JMap, HashMap}
|
| 76546 | 11 |
import java.util.zip.ZipFile |
| 67835 | 12 |
import java.io.{File => JFile, IOException}
|
| 76145 | 13 |
import java.net.ServerSocket |
| 73317 | 14 |
import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult,
|
15 |
StandardCopyOption, FileSystemException} |
|
| 56477 | 16 |
import java.nio.file.attribute.BasicFileAttributes |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
17 |
|
| 76546 | 18 |
import scala.jdk.CollectionConverters._ |
19 |
||
| 27919 | 20 |
|
| 75393 | 21 |
object Isabelle_System {
|
| 75218 | 22 |
/* settings environment */ |
| 56477 | 23 |
|
|
77079
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
24 |
trait Settings { def get(name: String): String }
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
25 |
trait Settings_Env extends Settings { def env: JMap[String, String] }
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
26 |
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
27 |
class Env(val env: JMap[String, String]) extends Settings_Env {
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
28 |
override def get(name: String): String = Option(env.get(name)).getOrElse("")
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
29 |
} |
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
30 |
|
| 75393 | 31 |
def settings(putenv: List[(String, String)] = Nil): JMap[String, String] = {
|
| 74146 | 32 |
val env0 = isabelle.setup.Environment.settings() |
33 |
if (putenv.isEmpty) env0 |
|
34 |
else {
|
|
35 |
val env = new HashMap(env0) |
|
36 |
for ((a, b) <- putenv) env.put(a, b) |
|
37 |
env |
|
38 |
} |
|
39 |
} |
|
| 71739 | 40 |
|
|
77079
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
41 |
def settings_env(putenv: List[(String, String)] = Nil): Settings_Env = |
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
42 |
new Env(settings(putenv = putenv)) |
| 31498 | 43 |
|
|
77079
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
44 |
def getenv(name: String, env: Settings = settings_env()): String = env.get(name) |
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
45 |
|
|
395a0701a125
clarified signature: minimal interface for getenv/expand_env, instead of bulky java.util.Map;
wenzelm
parents:
77027
diff
changeset
|
46 |
def getenv_strict(name: String, env: Settings = settings_env()): String = |
| 65717 | 47 |
proper_string(getenv(name, env)) getOrElse |
48 |
error("Undefined Isabelle environment variable: " + quote(name))
|
|
| 27919 | 49 |
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
50 |
|
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
51 |
/* services */ |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
52 |
|
| 75702 | 53 |
type Service = Classpath.Service |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
54 |
|
| 75702 | 55 |
@volatile private var _classpath: Option[Classpath] = None |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
56 |
|
| 75702 | 57 |
def classpath(): Classpath = {
|
58 |
if (_classpath.isEmpty) init() // unsynchronized check |
|
59 |
_classpath.get |
|
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
60 |
} |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
61 |
|
| 75702 | 62 |
def make_services[C](c: Class[C]): List[C] = classpath().make_services(c) |
| 75697 | 63 |
|
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
64 |
|
| 75702 | 65 |
/* init settings + classpath */ |
| 75692 | 66 |
|
| 75393 | 67 |
def init(isabelle_root: String = "", cygwin_root: String = ""): Unit = {
|
| 73906 | 68 |
isabelle.setup.Environment.init(isabelle_root, cygwin_root) |
| 75692 | 69 |
synchronized {
|
| 75702 | 70 |
if (_classpath.isEmpty) _classpath = Some(Classpath()) |
| 75692 | 71 |
} |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
72 |
} |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
73 |
|
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
74 |
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
75 |
/* getetc -- static distribution parameters */ |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
76 |
|
| 75393 | 77 |
def getetc(name: String, root: Path = Path.ISABELLE_HOME): Option[String] = {
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
78 |
val path = root + Path.basic("etc") + Path.basic(name)
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
79 |
if (path.is_file) {
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
80 |
Library.trim_split_lines(File.read(path)) match {
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
81 |
case Nil => None |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
82 |
case List(s) => Some(s) |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
83 |
case _ => error("Single line expected in " + path.absolute)
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
84 |
} |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
85 |
} |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
86 |
else None |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
87 |
} |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
88 |
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
89 |
|
| 73521 | 90 |
/* Isabelle distribution identification */ |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
91 |
|
| 73525 | 92 |
def isabelle_id(root: Path = Path.ISABELLE_HOME): String = |
| 75510 | 93 |
getetc("ISABELLE_ID", root = root) orElse
|
94 |
Mercurial.archive_id(root) orElse |
|
95 |
Mercurial.id_repository(root, rev = "") getOrElse |
|
96 |
error("Failed to identify Isabelle distribution " + root.expand)
|
|
| 67865 | 97 |
|
| 75393 | 98 |
object Isabelle_Id extends Scala.Fun_String("isabelle_id") {
|
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
99 |
val here = Scala_Project.here |
| 73525 | 100 |
def apply(arg: String): String = isabelle_id() |
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
101 |
} |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
102 |
|
| 73522 | 103 |
def isabelle_tags(root: Path = Path.ISABELLE_HOME): String = |
| 73521 | 104 |
getetc("ISABELLE_TAGS", root = root) orElse Mercurial.archive_tags(root) getOrElse {
|
105 |
if (Mercurial.is_repository(root)) {
|
|
106 |
val hg = Mercurial.repository(root) |
|
107 |
hg.tags(rev = hg.parent()) |
|
108 |
} |
|
109 |
else "" |
|
110 |
} |
|
| 31796 | 111 |
|
| 75218 | 112 |
def export_isabelle_identifier(isabelle_identifier: String): String = |
|
75563
5bba3516ddb5
more robust: always override ISABELLE_IDENTIFIER from environment;
wenzelm
parents:
75523
diff
changeset
|
113 |
"export ISABELLE_IDENTIFIER=" + Bash.string(isabelle_identifier) + "\n" |
| 75218 | 114 |
|
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
115 |
def isabelle_identifier(): Option[String] = proper_string(getenv("ISABELLE_IDENTIFIER"))
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
116 |
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
117 |
def isabelle_heading(): String = |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
118 |
isabelle_identifier() match {
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
119 |
case None => "" |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
120 |
case Some(version) => " (" + version + ")"
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
121 |
} |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
122 |
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
123 |
def isabelle_name(): String = getenv_strict("ISABELLE_NAME")
|
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
124 |
|
|
74336
7bb0ac635397
permissive identification, e.g. relevant for HOL-SPARK examples running on rsync-clone;
wenzelm
parents:
74268
diff
changeset
|
125 |
def identification(): String = |
|
7bb0ac635397
permissive identification, e.g. relevant for HOL-SPARK examples running on rsync-clone;
wenzelm
parents:
74268
diff
changeset
|
126 |
"Isabelle" + (try { "/" + isabelle_id () } catch { case ERROR(_) => "" }) + isabelle_heading()
|
| 73547 | 127 |
|
| 54039 | 128 |
|
| 43606 | 129 |
/** file-system operations **/ |
| 31796 | 130 |
|
| 73322 | 131 |
/* scala functions */ |
132 |
||
| 75437 | 133 |
private def apply_paths( |
134 |
args: List[String], |
|
135 |
fun: PartialFunction[List[Path], Unit] |
|
136 |
): List[String] = {
|
|
| 75393 | 137 |
fun(args.map(Path.explode)) |
138 |
Nil |
|
139 |
} |
|
| 73322 | 140 |
|
| 73567 | 141 |
private def apply_paths1(args: List[String], fun: Path => Unit): List[String] = |
| 75437 | 142 |
apply_paths(args, { case List(path) => fun(path) })
|
| 73322 | 143 |
|
| 73567 | 144 |
private def apply_paths2(args: List[String], fun: (Path, Path) => Unit): List[String] = |
| 75437 | 145 |
apply_paths(args, { case List(path1, path2) => fun(path1, path2) })
|
| 73322 | 146 |
|
| 73567 | 147 |
private def apply_paths3(args: List[String], fun: (Path, Path, Path) => Unit): List[String] = |
| 75437 | 148 |
apply_paths(args, { case List(path1, path2, path3) => fun(path1, path2, path3) })
|
| 73322 | 149 |
|
150 |
||
| 71114 | 151 |
/* permissions */ |
152 |
||
153 |
def chmod(arg: String, path: Path): Unit = |
|
| 71123 | 154 |
bash("chmod " + arg + " " + File.bash_path(path)).check
|
| 71114 | 155 |
|
156 |
def chown(arg: String, path: Path): Unit = |
|
| 71123 | 157 |
bash("chown " + arg + " " + File.bash_path(path)).check
|
| 71114 | 158 |
|
159 |
||
| 64189 | 160 |
/* directories */ |
|
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
|
161 |
|
| 75393 | 162 |
def make_directory(path: Path): Path = {
|
|
73325
a89f56ab2686
more robust (amending 87403fde8cc3): notably allow symlink to existing directory, which Files.createDirectories does not accept;
wenzelm
parents:
73324
diff
changeset
|
163 |
if (!path.is_dir) {
|
| 73945 | 164 |
try { Files.createDirectories(path.java_path) }
|
|
73325
a89f56ab2686
more robust (amending 87403fde8cc3): notably allow symlink to existing directory, which Files.createDirectories does not accept;
wenzelm
parents:
73324
diff
changeset
|
165 |
catch { case ERROR(_) => error("Failed to create directory: " + path.absolute) }
|
|
a89f56ab2686
more robust (amending 87403fde8cc3): notably allow symlink to existing directory, which Files.createDirectories does not accept;
wenzelm
parents:
73324
diff
changeset
|
166 |
} |
| 72376 | 167 |
path |
168 |
} |
|
|
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
|
169 |
|
| 72377 | 170 |
def new_directory(path: Path): Path = |
|
72426
f5d60c12deeb
more standard path output (despite platform_path from d55eb82ae77b);
wenzelm
parents:
72414
diff
changeset
|
171 |
if (path.is_dir) error("Directory already exists: " + path.absolute)
|
| 72377 | 172 |
else make_directory(path) |
173 |
||
|
76621
7af197063e2f
clarified signature: copy directory content more directly;
wenzelm
parents:
76620
diff
changeset
|
174 |
def copy_dir(dir1: Path, dir2: Path, direct: Boolean = false): Unit = {
|
| 76620 | 175 |
def make_path(dir: Path): String = {
|
|
76621
7af197063e2f
clarified signature: copy directory content more directly;
wenzelm
parents:
76620
diff
changeset
|
176 |
val s = File.standard_path(dir.absolute) |
|
7af197063e2f
clarified signature: copy directory content more directly;
wenzelm
parents:
76620
diff
changeset
|
177 |
if (direct) Url.direct_path(s) else s |
| 73322 | 178 |
} |
| 76620 | 179 |
val p1 = make_path(dir1) |
180 |
val p2 = make_path(dir2) |
|
| 76624 | 181 |
make_directory(if (direct) dir2.absolute else dir2.absolute.dir) |
| 76620 | 182 |
val res = bash("cp -a " + Bash.string(p1) + " " + Bash.string(p2))
|
183 |
if (!res.ok) cat_error("Failed to copy " + quote(p1) + " to " + quote(p2), res.err)
|
|
| 73322 | 184 |
} |
| 73317 | 185 |
|
| 75393 | 186 |
def with_copy_dir[A](dir1: Path, dir2: Path)(body: => A): A = {
|
| 76625 | 187 |
new_directory(dir2) |
188 |
try { copy_dir(dir1, dir2, direct = true); body }
|
|
189 |
finally { rm_tree(dir2) }
|
|
|
75230
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
190 |
} |
|
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
191 |
|
| 73317 | 192 |
|
| 75393 | 193 |
object Make_Directory extends Scala.Fun_Strings("make_directory") {
|
| 73322 | 194 |
val here = Scala_Project.here |
| 73567 | 195 |
def apply(args: List[String]): List[String] = apply_paths1(args, make_directory) |
| 73322 | 196 |
} |
| 73317 | 197 |
|
| 75393 | 198 |
object Copy_Dir extends Scala.Fun_Strings("copy_dir") {
|
| 73322 | 199 |
val here = Scala_Project.here |
|
76621
7af197063e2f
clarified signature: copy directory content more directly;
wenzelm
parents:
76620
diff
changeset
|
200 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_dir(_, _)) |
| 73322 | 201 |
} |
202 |
||
203 |
||
204 |
/* copy files */ |
|
| 64189 | 205 |
|
| 75393 | 206 |
def copy_file(src: JFile, dst: JFile): Unit = {
|
| 73317 | 207 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
| 73319 | 208 |
if (!File.eq(src, target)) {
|
| 73322 | 209 |
try {
|
210 |
Files.copy(src.toPath, target.toPath, |
|
211 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
212 |
StandardCopyOption.REPLACE_EXISTING) |
|
213 |
} |
|
214 |
catch {
|
|
215 |
case ERROR(msg) => |
|
| 73651 | 216 |
cat_error("Failed to copy file " +
|
| 73322 | 217 |
File.path(src).absolute + " to " + File.path(dst).absolute, msg) |
218 |
} |
|
| 73317 | 219 |
} |
220 |
} |
|
221 |
||
| 73320 | 222 |
def copy_file(src: Path, dst: Path): Unit = copy_file(src.file, dst.file) |
| 73317 | 223 |
|
| 75393 | 224 |
def copy_file_base(base_dir: Path, src: Path, target_dir: Path): Unit = {
|
| 73320 | 225 |
val src1 = src.expand |
226 |
val src1_dir = src1.dir |
|
| 76623 | 227 |
if (!src1.starts_basic) {
|
228 |
error("Illegal path specification " + src1 + " beyond base directory " + base_dir.absolute)
|
|
229 |
} |
|
| 73320 | 230 |
copy_file(base_dir + src1, Isabelle_System.make_directory(target_dir + src1_dir)) |
| 73317 | 231 |
} |
232 |
||
233 |
||
| 75393 | 234 |
object Copy_File extends Scala.Fun_Strings("copy_file") {
|
| 73322 | 235 |
val here = Scala_Project.here |
| 73567 | 236 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_file) |
| 73322 | 237 |
} |
238 |
||
| 75393 | 239 |
object Copy_File_Base extends Scala.Fun_Strings("copy_file_base") {
|
| 73322 | 240 |
val here = Scala_Project.here |
| 73567 | 241 |
def apply(args: List[String]): List[String] = apply_paths3(args, copy_file_base) |
| 73322 | 242 |
} |
243 |
||
244 |
||
245 |
/* move files */ |
|
| 73317 | 246 |
|
| 75393 | 247 |
def move_file(src: JFile, dst: JFile): Unit = {
|
| 73317 | 248 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
| 73319 | 249 |
if (!File.eq(src, target)) |
| 73317 | 250 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
251 |
} |
|
252 |
||
| 73320 | 253 |
def move_file(src: Path, dst: Path): Unit = move_file(src.file, dst.file) |
| 73317 | 254 |
|
255 |
||
256 |
/* symbolic link */ |
|
257 |
||
| 75393 | 258 |
def symlink(src: Path, dst: Path, force: Boolean = false, native: Boolean = false): Unit = {
|
| 73317 | 259 |
val src_file = src.file |
260 |
val dst_file = dst.file |
|
261 |
val target = if (dst_file.isDirectory) new JFile(dst_file, src_file.getName) else dst_file |
|
262 |
||
263 |
if (force) target.delete |
|
264 |
||
| 75393 | 265 |
def cygwin_link(): Unit = {
|
| 74070 | 266 |
if (native) {
|
267 |
error("Failed to create native symlink on Windows: " + quote(src_file.toString) +
|
|
268 |
"\n(but it could work as Administrator)") |
|
269 |
} |
|
270 |
else isabelle.setup.Environment.cygwin_link(File.standard_path(src), target) |
|
271 |
} |
|
272 |
||
| 73893 | 273 |
|
| 73317 | 274 |
try { Files.createSymbolicLink(target.toPath, src_file.toPath) }
|
275 |
catch {
|
|
| 73893 | 276 |
case _: UnsupportedOperationException if Platform.is_windows => cygwin_link() |
277 |
case _: FileSystemException if Platform.is_windows => cygwin_link() |
|
| 73317 | 278 |
} |
279 |
} |
|
280 |
||
|
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
|
281 |
|
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
282 |
/* tmp files */ |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
283 |
|
| 75393 | 284 |
def isabelle_tmp_prefix(): JFile = {
|
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
285 |
val path = Path.explode("$ISABELLE_TMP_PREFIX")
|
| 72375 | 286 |
path.file.mkdirs // low-level mkdirs to avoid recursion via Isabelle environment |
| 60988 | 287 |
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
|
288 |
} |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
289 |
|
| 76144 | 290 |
def tmp_file( |
291 |
name: String, |
|
292 |
ext: String = "", |
|
293 |
base_dir: JFile = isabelle_tmp_prefix(), |
|
294 |
initialized: Boolean = true |
|
295 |
): JFile = {
|
|
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
296 |
val suffix = if (ext == "") "" else "." + ext |
| 67924 | 297 |
val file = Files.createTempFile(base_dir.toPath, name, suffix).toFile |
| 76144 | 298 |
if (initialized) file.deleteOnExit() else file.delete() |
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
299 |
file |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
300 |
} |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
301 |
|
| 76177 | 302 |
def with_tmp_file[A]( |
303 |
name: String, |
|
304 |
ext: String = "", |
|
305 |
base_dir: JFile = isabelle_tmp_prefix() |
|
306 |
)(body: Path => A): A = {
|
|
307 |
val file = tmp_file(name, ext, base_dir = base_dir) |
|
| 64220 | 308 |
try { body(File.path(file)) } finally { file.delete }
|
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
309 |
} |
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
310 |
|
|
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
311 |
|
| 56477 | 312 |
/* tmp dirs */ |
313 |
||
| 75393 | 314 |
def rm_tree(root: JFile): Unit = {
|
| 56477 | 315 |
root.delete |
316 |
if (root.isDirectory) {
|
|
317 |
Files.walkFileTree(root.toPath, |
|
318 |
new SimpleFileVisitor[JPath] {
|
|
| 75393 | 319 |
override def visitFile(file: JPath, attrs: BasicFileAttributes): FileVisitResult = {
|
|
68991
6c1beb52d766
more robust: avoid race condition wrt. cleanup of ML process, e.g. relevant for "$ISABELLE_TMP/rat.ML" in theory Codegen.Further;
wenzelm
parents:
68409
diff
changeset
|
320 |
try { Files.deleteIfExists(file) }
|
|
6c1beb52d766
more robust: avoid race condition wrt. cleanup of ML process, e.g. relevant for "$ISABELLE_TMP/rat.ML" in theory Codegen.Further;
wenzelm
parents:
68409
diff
changeset
|
321 |
catch { case _: IOException => }
|
| 56477 | 322 |
FileVisitResult.CONTINUE |
323 |
} |
|
324 |
||
| 75393 | 325 |
override def postVisitDirectory(dir: JPath, e: IOException): FileVisitResult = {
|
| 56477 | 326 |
if (e == null) {
|
|
68991
6c1beb52d766
more robust: avoid race condition wrt. cleanup of ML process, e.g. relevant for "$ISABELLE_TMP/rat.ML" in theory Codegen.Further;
wenzelm
parents:
68409
diff
changeset
|
327 |
try { Files.deleteIfExists(dir) }
|
|
6c1beb52d766
more robust: avoid race condition wrt. cleanup of ML process, e.g. relevant for "$ISABELLE_TMP/rat.ML" in theory Codegen.Further;
wenzelm
parents:
68409
diff
changeset
|
328 |
catch { case _: IOException => }
|
| 56477 | 329 |
FileVisitResult.CONTINUE |
330 |
} |
|
331 |
else throw e |
|
332 |
} |
|
333 |
} |
|
334 |
) |
|
335 |
} |
|
336 |
} |
|
337 |
||
| 73324 | 338 |
def rm_tree(root: Path): Unit = rm_tree(root.file) |
339 |
||
| 75393 | 340 |
object Rm_Tree extends Scala.Fun_Strings("rm_tree") {
|
| 73324 | 341 |
val here = Scala_Project.here |
| 73567 | 342 |
def apply(args: List[String]): List[String] = apply_paths1(args, rm_tree) |
| 73324 | 343 |
} |
344 |
||
| 75393 | 345 |
def tmp_dir(name: String, base_dir: JFile = isabelle_tmp_prefix()): JFile = {
|
| 67924 | 346 |
val dir = Files.createTempDirectory(base_dir.toPath, name).toFile |
| 75219 | 347 |
dir.deleteOnExit() |
| 56477 | 348 |
dir |
349 |
} |
|
350 |
||
| 76177 | 351 |
def with_tmp_dir[A]( |
352 |
name: String, |
|
353 |
base_dir: JFile = isabelle_tmp_prefix() |
|
354 |
)(body: Path => A): A = {
|
|
355 |
val dir = tmp_dir(name, base_dir = base_dir) |
|
| 64220 | 356 |
try { body(File.path(dir)) } finally { rm_tree(dir) }
|
| 56477 | 357 |
} |
358 |
||
359 |
||
| 65793 | 360 |
/* quasi-atomic update of directory */ |
361 |
||
| 75393 | 362 |
def update_directory(dir: Path, f: Path => Unit): Unit = {
|
| 65793 | 363 |
val new_dir = dir.ext("new")
|
364 |
val old_dir = dir.ext("old")
|
|
365 |
||
366 |
rm_tree(new_dir) |
|
367 |
rm_tree(old_dir) |
|
368 |
||
369 |
f(new_dir) |
|
370 |
||
| 73317 | 371 |
if (dir.is_dir) move_file(dir, old_dir) |
372 |
move_file(new_dir, dir) |
|
| 65793 | 373 |
rm_tree(old_dir) |
374 |
} |
|
375 |
||
376 |
||
| 76145 | 377 |
/* TCP/IP ports */ |
378 |
||
379 |
def local_port(): Int = {
|
|
380 |
val socket = new ServerSocket(0) |
|
381 |
val port = socket.getLocalPort |
|
382 |
socket.close() |
|
383 |
port |
|
384 |
} |
|
385 |
||
| 62613 | 386 |
|
| 76146 | 387 |
/* JVM shutdown hook */ |
388 |
||
389 |
def create_shutdown_hook(body: => Unit): Thread = {
|
|
390 |
val shutdown_hook = Isabelle_Thread.create(new Runnable { def run: Unit = body })
|
|
391 |
||
392 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) }
|
|
393 |
catch { case _: IllegalStateException => }
|
|
394 |
||
395 |
shutdown_hook |
|
396 |
} |
|
397 |
||
398 |
def remove_shutdown_hook(shutdown_hook: Thread): Unit = |
|
399 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) }
|
|
400 |
catch { case _: IllegalStateException => }
|
|
401 |
||
402 |
||
403 |
||
| 62613 | 404 |
/** external processes **/ |
405 |
||
406 |
/* GNU bash */ |
|
| 31796 | 407 |
|
|
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
408 |
def bash(script: String, |
| 74212 | 409 |
description: String = "", |
|
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
410 |
cwd: JFile = null, |
| 73897 | 411 |
env: JMap[String, String] = settings(), |
|
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
412 |
redirect: Boolean = false, |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
413 |
input: 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
|
414 |
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
|
415 |
progress_stderr: String => Unit = (_: String) => (), |
| 72598 | 416 |
watchdog: Option[Bash.Watchdog] = None, |
| 62602 | 417 |
strict: Boolean = true, |
| 75393 | 418 |
cleanup: () => Unit = () => () |
419 |
): Process_Result = {
|
|
| 74212 | 420 |
Bash.process(script, |
421 |
description = description, cwd = cwd, env = env, redirect = redirect, cleanup = cleanup). |
|
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
422 |
result(input = input, progress_stdout = progress_stdout, progress_stderr = progress_stderr, |
| 72598 | 423 |
watchdog = watchdog, strict = strict) |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
424 |
} |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
425 |
|
| 75218 | 426 |
|
427 |
/* command-line tools */ |
|
428 |
||
| 75393 | 429 |
def require_command(cmd: String, test: String = "--version"): Unit = {
|
| 75218 | 430 |
if (!bash(Bash.string(cmd) + " " + test).ok) error("Missing system command: " + quote(cmd))
|
431 |
} |
|
432 |
||
|
64935
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
433 |
private lazy val gnutar_check: Boolean = |
|
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
434 |
try { bash("tar --version").check.out.containsSlice("GNU tar") || error("") }
|
|
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
435 |
catch { case ERROR(_) => false }
|
|
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
436 |
|
| 69425 | 437 |
def gnutar( |
438 |
args: String, |
|
439 |
dir: Path = Path.current, |
|
440 |
original_owner: Boolean = false, |
|
|
76540
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
441 |
strip: Boolean = false, |
| 75393 | 442 |
redirect: Boolean = false |
443 |
): Process_Result = {
|
|
| 69425 | 444 |
val options = |
445 |
(if (dir.is_current) "" else "-C " + File.bash_path(dir) + " ") + |
|
| 73624 | 446 |
(if (original_owner) "" else "--owner=root --group=staff ") + |
|
76540
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
447 |
(if (!strip) "" else "--strip-components=1 ") |
| 69425 | 448 |
|
449 |
if (gnutar_check) bash("tar " + options + args, redirect = redirect)
|
|
|
64935
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
450 |
else error("Expected to find GNU tar executable")
|
|
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
451 |
} |
|
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
452 |
|
|
76540
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
453 |
def extract(archive: Path, dir: Path, strip: Boolean = false): Unit = {
|
| 76533 | 454 |
val name = archive.file_name |
| 76541 | 455 |
make_directory(dir) |
| 76546 | 456 |
if (File.is_zip(name) || File.is_jar(name)) {
|
457 |
using(new ZipFile(archive.file)) { zip_file =>
|
|
458 |
val items = |
|
459 |
for (entry <- zip_file.entries().asScala.toList) |
|
460 |
yield {
|
|
461 |
val input = JPath.of(entry.getName) |
|
462 |
val count = input.getNameCount |
|
463 |
val output = |
|
464 |
if (strip && count <= 1) None |
|
465 |
else if (strip) Some(input.subpath(1, count)) |
|
466 |
else Some(input) |
|
467 |
val result = output.map(dir.java_path.resolve(_)) |
|
468 |
for (res <- result) {
|
|
469 |
if (entry.isDirectory) Files.createDirectories(res) |
|
470 |
else {
|
|
471 |
val bytes = using(zip_file.getInputStream(entry))(Bytes.read_stream(_)) |
|
472 |
Files.createDirectories(res.getParent) |
|
473 |
Files.write(res, bytes.array) |
|
474 |
} |
|
475 |
} |
|
476 |
(entry, result) |
|
477 |
} |
|
478 |
for {
|
|
479 |
(entry, Some(res)) <- items |
|
480 |
if !entry.isDirectory |
|
481 |
t <- Option(entry.getLastModifiedTime) |
|
482 |
} Files.setLastModifiedTime(res, t) |
|
483 |
} |
|
|
76543
fef0195f8d8e
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76541
diff
changeset
|
484 |
} |
|
76540
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
485 |
else if (File.is_tar_bz2(name) || File.is_tgz(name) || File.is_tar_gz(name)) {
|
|
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
486 |
val flags = if (File.is_tar_bz2(name)) "-xjf " else "-xzf " |
|
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
wenzelm
parents:
76533
diff
changeset
|
487 |
Isabelle_System.gnutar(flags + File.bash_path(archive), dir = dir, strip = strip).check |
| 76533 | 488 |
} |
489 |
else error("Cannot extract " + archive)
|
|
490 |
} |
|
| 76530 | 491 |
|
| 75393 | 492 |
def make_patch(base_dir: Path, src: Path, dst: Path, diff_options: String = ""): String = {
|
| 75394 | 493 |
with_tmp_file("patch") { patch =>
|
| 75229 | 494 |
Isabelle_System.bash( |
|
75230
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
495 |
"diff -ru " + diff_options + " -- " + File.bash_path(src) + " " + File.bash_path(dst) + |
|
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
496 |
" > " + File.bash_path(patch), |
| 75229 | 497 |
cwd = base_dir.file).check_rc(_ <= 1) |
498 |
File.read(patch) |
|
| 75394 | 499 |
} |
| 75220 | 500 |
} |
501 |
||
| 64152 | 502 |
def hostname(): String = bash("hostname -s").check.out
|
| 64139 | 503 |
|
| 54690 | 504 |
def open(arg: String): Unit = |
| 64304 | 505 |
bash("exec \"$ISABELLE_OPEN\" " + Bash.string(arg) + " >/dev/null 2>/dev/null &")
|
| 54690 | 506 |
|
507 |
def pdf_viewer(arg: Path): Unit = |
|
| 62616 | 508 |
bash("exec \"$PDF_VIEWER\" " + File.bash_path(arg) + " >/dev/null 2>/dev/null &")
|
| 54690 | 509 |
|
| 75393 | 510 |
def open_external_file(name: String): Boolean = {
|
|
73224
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
511 |
val ext = Library.take_suffix((c: Char) => c != '.', name.toList)._2.mkString |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
512 |
val external = |
| 77218 | 513 |
ext.nonEmpty && space_explode(':', getenv("ISABELLE_EXTERNAL_FILES")).contains(ext)
|
|
73224
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
514 |
if (external) {
|
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
515 |
if (ext == "pdf" && Path.is_wellformed(name)) pdf_viewer(Path.explode(name)) |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
516 |
else open(name) |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
517 |
} |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
518 |
external |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
519 |
} |
|
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
520 |
|
| 65916 | 521 |
|
| 32450 | 522 |
|
| 31796 | 523 |
/** Isabelle resources **/ |
524 |
||
| 64161 | 525 |
/* repository clone with Admin */ |
526 |
||
527 |
def admin(): Boolean = Path.explode("~~/Admin").is_dir
|
|
528 |
||
529 |
||
| 62633 | 530 |
/* default logic */ |
| 48503 | 531 |
|
| 75393 | 532 |
def default_logic(args: String*): String = {
|
|
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
533 |
args.find(_ != "") match {
|
|
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
534 |
case Some(logic) => logic |
| 73317 | 535 |
case None => getenv_strict("ISABELLE_LOGIC")
|
|
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
536 |
} |
|
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
537 |
} |
| 73323 | 538 |
|
539 |
||
540 |
/* download file */ |
|
541 |
||
| 75393 | 542 |
def download(url_name: String, progress: Progress = new Progress): HTTP.Content = {
|
| 73335 | 543 |
val url = Url(url_name) |
544 |
progress.echo("Getting " + quote(url_name))
|
|
| 73566 | 545 |
try { HTTP.Client.get(url) }
|
546 |
catch { case ERROR(msg) => cat_error("Failed to download " + quote(url_name), msg) }
|
|
| 73323 | 547 |
} |
548 |
||
| 73566 | 549 |
def download_file(url_name: String, file: Path, progress: Progress = new Progress): Unit = |
550 |
Bytes.write(file, download(url_name, progress = progress).bytes) |
|
551 |
||
| 75393 | 552 |
object Download extends Scala.Fun("download", thread = true) {
|
| 73323 | 553 |
val here = Scala_Project.here |
| 77027 | 554 |
override def invoke(session: Session, args: List[Bytes]): List[Bytes] = |
| 75439 | 555 |
args.map(url => download(url.text).bytes) |
| 73323 | 556 |
} |
| 73608 | 557 |
|
558 |
||
559 |
/* repositories */ |
|
560 |
||
| 73611 | 561 |
val isabelle_repository: Mercurial.Server = |
562 |
Mercurial.Server("https://isabelle.sketis.net/repos/isabelle")
|
|
| 73608 | 563 |
|
| 73611 | 564 |
val afp_repository: Mercurial.Server = |
565 |
Mercurial.Server("https://isabelle.sketis.net/repos/afp-devel")
|
|
| 73610 | 566 |
|
567 |
def official_releases(): List[String] = |
|
568 |
Library.trim_split_lines( |
|
569 |
isabelle_repository.read_file(Path.explode("Admin/Release/official")))
|
|
| 27919 | 570 |
} |