src/Pure/System/linux.scala
author wenzelm
Tue, 29 Oct 2019 18:08:24 +0100
changeset 70965 fe9496df6298
child 70966 acc3bac0d7c5
permissions -rw-r--r--
specific support for Linux, notably Ubuntu/Debian;
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
  }
fe9496df6298 specific support for Linux, notably Ubuntu/Debian;
wenzelm
parents:
diff changeset
    49
}