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