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