upstream: https://github.com/avsm/osrelease
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Squashed 'osrelease/' content from commit 7302650c git-subtree-split: 7302650c60682a8e5295cd237fd50b96b1d9945e

+484
+4
.gitignore
··· 1 + _build 2 + .merlin 3 + *.install 4 + .*.swp
+1
.ocamlformat
··· 1 + version = 0.28.1
+8
README.md
··· 1 + ## osrelease - detect system and distro information in OCaml 2 + 3 + **Status: WIP** 4 + 5 + This library is used to detect local information about 6 + which operating system, architecture, distribution and 7 + version the program is running under. 8 +
+21
dune-project
··· 1 + (lang dune 2.3) 2 + (name osrelease) 3 + (generate_opam_files true) 4 + 5 + (source (github avsm/osrelease)) 6 + (license ISC) 7 + (authors "Anil Madhavapeddy <anil@recoil.org>") 8 + (maintainers "anil@recoil.org") 9 + 10 + (package 11 + (name osrelease) 12 + (synopsis "Detect operating system, distro and version information") 13 + (description " 14 + This library is used to detect local information about 15 + which operating system, architecture, distribution and 16 + version the program is running under.") 17 + (depends 18 + (ocaml (>= 4.06.0)) 19 + bos 20 + astring 21 + ))
+4
lib/dune
··· 1 + (library 2 + (name osrelease) 3 + (public_name osrelease) 4 + (libraries bos astring))
+320
lib/osrelease.ml
··· 1 + (* 2 + * Copyright (c) 2020-2023 Anil Madhavapeddy <anil@recoil.org> 3 + * 4 + * Permission to use, copy, modify, and distribute this software for any 5 + * purpose with or without fee is hereby granted, provided that the above 6 + * copyright notice and this permission notice appear in all copies. 7 + * 8 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 + * 16 + *) 17 + 18 + open Astring 19 + open Bos 20 + 21 + let ( >>= ) v f = match v with Ok v -> f v | Error _ as e -> e 22 + let ( >>| ) v f = match v with Ok v -> Ok (f v) | Error _ as e -> e 23 + 24 + let uname f = 25 + let cmd = Cmd.(v "uname" % f) in 26 + OS.Cmd.( 27 + run_out cmd |> to_string |> function 28 + | Ok v -> v 29 + | Error _ -> failwith "error") 30 + 31 + module Arch = struct 32 + type t = 33 + [ `X86_32 34 + | `X86_64 35 + | `Ppc32 36 + | `Ppc64 of [ `Le | `Be ] 37 + | `Arm32 of [ `Armv5 | `Armv6 | `Earmv6 | `Armv7 | `Earmv7 ] 38 + | `Aarch64 39 + | `Unknown of string ] 40 + 41 + let to_string (x : t) = 42 + match x with 43 + | `X86_32 -> "x86_32" 44 + | `X86_64 -> "x86_64" 45 + | `Ppc32 -> "ppc32" 46 + | `Ppc64 `Be -> "ppc64" 47 + | `Ppc64 `Le -> "ppc64le" 48 + | `Arm32 _ -> "arm32" 49 + | `Aarch64 -> "arm64" 50 + | `Unknown v -> v 51 + 52 + let pp fmt v = Format.pp_print_string fmt (to_string v) 53 + 54 + let of_string v : t = 55 + match String.Ascii.lowercase v with 56 + | "x86" | "i386" | "i586" | "i686" -> `X86_32 57 + | "powerpc" | "ppc" | "ppcle" -> `Ppc32 58 + | "amd64" | "x86_64" -> `X86_64 59 + | "ppc64" | "ppc64be" -> `Ppc64 `Be 60 + | "ppc64le" -> `Ppc64 `Le 61 + | "aarch64_be" | "aarch64" | "armv8b" | "armv8l" -> `Aarch64 62 + | "armv5" -> `Arm32 `Armv5 63 + | "armv6" -> `Arm32 `Armv6 64 + | "earmv6" -> `Arm32 `Earmv6 65 + | "armv7" -> `Arm32 `Earmv7 66 + | v -> `Unknown v 67 + 68 + let v () = 69 + match Sys.os_type with 70 + | "Unix" | "Cygwin" -> uname "-m" |> of_string 71 + | "Win32" when Sys.word_size = 32 (* TODO WoW64? *) -> `X86_32 72 + | "Win32" -> `X86_64 73 + | v -> `Unknown v 74 + end 75 + 76 + module OS = struct 77 + type t = 78 + [ `Linux 79 + | `MacOS 80 + | `Win32 81 + | `Cygwin 82 + | `FreeBSD 83 + | `OpenBSD 84 + | `DragonFly 85 + | `Unknown of string ] 86 + 87 + let to_string (v : t) = 88 + match v with 89 + | `Linux -> "linux" 90 + | `MacOS -> "macos" 91 + | `Win32 -> "win32" 92 + | `Cygwin -> "cygwin" 93 + | `FreeBSD -> "freebsd" 94 + | `OpenBSD -> "openbsd" 95 + | `DragonFly -> "dragonfly" 96 + | `Unknown v -> v 97 + 98 + let of_string v : t = 99 + match String.Ascii.lowercase v with 100 + | "darwin" | "osx" -> `MacOS 101 + | "linux" -> `Linux 102 + | "win32" -> `Win32 103 + | "cygwin" -> `Cygwin 104 + | "freebsd" -> `FreeBSD 105 + | "openbsd" -> `OpenBSD 106 + | "dragonfly" -> `DragonFly 107 + | v -> `Unknown v 108 + 109 + let pp fmt v = Format.pp_print_string fmt (to_string v) 110 + 111 + let v () = 112 + match Sys.os_type with 113 + | "Unix" -> uname "-s" |> of_string 114 + | v -> of_string v 115 + end 116 + 117 + module Distro = struct 118 + type linux = 119 + [ `Arch 120 + | `Alpine 121 + | `CentOS 122 + | `Debian 123 + | `Fedora 124 + | `Gentoo 125 + | `Mageia 126 + | `NixOS 127 + | `OracleLinux 128 + | `RHEL 129 + | `Ubuntu 130 + | `OpenSUSE 131 + | `Android 132 + | `Other of string ] 133 + 134 + type macos = [ `Homebrew | `MacPorts | `None ] 135 + type windows = [ `Cygwin | `None ] 136 + 137 + type t = 138 + [ `Linux of linux 139 + | `MacOS of macos 140 + | `Windows of windows 141 + | `Other of string ] 142 + 143 + let linux_to_string (x : linux) = 144 + match x with 145 + | `Alpine -> "alpine" 146 + | `Android -> "android" 147 + | `Arch -> "arch" 148 + | `CentOS -> "centos" 149 + | `Debian -> "debian" 150 + | `Fedora -> "fedora" 151 + | `Gentoo -> "gentoo" 152 + | `Mageia -> "mageia" 153 + | `NixOS -> "nixos" 154 + | `OpenSUSE -> "opensuse" 155 + | `OracleLinux -> "oraclelinux" 156 + | `RHEL -> "rhel" 157 + | `Ubuntu -> "ubuntu" 158 + | `Other v -> v 159 + 160 + let linux_of_string = function 161 + | "alpine" -> `Alpine 162 + | "android" -> `Android 163 + | "arch" -> `Arch 164 + | "centos" -> `CentOS 165 + | "debian" -> `Debian 166 + | "fedora" -> `Fedora 167 + | "gentoo" -> `Gentoo 168 + | "mageia" -> `Mageia 169 + | "nixos" -> `NixOS 170 + | "opensuse" -> `OpenSUSE 171 + | "oraclelinux" -> `OracleLinux 172 + | "rhel" -> `RHEL 173 + | "ubuntu" -> `Ubuntu 174 + | v -> `Other v 175 + 176 + let macos_to_string (x : macos) = 177 + match x with 178 + | `Homebrew -> "homebrew" 179 + | `MacPorts -> "macports" 180 + | `None -> "macos" 181 + 182 + let macos_of_string = function 183 + | "homebrew" -> `Homebrew 184 + | "macports" -> `MacPorts 185 + | _ -> `None 186 + 187 + let windows_to_string (x : windows) = 188 + match x with `Cygwin -> "cygwin" | `None -> "windows" 189 + 190 + let windows_of_string = function "cygwin" -> `Cygwin | _ -> `None 191 + 192 + let to_string (x : t) = 193 + match x with 194 + | `Linux v -> "linux " ^ linux_to_string v 195 + | `MacOS v -> "macos " ^ macos_to_string v 196 + | `Windows v -> "windows " ^ windows_to_string v 197 + | `Other v -> "unknown " ^ v 198 + 199 + let of_string x = 200 + try 201 + Scanf.sscanf x "%s %s" (fun a b -> 202 + match a with 203 + | "linux" -> `Linux (linux_of_string b) 204 + | "macos" -> `MacOS (macos_of_string b) 205 + | "windows" -> `Windows (windows_of_string b) 206 + | _ -> `Other b) 207 + with _ -> `Other "" 208 + 209 + let pp fmt v = Format.pp_print_string fmt (to_string v) 210 + 211 + let android_release = 212 + lazy 213 + (let open Bos in 214 + let cmd = Cmd.(v "getprop" % "ro.build.version.release") in 215 + match OS.Cmd.(run_out cmd |> to_string) with 216 + | Ok "" -> None 217 + | Ok out -> Some out 218 + | Error _ -> None) 219 + 220 + let find_first_file files = List.find_opt Sys.file_exists files 221 + 222 + let os_release_fields = 223 + lazy 224 + (let os_release_file = 225 + find_first_file [ "/etc/os-release"; "/usr/lib/os-release" ] 226 + in 227 + match os_release_file with 228 + | None -> Error (`Msg "no os-release file found") 229 + | Some file -> 230 + Bos.OS.File.fold_lines 231 + (fun acc line -> 232 + let open Scanf in 233 + try 234 + sscanf line "%s@= %s" (fun k v -> 235 + try sscanf v "\"%s@\"" (fun s -> (k, s) :: acc) 236 + with Scan_failure _ | End_of_file -> acc) 237 + with Scan_failure _ | End_of_file -> acc) 238 + [] (Fpath.v file)) 239 + 240 + let os_release_field f = 241 + Lazy.force os_release_fields >>| List.assoc_opt f >>| function 242 + | Some "" -> None 243 + | v -> v 244 + 245 + let identify_linux () = 246 + os_release_field "ID" >>= function 247 + | Some v -> Ok (Some v) 248 + | None -> ( 249 + let cmd = Cmd.(v "lsb_release" % "-i" % "-s") in 250 + Bos.OS.Cmd.(run_out cmd |> to_string) |> function 251 + | Ok v -> Ok (Some (String.Ascii.lowercase v)) 252 + | Error _ -> ( 253 + let issue = 254 + find_first_file 255 + [ 256 + "/etc/redhat-release"; 257 + "/etc/centos-release"; 258 + "/etc/gentoo-release"; 259 + "/etc/issue"; 260 + ] 261 + in 262 + match issue with 263 + | None -> Ok None 264 + | Some f -> ( 265 + Bos.OS.File.read (Fpath.v f) >>= fun v -> 266 + match Scanf.sscanf v " %s " (fun x -> x) with 267 + | "" -> Ok None 268 + | v -> Ok (Some (String.Ascii.lowercase v)) 269 + | exception Not_found -> Ok None))) 270 + 271 + let v () : (t, [ `Msg of string ]) result = 272 + match OS.v () with 273 + | `MacOS -> ( 274 + Bos.OS.Cmd.exists (Cmd.v "brew") >>= function 275 + | true -> Ok (`MacOS `Homebrew) 276 + | false -> ( 277 + Bos.OS.Cmd.exists (Cmd.v "port") >>= function 278 + | true -> Ok (`MacOS `MacPorts) 279 + | false -> Ok (`MacOS `None))) 280 + | `Linux -> ( 281 + match Lazy.force android_release with 282 + | Some _ -> Ok (`Linux `Android) 283 + | None -> ( 284 + identify_linux () >>= function 285 + | None -> Ok (`Linux (`Other "")) 286 + | Some v -> Ok (`Linux (`Other v)))) 287 + | _ -> Error (`Msg "foo") 288 + end 289 + 290 + module Version = struct 291 + let detect_linux_version () = 292 + match Lazy.force Distro.android_release with 293 + | Some r -> Ok (Some r) 294 + | None -> ( 295 + let cmd = Cmd.(v "lsb_release" % "-r" % "-s") in 296 + Bos.OS.Cmd.(run_out cmd |> to_string) |> function 297 + | Ok v -> Ok (Some v) 298 + | Error _ -> Distro.os_release_field "VERSION_ID") 299 + 300 + let detect_macos_version () = 301 + let cmd = Cmd.(v "sw_vers" % "-productVersion") in 302 + Bos.OS.Cmd.(run_out cmd |> to_string) >>| function 303 + | "" -> None 304 + | v -> Some v 305 + 306 + let detect_freebsd_version () = 307 + let cmd = Cmd.(v "uname" % "-U") in 308 + Bos.OS.Cmd.(run_out cmd |> to_string) >>| function 309 + | "" -> None 310 + | v -> Some v 311 + 312 + let v () = 313 + match OS.v () with 314 + | `Linux -> detect_linux_version () 315 + | `MacOS -> detect_macos_version () 316 + | `FreeBSD -> detect_freebsd_version () 317 + | `Win32 | `OpenBSD | `DragonFly | `Cygwin -> 318 + Error (`Msg "Version detection on this platform not yet supported") 319 + | `Unknown _ -> Ok None 320 + end
+92
lib/osrelease.mli
··· 1 + (* 2 + * Copyright (c) 2020 Anil Madhavapeddy <anil@recoil.org> 3 + * 4 + * Permission to use, copy, modify, and distribute this software for any 5 + * purpose with or without fee is hereby granted, provided that the above 6 + * copyright notice and this permission notice appear in all copies. 7 + * 8 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 + * 16 + *) 17 + 18 + module Arch : sig 19 + type t = 20 + [ `Aarch64 21 + | `Arm32 of [ `Armv5 | `Armv6 | `Armv7 | `Earmv6 | `Earmv7 ] 22 + | `Ppc32 23 + | `Ppc64 of [ `Be | `Le ] 24 + | `Unknown of string 25 + | `X86_32 26 + | `X86_64 ] 27 + 28 + val to_string : t -> string 29 + val of_string : string -> t 30 + val pp : Format.formatter -> t -> unit 31 + val v : unit -> t 32 + end 33 + 34 + module OS : sig 35 + type t = 36 + [ `Cygwin 37 + | `DragonFly 38 + | `FreeBSD 39 + | `Linux 40 + | `MacOS 41 + | `OpenBSD 42 + | `Unknown of string 43 + | `Win32 ] 44 + 45 + val to_string : t -> string 46 + val of_string : string -> t 47 + val pp : Format.formatter -> t -> unit 48 + val v : unit -> t 49 + end 50 + 51 + module Distro : sig 52 + type linux = 53 + [ `Alpine 54 + | `Android 55 + | `Arch 56 + | `CentOS 57 + | `Debian 58 + | `Fedora 59 + | `Gentoo 60 + | `Mageia 61 + | `NixOS 62 + | `OpenSUSE 63 + | `OracleLinux 64 + | `Other of string 65 + | `RHEL 66 + | `Ubuntu ] 67 + 68 + type macos = [ `Homebrew | `MacPorts | `None ] 69 + type windows = [ `Cygwin | `None ] 70 + 71 + type t = 72 + [ `Linux of linux 73 + | `MacOS of macos 74 + | `Other of string 75 + | `Windows of windows ] 76 + 77 + val linux_to_string : linux -> string 78 + val linux_of_string : string -> linux 79 + val macos_to_string : macos -> string 80 + val macos_of_string : string -> macos 81 + val windows_to_string : windows -> string 82 + val windows_of_string : string -> windows 83 + val to_string : t -> string 84 + val of_string : string -> t 85 + val pp : Format.formatter -> t -> unit 86 + val os_release_field : string -> (string option, [> `Msg of string ]) result 87 + val v : unit -> (t, [ `Msg of string ]) result 88 + end 89 + 90 + module Version : sig 91 + val v : unit -> (string option, [> `Msg of string ]) result 92 + end
+34
osrelease.opam
··· 1 + # This file is generated by dune, edit dune-project instead 2 + opam-version: "2.0" 3 + synopsis: "Detect operating system, distro and version information" 4 + description: """ 5 + 6 + This library is used to detect local information about 7 + which operating system, architecture, distribution and 8 + version the program is running under.""" 9 + maintainer: ["anil@recoil.org"] 10 + authors: ["Anil Madhavapeddy <anil@recoil.org>"] 11 + license: "ISC" 12 + homepage: "https://github.com/avsm/osrelease" 13 + bug-reports: "https://github.com/avsm/osrelease/issues" 14 + depends: [ 15 + "dune" {>= "2.3"} 16 + "ocaml" {>= "4.06.0"} 17 + "bos" 18 + "astring" 19 + ] 20 + build: [ 21 + ["dune" "subst"] {pinned} 22 + [ 23 + "dune" 24 + "build" 25 + "-p" 26 + name 27 + "-j" 28 + jobs 29 + "@install" 30 + "@runtest" {with-test} 31 + "@doc" {with-doc} 32 + ] 33 + ] 34 + dev-repo: "git+https://github.com/avsm/osrelease.git"