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