src/Pure/Admin/other_isabelle.scala
author wenzelm
Tue, 24 Jan 2023 14:46:51 +0100
changeset 77069 348f4d95d110
parent 77055 f56800b8b085
child 77071 7a89ef6b0276
permissions -rw-r--r--
more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Admin/other_isabelle.scala
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     3
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     4
Manage other Isabelle distributions.
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     6
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     8
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    10
object Other_Isabelle {
77050
wenzelm
parents: 77044
diff changeset
    11
  def apply(
wenzelm
parents: 77044
diff changeset
    12
    isabelle_home: Path,
wenzelm
parents: 77044
diff changeset
    13
    isabelle_identifier: String = "",
wenzelm
parents: 77044
diff changeset
    14
    user_home: Path = Path.USER_HOME,
wenzelm
parents: 77044
diff changeset
    15
    progress: Progress = new Progress
wenzelm
parents: 77044
diff changeset
    16
  ): Other_Isabelle = {
69408
fb26935838c7 more robust, notably for macos /var vs. /private/var;
wenzelm
parents: 69404
diff changeset
    17
    new Other_Isabelle(isabelle_home.canonical, isabelle_identifier, user_home, progress)
77050
wenzelm
parents: 77044
diff changeset
    18
  }
67045
6c94f749410a tuned signature;
wenzelm
parents: 65930
diff changeset
    19
}
6c94f749410a tuned signature;
wenzelm
parents: 65930
diff changeset
    20
77041
4adee07a5e48 tuned signature;
wenzelm
parents: 77040
diff changeset
    21
