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