author | wenzelm |
Mon, 25 Jul 2022 11:19:08 +0200 | |
changeset 75695 | 14e22b525b13 |
parent 75692 | 048bbe0bf807 |
child 75697 | 21c1f82e7f5d |
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} |
67835 | 11 |
import java.io.{File => JFile, IOException} |
73317 | 12 |
import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult, |
13 |
StandardCopyOption, FileSystemException} |
|
56477 | 14 |
import java.nio.file.attribute.BasicFileAttributes |
27936 | 15 |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
16 |
import scala.jdk.CollectionConverters._ |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
17 |
|
27919 | 18 |
|
75393 | 19 |
object Isabelle_System { |
75218 | 20 |
/* settings environment */ |
56477 | 21 |
|
75393 | 22 |
def settings(putenv: List[(String, String)] = Nil): JMap[String, String] = { |
74146 | 23 |
val env0 = isabelle.setup.Environment.settings() |
24 |
if (putenv.isEmpty) env0 |
|
25 |
else { |
|
26 |
val env = new HashMap(env0) |
|
27 |
for ((a, b) <- putenv) env.put(a, b) |
|
28 |
env |
|
29 |
} |
|
30 |
} |
|
71739 | 31 |
|
73897 | 32 |
def getenv(name: String, env: JMap[String, String] = settings()): String = |
33 |
Option(env.get(name)).getOrElse("") |
|
31498 | 34 |
|
73897 | 35 |
def getenv_strict(name: String, env: JMap[String, String] = settings()): String = |
65717 | 36 |
proper_string(getenv(name, env)) getOrElse |
37 |
error("Undefined Isabelle environment variable: " + quote(name)) |
|
27919 | 38 |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
39 |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
40 |
/* services */ |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
41 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
42 |
abstract class Service |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
43 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
44 |
@volatile private var _services: Option[List[Class[Service]]] = None |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
45 |
|
75393 | 46 |
def services(): List[Class[Service]] = { |
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
47 |
if (_services.isEmpty) init() // unsynchronized check |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
48 |
_services.get |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
49 |
} |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
50 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
51 |
def make_services[C](c: Class[C]): List[C] = |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
52 |
for { c1 <- services() if Library.is_subclass(c1, c) } |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
53 |
yield c1.getDeclaredConstructor().newInstance().asInstanceOf[C] |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
54 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
55 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
56 |
/* init settings + services */ |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
57 |
|
75692 | 58 |
private def init_services(where: String, names: List[String]): List[Class[Service]] = { |
59 |
for (name <- names) yield { |
|
60 |
def err(msg: String): Nothing = |
|
61 |
error("Bad Isabelle/Scala service " + quote(name) + " in " + where + "\n" + msg) |
|
62 |
try { Class.forName(name).asInstanceOf[Class[Service]] } |
|
63 |
catch { |
|
64 |
case _: ClassNotFoundException => err("Class not found") |
|
65 |
case exn: Throwable => err(Exn.message(exn)) |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
66 |
} |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
67 |
} |
75692 | 68 |
} |
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
69 |
|
75692 | 70 |
def init_services_env(): List[Class[Service]] = |
71 |
{ |
|
72 |
val variable = "ISABELLE_SCALA_SERVICES" |
|
73 |
init_services(quote(variable), space_explode(':', getenv_strict(variable))) |
|
74 |
} |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73945
diff
changeset
|
75 |
|
75692 | 76 |
def init_services_jar(jar: Path): List[Class[Service]] = |
77 |
init_services(jar.toString, isabelle.setup.Build.get_services(jar.java_path).asScala.toList) |
|
78 |
||
75393 | 79 |
def init(isabelle_root: String = "", cygwin_root: String = ""): Unit = { |
73906 | 80 |
isabelle.setup.Environment.init(isabelle_root, cygwin_root) |
75692 | 81 |
synchronized { |
82 |
if (_services.isEmpty) { |
|
83 |
_services = Some(init_services_env() ::: Scala.get_classpath().flatMap(init_services_jar)) |
|
84 |
} |
|
85 |
} |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
86 |
} |
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
87 |
|
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
88 |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
89 |
/* getetc -- static distribution parameters */ |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
90 |
|
75393 | 91 |
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
|
92 |
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
|
93 |
if (path.is_file) { |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
94 |
Library.trim_split_lines(File.read(path)) match { |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
95 |
case Nil => None |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
96 |
case List(s) => Some(s) |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
97 |
case _ => error("Single line expected in " + path.absolute) |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
98 |
} |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
99 |
} |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
100 |
else None |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
101 |
} |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
102 |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
103 |
|
73521 | 104 |
/* Isabelle distribution identification */ |
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73445
diff
changeset
|
105 |
|
73525 | 106 |
def isabelle_id(root: Path = Path.ISABELLE_HOME): String = |
75510 | 107 |
getetc("ISABELLE_ID", root = root) orElse |
108 |
Mercurial.archive_id(root) orElse |
|
109 |
Mercurial.id_repository(root, rev = "") getOrElse |
|
110 |
error("Failed to identify Isabelle distribution " + root.expand) |
|
67865 | 111 |
|
75393 | 112 |
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
|
113 |
val here = Scala_Project.here |
73525 | 114 |
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
|
115 |
} |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
116 |
|
73522 | 117 |
def isabelle_tags(root: Path = Path.ISABELLE_HOME): String = |
73521 | 118 |
getetc("ISABELLE_TAGS", root = root) orElse Mercurial.archive_tags(root) getOrElse { |
119 |
if (Mercurial.is_repository(root)) { |
|
120 |
val hg = Mercurial.repository(root) |
|
121 |
hg.tags(rev = hg.parent()) |
|
122 |
} |
|
123 |
else "" |
|
124 |
} |
|
31796 | 125 |
|
75218 | 126 |
def export_isabelle_identifier(isabelle_identifier: String): String = |
75563
5bba3516ddb5
more robust: always override ISABELLE_IDENTIFIER from environment;
wenzelm
parents:
75523
diff
changeset
|
127 |
"export ISABELLE_IDENTIFIER=" + Bash.string(isabelle_identifier) + "\n" |
75218 | 128 |
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
129 |
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
|
130 |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
131 |
def isabelle_heading(): String = |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
132 |
isabelle_identifier() match { |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
133 |
case None => "" |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
134 |
case Some(version) => " (" + version + ")" |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
135 |
} |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
136 |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73522
diff
changeset
|
137 |
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
|
138 |
|
74336
7bb0ac635397
permissive identification, e.g. relevant for HOL-SPARK examples running on rsync-clone;
wenzelm
parents:
74268
diff
changeset
|
139 |
def identification(): String = |
7bb0ac635397
permissive identification, e.g. relevant for HOL-SPARK examples running on rsync-clone;
wenzelm
parents:
74268
diff
changeset
|
140 |
"Isabelle" + (try { "/" + isabelle_id () } catch { case ERROR(_) => "" }) + isabelle_heading() |
73547 | 141 |
|
54039 | 142 |
|
43606 | 143 |
/** file-system operations **/ |
31796 | 144 |
|
73322 | 145 |
/* scala functions */ |
146 |
||
75437 | 147 |
private def apply_paths( |
148 |
args: List[String], |
|
149 |
fun: PartialFunction[List[Path], Unit] |
|
150 |
): List[String] = { |
|
75393 | 151 |
fun(args.map(Path.explode)) |
152 |
Nil |
|
153 |
} |
|
73322 | 154 |
|
73567 | 155 |
private def apply_paths1(args: List[String], fun: Path => Unit): List[String] = |
75437 | 156 |
apply_paths(args, { case List(path) => fun(path) }) |
73322 | 157 |
|
73567 | 158 |
private def apply_paths2(args: List[String], fun: (Path, Path) => Unit): List[String] = |
75437 | 159 |
apply_paths(args, { case List(path1, path2) => fun(path1, path2) }) |
73322 | 160 |
|
73567 | 161 |
private def apply_paths3(args: List[String], fun: (Path, Path, Path) => Unit): List[String] = |
75437 | 162 |
apply_paths(args, { case List(path1, path2, path3) => fun(path1, path2, path3) }) |
73322 | 163 |
|
164 |
||
71114 | 165 |
/* permissions */ |
166 |
||
167 |
def chmod(arg: String, path: Path): Unit = |
|
71123 | 168 |
bash("chmod " + arg + " " + File.bash_path(path)).check |
71114 | 169 |
|
170 |
def chown(arg: String, path: Path): Unit = |
|
71123 | 171 |
bash("chown " + arg + " " + File.bash_path(path)).check |
71114 | 172 |
|
173 |
||
64189 | 174 |
/* 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
|
175 |
|
75393 | 176 |
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
|
177 |
if (!path.is_dir) { |
73945 | 178 |
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
|
179 |
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
|
180 |
} |
72376 | 181 |
path |
182 |
} |
|
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
|
183 |
|
72377 | 184 |
def new_directory(path: Path): Path = |
72426
f5d60c12deeb
more standard path output (despite platform_path from d55eb82ae77b);
wenzelm
parents:
72414
diff
changeset
|
185 |
if (path.is_dir) error("Directory already exists: " + path.absolute) |
72377 | 186 |
else make_directory(path) |
187 |
||
75393 | 188 |
def copy_dir(dir1: Path, dir2: Path): Unit = { |
73322 | 189 |
val res = bash("cp -a " + File.bash_path(dir1) + " " + File.bash_path(dir2)) |
190 |
if (!res.ok) { |
|
191 |
cat_error("Failed to copy directory " + dir1.absolute + " to " + dir2.absolute, res.err) |
|
192 |
} |
|
193 |
} |
|
73317 | 194 |
|
75393 | 195 |
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
|
196 |
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
|
197 |
else { |
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
198 |
try { copy_dir(dir1, dir2); body } |
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
199 |
finally { rm_tree(dir2 ) } |
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
200 |
} |
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
201 |
} |
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
202 |
|
73317 | 203 |
|
75393 | 204 |
object Make_Directory extends Scala.Fun_Strings("make_directory") { |
73322 | 205 |
val here = Scala_Project.here |
73567 | 206 |
def apply(args: List[String]): List[String] = apply_paths1(args, make_directory) |
73322 | 207 |
} |
73317 | 208 |
|
75393 | 209 |
object Copy_Dir extends Scala.Fun_Strings("copy_dir") { |
73322 | 210 |
val here = Scala_Project.here |
73567 | 211 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_dir) |
73322 | 212 |
} |
213 |
||
214 |
||
215 |
/* copy files */ |
|
64189 | 216 |
|
75393 | 217 |
def copy_file(src: JFile, dst: JFile): Unit = { |
73317 | 218 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
73319 | 219 |
if (!File.eq(src, target)) { |
73322 | 220 |
try { |
221 |
Files.copy(src.toPath, target.toPath, |
|
222 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
223 |
StandardCopyOption.REPLACE_EXISTING) |
|
224 |
} |
|
225 |
catch { |
|
226 |
case ERROR(msg) => |
|
73651 | 227 |
cat_error("Failed to copy file " + |
73322 | 228 |
File.path(src).absolute + " to " + File.path(dst).absolute, msg) |
229 |
} |
|
73317 | 230 |
} |
231 |
} |
|
232 |
||
73320 | 233 |
def copy_file(src: Path, dst: Path): Unit = copy_file(src.file, dst.file) |
73317 | 234 |
|
75393 | 235 |
def copy_file_base(base_dir: Path, src: Path, target_dir: Path): Unit = { |
73320 | 236 |
val src1 = src.expand |
237 |
val src1_dir = src1.dir |
|
73321 | 238 |
if (!src1.starts_basic) error("Illegal path specification " + src1 + " beyond base directory") |
73320 | 239 |
copy_file(base_dir + src1, Isabelle_System.make_directory(target_dir + src1_dir)) |
73317 | 240 |
} |
241 |
||
242 |
||
75393 | 243 |
object Copy_File extends Scala.Fun_Strings("copy_file") { |
73322 | 244 |
val here = Scala_Project.here |
73567 | 245 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_file) |
73322 | 246 |
} |
247 |
||
75393 | 248 |
object Copy_File_Base extends Scala.Fun_Strings("copy_file_base") { |
73322 | 249 |
val here = Scala_Project.here |
73567 | 250 |
def apply(args: List[String]): List[String] = apply_paths3(args, copy_file_base) |
73322 | 251 |
} |
252 |
||
253 |
||
254 |
/* move files */ |
|
73317 | 255 |
|
75393 | 256 |
def move_file(src: JFile, dst: JFile): Unit = { |
73317 | 257 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
73319 | 258 |
if (!File.eq(src, target)) |
73317 | 259 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
260 |
} |
|
261 |
||
73320 | 262 |
def move_file(src: Path, dst: Path): Unit = move_file(src.file, dst.file) |
73317 | 263 |
|
264 |
||
265 |
/* symbolic link */ |
|
266 |
||
75393 | 267 |
def symlink(src: Path, dst: Path, force: Boolean = false, native: Boolean = false): Unit = { |
73317 | 268 |
val src_file = src.file |
269 |
val dst_file = dst.file |
|
270 |
val target = if (dst_file.isDirectory) new JFile(dst_file, src_file.getName) else dst_file |
|
271 |
||
272 |
if (force) target.delete |
|
273 |
||
75393 | 274 |
def cygwin_link(): Unit = { |
74070 | 275 |
if (native) { |
276 |
error("Failed to create native symlink on Windows: " + quote(src_file.toString) + |
|
277 |
"\n(but it could work as Administrator)") |
|
278 |
} |
|
279 |
else isabelle.setup.Environment.cygwin_link(File.standard_path(src), target) |
|
280 |
} |
|
281 |
||
73893 | 282 |
|
73317 | 283 |
try { Files.createSymbolicLink(target.toPath, src_file.toPath) } |
284 |
catch { |
|
73893 | 285 |
case _: UnsupportedOperationException if Platform.is_windows => cygwin_link() |
286 |
case _: FileSystemException if Platform.is_windows => cygwin_link() |
|
73317 | 287 |
} |
288 |
} |
|
289 |
||
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
|
290 |
|
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
291 |
/* tmp files */ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
292 |
|
75393 | 293 |
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
|
294 |
val path = Path.explode("$ISABELLE_TMP_PREFIX") |
72375 | 295 |
path.file.mkdirs // low-level mkdirs to avoid recursion via Isabelle environment |
60988 | 296 |
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
|
297 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
298 |
|
75393 | 299 |
def tmp_file(name: String, ext: String = "", base_dir: JFile = 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
|
300 |
val suffix = if (ext == "") "" else "." + ext |
67924 | 301 |
val file = Files.createTempFile(base_dir.toPath, name, suffix).toFile |
75219 | 302 |
file.deleteOnExit() |
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 |
|
75393 | 306 |
def with_tmp_file[A](name: String, ext: String = "")(body: Path => A): A = { |
56428
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
307 |
val file = tmp_file(name, ext) |
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 |
||
75393 | 351 |
def with_tmp_dir[A](name: String)(body: Path => A): A = { |
56477 | 352 |
val dir = tmp_dir(name) |
64220 | 353 |
try { body(File.path(dir)) } finally { rm_tree(dir) } |
56477 | 354 |
} |
355 |
||
356 |
||
65793 | 357 |
/* quasi-atomic update of directory */ |
358 |
||
75393 | 359 |
def update_directory(dir: Path, f: Path => Unit): Unit = { |
65793 | 360 |
val new_dir = dir.ext("new") |
361 |
val old_dir = dir.ext("old") |
|
362 |
||
363 |
rm_tree(new_dir) |
|
364 |
rm_tree(old_dir) |
|
365 |
||
366 |
f(new_dir) |
|
367 |
||
73317 | 368 |
if (dir.is_dir) move_file(dir, old_dir) |
369 |
move_file(new_dir, dir) |
|
65793 | 370 |
rm_tree(old_dir) |
371 |
} |
|
372 |
||
373 |
||
62613 | 374 |
|
375 |
/** external processes **/ |
|
376 |
||
377 |
/* GNU bash */ |
|
31796 | 378 |
|
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
379 |
def bash(script: String, |
74212 | 380 |
description: String = "", |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
381 |
cwd: JFile = null, |
73897 | 382 |
env: JMap[String, String] = settings(), |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
383 |
redirect: Boolean = false, |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
384 |
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
|
385 |
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
|
386 |
progress_stderr: String => Unit = (_: String) => (), |
72598 | 387 |
watchdog: Option[Bash.Watchdog] = None, |
62602 | 388 |
strict: Boolean = true, |
75393 | 389 |
cleanup: () => Unit = () => () |
390 |
): Process_Result = { |
|
74212 | 391 |
Bash.process(script, |
392 |
description = description, cwd = cwd, env = env, redirect = redirect, cleanup = cleanup). |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
393 |
result(input = input, progress_stdout = progress_stdout, progress_stderr = progress_stderr, |
72598 | 394 |
watchdog = watchdog, strict = strict) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
395 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
396 |
|
75218 | 397 |
|
398 |
/* command-line tools */ |
|
399 |
||
75393 | 400 |
def require_command(cmd: String, test: String = "--version"): Unit = { |
75218 | 401 |
if (!bash(Bash.string(cmd) + " " + test).ok) error("Missing system command: " + quote(cmd)) |
402 |
} |
|
403 |
||
64935
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
404 |
private lazy val gnutar_check: Boolean = |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
405 |
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
|
406 |
catch { case ERROR(_) => false } |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
407 |
|
69425 | 408 |
def gnutar( |
409 |
args: String, |
|
410 |
dir: Path = Path.current, |
|
411 |
original_owner: Boolean = false, |
|
73624 | 412 |
strip: Int = 0, |
75393 | 413 |
redirect: Boolean = false |
414 |
): Process_Result = { |
|
69425 | 415 |
val options = |
416 |
(if (dir.is_current) "" else "-C " + File.bash_path(dir) + " ") + |
|
73624 | 417 |
(if (original_owner) "" else "--owner=root --group=staff ") + |
418 |
(if (strip <= 0) "" else "--strip-components=" + strip + " ") |
|
69425 | 419 |
|
420 |
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
|
421 |
else error("Expected to find GNU tar executable") |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
422 |
} |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
423 |
|
75393 | 424 |
def make_patch(base_dir: Path, src: Path, dst: Path, diff_options: String = ""): String = { |
75394 | 425 |
with_tmp_file("patch") { patch => |
75229 | 426 |
Isabelle_System.bash( |
75230
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75229
diff
changeset
|
427 |
"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
|
428 |
" > " + File.bash_path(patch), |
75229 | 429 |
cwd = base_dir.file).check_rc(_ <= 1) |
430 |
File.read(patch) |
|
75394 | 431 |
} |
75220 | 432 |
} |
433 |
||
64152 | 434 |
def hostname(): String = bash("hostname -s").check.out |
64139 | 435 |
|
54690 | 436 |
def open(arg: String): Unit = |
64304 | 437 |
bash("exec \"$ISABELLE_OPEN\" " + Bash.string(arg) + " >/dev/null 2>/dev/null &") |
54690 | 438 |
|
439 |
def pdf_viewer(arg: Path): Unit = |
|
62616 | 440 |
bash("exec \"$PDF_VIEWER\" " + File.bash_path(arg) + " >/dev/null 2>/dev/null &") |
54690 | 441 |
|
75393 | 442 |
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
|
443 |
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
|
444 |
val external = |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
445 |
ext.nonEmpty && |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
446 |
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
|
447 |
if (external) { |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
448 |
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
|
449 |
else open(name) |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
450 |
} |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
451 |
external |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
452 |
} |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
453 |
|
65916 | 454 |
|
32450 | 455 |
|
31796 | 456 |
/** Isabelle resources **/ |
457 |
||
64161 | 458 |
/* repository clone with Admin */ |
459 |
||
460 |
def admin(): Boolean = Path.explode("~~/Admin").is_dir |
|
461 |
||
462 |
||
62633 | 463 |
/* default logic */ |
48503 | 464 |
|
75393 | 465 |
def default_logic(args: String*): String = { |
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
466 |
args.find(_ != "") match { |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
467 |
case Some(logic) => logic |
73317 | 468 |
case None => getenv_strict("ISABELLE_LOGIC") |
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
469 |
} |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
470 |
} |
73323 | 471 |
|
472 |
||
473 |
/* download file */ |
|
474 |
||
75393 | 475 |
def download(url_name: String, progress: Progress = new Progress): HTTP.Content = { |
73335 | 476 |
val url = Url(url_name) |
477 |
progress.echo("Getting " + quote(url_name)) |
|
73566 | 478 |
try { HTTP.Client.get(url) } |
479 |
catch { case ERROR(msg) => cat_error("Failed to download " + quote(url_name), msg) } |
|
73323 | 480 |
} |
481 |
||
73566 | 482 |
def download_file(url_name: String, file: Path, progress: Progress = new Progress): Unit = |
483 |
Bytes.write(file, download(url_name, progress = progress).bytes) |
|
484 |
||
75393 | 485 |
object Download extends Scala.Fun("download", thread = true) { |
73323 | 486 |
val here = Scala_Project.here |
73566 | 487 |
override def invoke(args: List[Bytes]): List[Bytes] = |
75439 | 488 |
args.map(url => download(url.text).bytes) |
73323 | 489 |
} |
73608 | 490 |
|
491 |
||
492 |
/* repositories */ |
|
493 |
||
73611 | 494 |
val isabelle_repository: Mercurial.Server = |
495 |
Mercurial.Server("https://isabelle.sketis.net/repos/isabelle") |
|
73608 | 496 |
|
73611 | 497 |
val afp_repository: Mercurial.Server = |
498 |
Mercurial.Server("https://isabelle.sketis.net/repos/afp-devel") |
|
73610 | 499 |
|
500 |
def official_releases(): List[String] = |
|
501 |
Library.trim_split_lines( |
|
502 |
isabelle_repository.read_file(Path.explode("Admin/Release/official"))) |
|
27919 | 503 |
} |