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