author | wenzelm |
Thu, 09 Sep 2021 14:05:31 +0200 | |
changeset 74268 | d01920a8b082 |
parent 74212 | a1ccecae6a57 |
child 74336 | 7bb0ac635397 |
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 |
|
73547 | 142 |
def identification(): String = "Isabelle/" + isabelle_id() + isabelle_heading() |
143 |
||
54039 | 144 |
|
43606 | 145 |
/** file-system operations **/ |
31796 | 146 |
|
73322 | 147 |
/* scala functions */ |
148 |
||
73567 | 149 |
private def apply_paths(args: List[String], fun: List[Path] => Unit): List[String] = |
150 |
{ fun(args.map(Path.explode)); Nil } |
|
73322 | 151 |
|
73567 | 152 |
private def apply_paths1(args: List[String], fun: Path => Unit): List[String] = |
153 |
apply_paths(args, { case List(path) => fun(path) }) |
|
73322 | 154 |
|
73567 | 155 |
private def apply_paths2(args: List[String], fun: (Path, Path) => Unit): List[String] = |
156 |
apply_paths(args, { case List(path1, path2) => fun(path1, path2) }) |
|
73322 | 157 |
|
73567 | 158 |
private def apply_paths3(args: List[String], fun: (Path, Path, Path) => Unit): List[String] = |
159 |
apply_paths(args, { case List(path1, path2, path3) => fun(path1, path2, path3) }) |
|
73322 | 160 |
|
161 |
||
71114 | 162 |
/* permissions */ |
163 |
||
164 |
def chmod(arg: String, path: Path): Unit = |
|
71123 | 165 |
bash("chmod " + arg + " " + File.bash_path(path)).check |
71114 | 166 |
|
167 |
def chown(arg: String, path: Path): Unit = |
|
71123 | 168 |
bash("chown " + arg + " " + File.bash_path(path)).check |
71114 | 169 |
|
170 |
||
64189 | 171 |
/* 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
|
172 |
|
72376 | 173 |
def make_directory(path: Path): Path = |
174 |
{ |
|
73325
a89f56ab2686
more robust (amending 87403fde8cc3): notably allow symlink to existing directory, which Files.createDirectories does not accept;
wenzelm
parents:
73324
diff
changeset
|
175 |
if (!path.is_dir) { |
73945 | 176 |
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
|
177 |
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
|
178 |
} |
72376 | 179 |
path |
180 |
} |
|
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
|
181 |
|
72377 | 182 |
def new_directory(path: Path): Path = |
72426
f5d60c12deeb
more standard path output (despite platform_path from d55eb82ae77b);
wenzelm
parents:
72414
diff
changeset
|
183 |
if (path.is_dir) error("Directory already exists: " + path.absolute) |
72377 | 184 |
else make_directory(path) |
185 |
||
73322 | 186 |
def copy_dir(dir1: Path, dir2: Path): Unit = |
187 |
{ |
|
188 |
val res = bash("cp -a " + File.bash_path(dir1) + " " + File.bash_path(dir2)) |
|
189 |
if (!res.ok) { |
|
190 |
cat_error("Failed to copy directory " + dir1.absolute + " to " + dir2.absolute, res.err) |
|
191 |
} |
|
192 |
} |
|
73317 | 193 |
|
194 |
||
73567 | 195 |
object Make_Directory extends Scala.Fun_Strings("make_directory") |
73322 | 196 |
{ |
197 |
val here = Scala_Project.here |
|
73567 | 198 |
def apply(args: List[String]): List[String] = apply_paths1(args, make_directory) |
73322 | 199 |
} |
73317 | 200 |
|
73567 | 201 |
object Copy_Dir extends Scala.Fun_Strings("copy_dir") |
73322 | 202 |
{ |
203 |
val here = Scala_Project.here |
|
73567 | 204 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_dir) |
73322 | 205 |
} |
206 |
||
207 |
||
208 |
/* copy files */ |
|
64189 | 209 |
|
73317 | 210 |
def copy_file(src: JFile, dst: JFile): Unit = |
211 |
{ |
|
212 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
|
73319 | 213 |
if (!File.eq(src, target)) { |
73322 | 214 |
try { |
215 |
Files.copy(src.toPath, target.toPath, |
|
216 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
217 |
StandardCopyOption.REPLACE_EXISTING) |
|
218 |
} |
|
219 |
catch { |
|
220 |
case ERROR(msg) => |
|
73651 | 221 |
cat_error("Failed to copy file " + |
73322 | 222 |
File.path(src).absolute + " to " + File.path(dst).absolute, msg) |
223 |
} |
|
73317 | 224 |
} |
225 |
} |
|
226 |
||
73320 | 227 |
def copy_file(src: Path, dst: Path): Unit = copy_file(src.file, dst.file) |
73317 | 228 |
|
73320 | 229 |
def copy_file_base(base_dir: Path, src: Path, target_dir: Path): Unit = |
73317 | 230 |
{ |
73320 | 231 |
val src1 = src.expand |
232 |
val src1_dir = src1.dir |
|
73321 | 233 |
if (!src1.starts_basic) error("Illegal path specification " + src1 + " beyond base directory") |
73320 | 234 |
copy_file(base_dir + src1, Isabelle_System.make_directory(target_dir + src1_dir)) |
73317 | 235 |
} |
236 |
||
237 |
||
73567 | 238 |
object Copy_File extends Scala.Fun_Strings("copy_file") |
73322 | 239 |
{ |
240 |
val here = Scala_Project.here |
|
73567 | 241 |
def apply(args: List[String]): List[String] = apply_paths2(args, copy_file) |
73322 | 242 |
} |
243 |
||
73567 | 244 |
object Copy_File_Base extends Scala.Fun_Strings("copy_file_base") |
73322 | 245 |
{ |
246 |
val here = Scala_Project.here |
|
73567 | 247 |
def apply(args: List[String]): List[String] = apply_paths3(args, copy_file_base) |
73322 | 248 |
} |
249 |
||
250 |
||
251 |
/* move files */ |
|
73317 | 252 |
|
73340 | 253 |
def move_file(src: JFile, dst: JFile): Unit = |
73317 | 254 |
{ |
255 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
|
73319 | 256 |
if (!File.eq(src, target)) |
73317 | 257 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
258 |
} |
|
259 |
||
73320 | 260 |
def move_file(src: Path, dst: Path): Unit = move_file(src.file, dst.file) |
73317 | 261 |
|
262 |
||
263 |
/* symbolic link */ |
|
264 |
||
74070 | 265 |
def symlink(src: Path, dst: Path, force: Boolean = false, native: Boolean = false): Unit = |
73317 | 266 |
{ |
267 |
val src_file = src.file |
|
268 |
val dst_file = dst.file |
|
269 |
val target = if (dst_file.isDirectory) new JFile(dst_file, src_file.getName) else dst_file |
|
270 |
||
271 |
if (force) target.delete |
|
272 |
||
73893 | 273 |
def cygwin_link(): Unit = |
74070 | 274 |
{ |
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 |
|
71569
391ea80ff27c
more robust re-use of $ISABELLE_TMP_PREFIX (amending c1597167563e);
wenzelm
parents:
71520
diff
changeset
|
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 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
295 |
val path = Path.explode("$ISABELLE_TMP_PREFIX") |
72375 | 296 |
path.file.mkdirs // low-level mkdirs to avoid recursion via Isabelle environment |
60988 | 297 |
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
|
298 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
299 |
|
67924 | 300 |
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
|
301 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
302 |
val suffix = if (ext == "") "" else "." + ext |
67924 | 303 |
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
|
304 |
file.deleteOnExit |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
305 |
file |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
306 |
} |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
307 |
|
64220 | 308 |
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
|
309 |
{ |
1acf2d76ac23
more standard Isabelle_System.tmp_file and tmp_dir operations, in accordance to ML version;
wenzelm
parents:
55876
diff
changeset
|
310 |
val file = tmp_file(name, ext) |
64220 | 311 |
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
|
312 |
} |
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 |
|
56477 | 315 |
/* tmp dirs */ |
316 |
||
73324 | 317 |
def rm_tree(root: JFile): Unit = |
56477 | 318 |
{ |
319 |
root.delete |
|
320 |
if (root.isDirectory) { |
|
321 |
Files.walkFileTree(root.toPath, |
|
322 |
new SimpleFileVisitor[JPath] { |
|
323 |
override def visitFile(file: JPath, attrs: BasicFileAttributes): FileVisitResult = |
|
324 |
{ |
|
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
|
325 |
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
|
326 |
catch { case _: IOException => } |
56477 | 327 |
FileVisitResult.CONTINUE |
328 |
} |
|
329 |
||
330 |
override def postVisitDirectory(dir: JPath, e: IOException): FileVisitResult = |
|
331 |
{ |
|
332 |
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
|
333 |
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
|
334 |
catch { case _: IOException => } |
56477 | 335 |
FileVisitResult.CONTINUE |
336 |
} |
|
337 |
else throw e |
|
338 |
} |
|
339 |
} |
|
340 |
) |
|
341 |
} |
|
342 |
} |
|
343 |
||
73324 | 344 |
def rm_tree(root: Path): Unit = rm_tree(root.file) |
345 |
||
73567 | 346 |
object Rm_Tree extends Scala.Fun_Strings("rm_tree") |
73324 | 347 |
{ |
348 |
val here = Scala_Project.here |
|
73567 | 349 |
def apply(args: List[String]): List[String] = apply_paths1(args, rm_tree) |
73324 | 350 |
} |
351 |
||
67924 | 352 |
def tmp_dir(name: String, base_dir: JFile = isabelle_tmp_prefix()): JFile = |
56477 | 353 |
{ |
67924 | 354 |
val dir = Files.createTempDirectory(base_dir.toPath, name).toFile |
56477 | 355 |
dir.deleteOnExit |
356 |
dir |
|
357 |
} |
|
358 |
||
64220 | 359 |
def with_tmp_dir[A](name: String)(body: Path => A): A = |
56477 | 360 |
{ |
361 |
val dir = tmp_dir(name) |
|
64220 | 362 |
try { body(File.path(dir)) } finally { rm_tree(dir) } |
56477 | 363 |
} |
364 |
||
365 |
||
65793 | 366 |
/* quasi-atomic update of directory */ |
367 |
||
73340 | 368 |
def update_directory(dir: Path, f: Path => Unit): Unit = |
65793 | 369 |
{ |
370 |
val new_dir = dir.ext("new") |
|
371 |
val old_dir = dir.ext("old") |
|
372 |
||
373 |
rm_tree(new_dir) |
|
374 |
rm_tree(old_dir) |
|
375 |
||
376 |
f(new_dir) |
|
377 |
||
73317 | 378 |
if (dir.is_dir) move_file(dir, old_dir) |
379 |
move_file(new_dir, dir) |
|
65793 | 380 |
rm_tree(old_dir) |
381 |
} |
|
382 |
||
383 |
||
62613 | 384 |
|
385 |
/** external processes **/ |
|
386 |
||
387 |
/* GNU bash */ |
|
31796 | 388 |
|
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
389 |
def bash(script: String, |
74212 | 390 |
description: String = "", |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
391 |
cwd: JFile = null, |
73897 | 392 |
env: JMap[String, String] = settings(), |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62602
diff
changeset
|
393 |
redirect: Boolean = false, |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
394 |
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
|
395 |
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
|
396 |
progress_stderr: String => Unit = (_: String) => (), |
72598 | 397 |
watchdog: Option[Bash.Watchdog] = None, |
62602 | 398 |
strict: Boolean = true, |
399 |
cleanup: () => Unit = () => ()): Process_Result = |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
400 |
{ |
74212 | 401 |
Bash.process(script, |
402 |
description = description, cwd = cwd, env = env, redirect = redirect, cleanup = cleanup). |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
74070
diff
changeset
|
403 |
result(input = input, progress_stdout = progress_stdout, progress_stderr = progress_stderr, |
72598 | 404 |
watchdog = watchdog, strict = strict) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
405 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
406 |
|
64935
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
407 |
private lazy val gnutar_check: Boolean = |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
408 |
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
|
409 |
catch { case ERROR(_) => false } |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
410 |
|
69425 | 411 |
def gnutar( |
412 |
args: String, |
|
413 |
dir: Path = Path.current, |
|
414 |
original_owner: Boolean = false, |
|
73624 | 415 |
strip: Int = 0, |
69425 | 416 |
redirect: Boolean = false): Process_Result = |
64935
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
417 |
{ |
69425 | 418 |
val options = |
419 |
(if (dir.is_current) "" else "-C " + File.bash_path(dir) + " ") + |
|
73624 | 420 |
(if (original_owner) "" else "--owner=root --group=staff ") + |
421 |
(if (strip <= 0) "" else "--strip-components=" + strip + " ") |
|
69425 | 422 |
|
423 |
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
|
424 |
else error("Expected to find GNU tar executable") |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
425 |
} |
9437a117408b
insist in proper GNU tar, to avoid subtle semantic differences;
wenzelm
parents:
64657
diff
changeset
|
426 |
|
73650 | 427 |
def require_command(cmd: String, test: String = "--version"): Unit = |
72455 | 428 |
{ |
73650 | 429 |
if (!bash(Bash.string(cmd) + " " + test).ok) error("Missing system command: " + quote(cmd)) |
72455 | 430 |
} |
431 |
||
64152 | 432 |
def hostname(): String = bash("hostname -s").check.out |
64139 | 433 |
|
54690 | 434 |
def open(arg: String): Unit = |
64304 | 435 |
bash("exec \"$ISABELLE_OPEN\" " + Bash.string(arg) + " >/dev/null 2>/dev/null &") |
54690 | 436 |
|
437 |
def pdf_viewer(arg: Path): Unit = |
|
62616 | 438 |
bash("exec \"$PDF_VIEWER\" " + File.bash_path(arg) + " >/dev/null 2>/dev/null &") |
54690 | 439 |
|
73224
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
440 |
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
|
441 |
{ |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
442 |
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
|
443 |
val external = |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
444 |
ext.nonEmpty && |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
445 |
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
|
446 |
if (external) { |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
447 |
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
|
448 |
else open(name) |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
449 |
} |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
450 |
external |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
451 |
} |
49686e3b1909
clarified links to external files, e.g. .pdf within .thy source document;
wenzelm
parents:
72976
diff
changeset
|
452 |
|
65916 | 453 |
def export_isabelle_identifier(isabelle_identifier: String): String = |
454 |
if (isabelle_identifier == "") "" |
|
455 |
else "export ISABELLE_IDENTIFIER=" + Bash.string(isabelle_identifier) + "\n" |
|
456 |
||
32450 | 457 |
|
31796 | 458 |
/** Isabelle resources **/ |
459 |
||
64161 | 460 |
/* repository clone with Admin */ |
461 |
||
462 |
def admin(): Boolean = Path.explode("~~/Admin").is_dir |
|
463 |
||
464 |
||
62633 | 465 |
/* default logic */ |
48503 | 466 |
|
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
467 |
def default_logic(args: String*): String = |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
468 |
{ |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
469 |
args.find(_ != "") match { |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
470 |
case Some(logic) => logic |
73317 | 471 |
case None => getenv_strict("ISABELLE_LOGIC") |
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
472 |
} |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
473 |
} |
73323 | 474 |
|
475 |
||
476 |
/* download file */ |
|
477 |
||
73566 | 478 |
def download(url_name: String, progress: Progress = new Progress): HTTP.Content = |
73323 | 479 |
{ |
73335 | 480 |
val url = Url(url_name) |
481 |
progress.echo("Getting " + quote(url_name)) |
|
73566 | 482 |
try { HTTP.Client.get(url) } |
483 |
catch { case ERROR(msg) => cat_error("Failed to download " + quote(url_name), msg) } |
|
73323 | 484 |
} |
485 |
||
73566 | 486 |
def download_file(url_name: String, file: Path, progress: Progress = new Progress): Unit = |
487 |
Bytes.write(file, download(url_name, progress = progress).bytes) |
|
488 |
||
489 |
object Download extends Scala.Fun("download", thread = true) |
|
73323 | 490 |
{ |
491 |
val here = Scala_Project.here |
|
73566 | 492 |
override def invoke(args: List[Bytes]): List[Bytes] = |
493 |
args match { case List(url) => List(download(url.text).bytes) } |
|
73323 | 494 |
} |
73608 | 495 |
|
496 |
||
497 |
/* repositories */ |
|
498 |
||
73611 | 499 |
val isabelle_repository: Mercurial.Server = |
500 |
Mercurial.Server("https://isabelle.sketis.net/repos/isabelle") |
|
73608 | 501 |
|
73611 | 502 |
val afp_repository: Mercurial.Server = |
503 |
Mercurial.Server("https://isabelle.sketis.net/repos/afp-devel") |
|
73610 | 504 |
|
505 |
def official_releases(): List[String] = |
|
506 |
Library.trim_split_lines( |
|
507 |
isabelle_repository.read_file(Path.explode("Admin/Release/official"))) |
|
27919 | 508 |
} |