final class Other_Isabelle private(
67045
6c94f749410a tuned signature;
wenzelm
parents: 65930
diff changeset
    22
  val isabelle_home: Path,
6c94f749410a tuned signature;
wenzelm
parents: 65930
diff changeset
    23
  val isabelle_identifier: String,
67046
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    24
  user_home: Path,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    25
  progress: Progress
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    26
) {
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    27
  other_isabelle =>
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    28
69404
de88761edbe2 clarified absolute isabelle_home and (implicitly) isabelle_home_user;
wenzelm
parents: 69401
diff changeset
    29
  override def toString: String = isabelle_home.toString
67046
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    30
77042
wenzelm
parents: 77041
diff changeset
    31
  if (proper_string(System.getenv("ISABELLE_SETTINGS_PRESENT")).isDefined) {
67046
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    32
    error("Cannot initialize with enclosing ISABELLE_SETTINGS_PRESENT")
77042
wenzelm
parents: 77041
diff changeset
    33
  }
67046
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    34
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    35
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    36
  /* static system */
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    37
65930
9a28fc03c3fe tuned signature;
wenzelm
parents: 65916
diff changeset
    38
  def bash(
77042
wenzelm
parents: 77041
diff changeset
    39
    script: String,
wenzelm
parents: 77041
diff changeset
    40
    redirect: Boolean = false,
wenzelm
parents: 77041
diff changeset
    41
    echo: Boolean = false,
wenzelm
parents: 77041
diff changeset
    42
    strict: Boolean = true
wenzelm
parents: 77041
diff changeset
    43
  ): Process_Result = {
67046
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    44
    progress.bash(
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    45
      "export USER_HOME=" + File.bash_path(user_home) + "\n" +
897f1ac84aab clarified settings environment;
wenzelm
parents: 67045
diff changeset
    46
      Isabelle_System.export_isabelle_identifier(isabelle_identifier) + script,
65930
9a28fc03c3fe tuned signature;
wenzelm
parents: 65916
diff changeset
    47
      env = null, cwd = isabelle_home.file, redirect = redirect, echo = echo, strict = strict)
77042
wenzelm
parents: 77041
diff changeset
    48
  }
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    49
69166
5c553c48c0e5 tuned signature;
wenzelm
parents: 68754
diff changeset
    50
  def getenv(name: String): String =
77044
a4380a2d6d2c tuned signature: avoid aliases;
wenzelm
parents: 77042
diff changeset
    51
    other_isabelle.bash("bin/isabelle getenv -b " + Bash.string(name)).check.out
69166
5c553c48c0e5 tuned signature;
wenzelm
parents: 68754
diff changeset
    52
5c553c48c0e5 tuned signature;
wenzelm
parents: 68754
diff changeset
    53
  val isabelle_home_user: Path = Path.explode(getenv("ISABELLE_HOME_USER"))
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    54
68754
8999f9143e5f clarified signature;
wenzelm
parents: 67046
diff changeset
    55
  val etc: Path = isabelle_home_user + Path.explode("etc")
8999f9143e5f clarified signature;
wenzelm
parents: 67046
diff changeset
    56
  val etc_settings: Path = etc + Path.explode("settings")
8999f9143e5f clarified signature;
wenzelm
parents: 67046
diff changeset
    57
  val etc_preferences: Path = etc + Path.explode("preferences")
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    58
77069
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    59
  def resolve_components(echo: Boolean): Unit = {
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    60
    val missing = Path.split(getenv("ISABELLE_COMPONENTS_MISSING"))
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    61
    for (path <- missing) {
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    62
      Components.resolve(path.dir, path.file_name, progress = if (echo) progress else new Progress)
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    63
    }
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    64
  }
348f4d95d110 more robust (see also 7f55a3e28c88): resolve components from current Isabelle context, using Isabelle/Scala instead of shell scripts;
wenzelm
parents: 77055
diff changeset
    65
73625
f8f065e20837 misc tuning and clarification: more explicit types Release_Context, Release_Archive;
wenzelm
parents: 73522
diff changeset
    66
69388
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    67
  /* components */
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    68
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    69
  def init_components(
73240
3e963d68d394 more robust ISABELLE_COMPONENT_REPOSITORY: use current value of managing process to avoid its fluctuation in ancient history;
wenzelm
parents: 72375
diff changeset
    70
    component_repository: String = Components.default_component_repository,
69434
b93404a4c3dd clarified settings and defaults;
wenzelm
parents: 69408
diff changeset
    71
    components_base: Path = Components.default_components_base,
77055
f56800b8b085 clarified defaults;
wenzelm
parents: 77050
diff changeset
    72
    catalogs: List[String] = Components.default_catalogs,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    73
    components: List[String] = Nil
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    74
  ): List[String] = {
69404
de88761edbe2 clarified absolute isabelle_home and (implicitly) isabelle_home_user;
wenzelm
parents: 69401
diff changeset
    75
    val dir = Components.admin(isabelle_home)
73240
3e963d68d394 more robust ISABELLE_COMPONENT_REPOSITORY: use current value of managing process to avoid its fluctuation in ancient history;
wenzelm
parents: 72375
diff changeset
    76
3e963d68d394 more robust ISABELLE_COMPONENT_REPOSITORY: use current value of managing process to avoid its fluctuation in ancient history;
wenzelm
parents: 72375
diff changeset
    77
    ("ISABELLE_COMPONENT_REPOSITORY=" + Bash.string(component_repository)) ::
69396
56bea34e0f8e clarified signature;
wenzelm
parents: 69395
diff changeset
    78
    catalogs.map(name =>
69434
b93404a4c3dd clarified settings and defaults;
wenzelm
parents: 69408
diff changeset
    79
      "init_components " + File.bash_path(components_base) + " " +
b93404a4c3dd clarified settings and defaults;
wenzelm
parents: 69408
diff changeset
    80
        File.bash_path(dir + Path.basic(name))) :::
69396
56bea34e0f8e clarified signature;
wenzelm
parents: 69395
diff changeset
    81
    components.map(name =>
69434
b93404a4c3dd clarified settings and defaults;
wenzelm
parents: 69408
diff changeset
    82
      "init_component " + File.bash_path(components_base + Path.basic(name)))
69388
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    83
  }
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    84
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
    85
69168
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    86
  /* settings */
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    87
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    88
  def clean_settings(): Boolean =
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    89
    if (!etc_settings.is_file) true
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    90
    else if (File.read(etc_settings).startsWith("# generated by Isabelle")) {
77042
wenzelm
parents: 77041
diff changeset
    91
      etc_settings.file.delete
wenzelm
parents: 77041
diff changeset
    92
      true
69168
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    93
    }
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
    94
    else false
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    95
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
    96
  def init_settings(settings: List[String]): Unit = {
77042
wenzelm
parents: 77041
diff changeset
    97
    if (!clean_settings()) {
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
    98
      error("Cannot proceed with existing user settings file: " + etc_settings)
77042
wenzelm
parents: 77041
diff changeset
    99
    }
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
   100
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 71726
diff changeset
   101
    Isabelle_System.make_directory(etc_settings.dir)
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
   102
    File.write(etc_settings,
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
   103
      "# generated by Isabelle " + Date.now() + "\n" +
69388
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
   104
      "#-*- shell-script -*- :mode=shellscript:\n" +
fc58534bc475 clarified component settings;
wenzelm
parents: 69387
diff changeset
   105
      settings.mkString("\n", "\n", "\n"))
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
   106
  }
69168
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   107
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   108
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   109
  /* cleanup */
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   110
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73988
diff changeset
   111
  def cleanup(): Unit = {
69168
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   112
    clean_settings()
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   113
    etc.file.delete
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   114
    isabelle_home_user.file.delete
68816d1c73a7 eliminated "isabelle makedist" -- prefer Scala over bash/perl scripting;
wenzelm
parents: 69166
diff changeset
   115
  }
64188
f88bae1922c4 clarified modules;
wenzelm
parents:
diff changeset
   116
}