src/Pure/System/linux.scala
author wenzelm
Tue, 29 Oct 2019 19:42:40 +0100
changeset 70966 acc3bac0d7c5
parent 70965 fe9496df6298
child 71046 b8aeeedf7e68
permissions -rw-r--r--
support for Linux packages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
70965
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/linux.scala
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     3
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     4
Specific support for Linux, notably Ubuntu/Debian.
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     5
*/
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     6
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     7
package isabelle
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     8
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
     9
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    10
import scala.util.matching.Regex
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    11
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    12
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    13
object Linux
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    14
{
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    15
  /* check system */
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    16
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    17
  def check_system(): Unit =
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    18
    if (!Platform.is_linux) error("Not a Linux system")
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    19
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    20
  def check_system_root(): Unit =
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    21
  {
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    22
    check_system()
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    23
    if (Isabelle_System.bash("id -u").check.out != "0") error("Not running as superuser (root)")
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    24
  }
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    25
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    26
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    27
  /* release */
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    28
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    29
  object Release
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    30
  {
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    31
    private val ID = """^Distributor ID:\s*(\S.*)$""".r
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    32
    private val RELEASE = """^Release:\s*(\S.*)$""".r
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    33
    private val DESCRIPTION = """^Description:\s*(\S.*)$""".r
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    34
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    35
    def apply(): Release =
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    36
    {
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    37
      val lines = Isabelle_System.bash("lsb_release -a").check.out_lines
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    38
      def find(R: Regex): String = lines.collectFirst({ case R(a) => a }).getOrElse("Unknown")
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    39
      new Release(find(ID), find(RELEASE), find(DESCRIPTION))
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    40
    }
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    41
  }
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    42
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    43
  final class Release private(val id: String, val release: String, val description: String)
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    44
  {
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    45
    override def toString: String = description
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    46
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    47
    def is_ubuntu: Boolean = id == "Ubuntu"
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    48
  }
70966
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    49
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    50
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    51
  /* packages */
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    52
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    53
  def reboot_required(): Boolean =
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    54
    Path.explode("/var/run/reboot-required").is_file
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    55
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    56
  def check_reboot_required(): Unit =
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    57
    if (reboot_required()) error("Reboot required")
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    58
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    59
  def package_update(progress: Progress = No_Progress): Unit =
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    60
    progress.bash(
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    61
      """apt-get update -y && apt-get upgrade -y && apt autoremove -y""",
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    62
      echo = true).check
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    63
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    64
  def package_install(packages: List[String], progress: Progress = No_Progress): Unit =
acc3bac0d7c5 support for Linux packages;
wenzelm
parents: 70965
diff changeset
    65
    progress.bash("apt-get install -y -- " + Bash.strings(packages), echo = true).check
70965
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    66
}