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