From c073cbf16e7887306199fd1acb8fd3ad0539fcc6 Mon Sep 17 00:00:00 2001 From: Arnaud Moura Date: Sun, 29 Dec 2019 19:09:30 +0100 Subject: [PATCH 1/5] Support detection in docker container. --- lib/pure/distros.nim | 70 +++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/lib/pure/distros.nim b/lib/pure/distros.nim index 7a616dbdb6347..6678ff033d971 100644 --- a/lib/pure/distros.nim +++ b/lib/pure/distros.nim @@ -134,18 +134,24 @@ const LacksDevPackages* = {Distribution.Gentoo, Distribution.Slackware, Distribution.ArchLinux} -# we cache the result of the 'uname -a' +# we cache the result of the 'cmdRelease' # execution for faster platform detections. -var unameRes, releaseRes, hostnamectlRes: string +var unameRes, osRes, releaseRes, hostnamectlRes: string -template unameRelease(cmd, cache): untyped = +template cmdRelease(cmd, cache): untyped = if cache.len == 0: cache = (when defined(nimscript): gorge(cmd) else: execProcess(cmd)) cache -template uname(): untyped = unameRelease("uname -a", unameRes) -template release(): untyped = unameRelease("lsb_release -d", releaseRes) -template hostnamectl(): untyped = unameRelease("hostnamectl", hostnamectlRes) +template uname(): untyped = cmdRelease("uname -a", unameRes) +template osrelease(): untyped = cmdRelease("cat /etc/os-release", osRes) +template release(): untyped = cmdRelease("lsb_release -d", releaseRes) +template hostnamectl(): untyped = cmdRelease("hostnamectl", hostnamectlRes) + +proc detectOsWithAllCmd(d: Distribution): bool = + let dd = toLowerAscii($d) + result = dd in toLowerAscii(osrelease()) or dd in toLowerAscii(release()) or + dd in toLowerAscii(uname()) or ("operating system: " & dd) in toLowerAscii(hostnamectl()) proc detectOsImpl(d: Distribution): bool = case d @@ -154,32 +160,36 @@ proc detectOsImpl(d: Distribution): bool = of Distribution.Posix: result = defined(posix) of Distribution.MacOSX: result = defined(macosx) of Distribution.Linux: result = defined(linux) - of Distribution.Ubuntu, Distribution.Gentoo, Distribution.FreeBSD, - Distribution.OpenBSD: - result = ("-" & $d & " ") in uname() - of Distribution.RedHat: - result = "Red Hat" in uname() of Distribution.BSD: result = defined(bsd) - of Distribution.ArchLinux: - result = "arch" in toLowerAscii(uname()) - of Distribution.NixOS: - result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE") - # Check if this is a Nix build or NixOS environment - of Distribution.OpenSUSE: - result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release()) - of Distribution.GoboLinux: - result = "-Gobo " in uname() - of Distribution.OpenMandriva: - result = "mandriva" in toLowerAscii(uname()) - of Distribution.Solaris: - let uname = toLowerAscii(uname()) - result = ("sun" in uname) or ("solaris" in uname) - of Distribution.Haiku: - result = defined(haiku) else: - let dd = toLowerAscii($d) - result = dd in toLowerAscii(uname()) or dd in toLowerAscii(release()) or - ("operating system: " & dd) in toLowerAscii(hostnamectl()) + when defined(linux): + case d + of Distribution.Ubuntu, Distribution.Gentoo, Distribution.FreeBSD, + Distribution.OpenBSD, Distribution.Debian, Distribution.Fedora, + Distribution.OpenMandriva, Distribution.CentOS: + result = $d in osrelease() + of Distribution.RedHat: + result = "Red Hat" in osrelease() + of Distribution.Elementary: + result = "elementary OS" in osrelease() + of Distribution.ArchLinux: + result = "Arch Linux" in osrelease() + of Distribution.NixOS: + result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE") + # Check if this is a Nix build or NixOS environment + of Distribution.OpenSUSE: + result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release()) + of Distribution.GoboLinux: + result = "-Gobo " in uname() + of Distribution.Solaris: + let uname = toLowerAscii(uname()) + result = ("sun" in uname) or ("solaris" in uname) + of Distribution.Haiku: + result = defined(haiku) + else: + result = detectOsWithAllCmd(d) + else: + result = detectOsWithAllCmd(d) template detectOs*(d: untyped): bool = ## Distro/OS detection. For convenience the From 71df1a91df7cd01cf6420c4ba94c239c401c2aec Mon Sep 17 00:00:00 2001 From: Arnaud Moura Date: Thu, 16 Jan 2020 12:18:22 +0100 Subject: [PATCH 2/5] Get only ID information in os-release. --- lib/pure/distros.nim | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/pure/distros.nim b/lib/pure/distros.nim index 6678ff033d971..8bd4256492131 100644 --- a/lib/pure/distros.nim +++ b/lib/pure/distros.nim @@ -136,7 +136,7 @@ const # we cache the result of the 'cmdRelease' # execution for faster platform detections. -var unameRes, osRes, releaseRes, hostnamectlRes: string +var unameRes, osReleaseIDRes, releaseRes, hostnamectlRes: string template cmdRelease(cmd, cache): untyped = if cache.len == 0: @@ -144,13 +144,13 @@ template cmdRelease(cmd, cache): untyped = cache template uname(): untyped = cmdRelease("uname -a", unameRes) -template osrelease(): untyped = cmdRelease("cat /etc/os-release", osRes) +template osReleaseID(): untyped = cmdRelease("cat /etc/os-release | grep ^ID=", osReleaseIDRes) template release(): untyped = cmdRelease("lsb_release -d", releaseRes) template hostnamectl(): untyped = cmdRelease("hostnamectl", hostnamectlRes) proc detectOsWithAllCmd(d: Distribution): bool = let dd = toLowerAscii($d) - result = dd in toLowerAscii(osrelease()) or dd in toLowerAscii(release()) or + result = dd in toLowerAscii(osReleaseID()) or dd in toLowerAscii(release()) or dd in toLowerAscii(uname()) or ("operating system: " & dd) in toLowerAscii(hostnamectl()) proc detectOsImpl(d: Distribution): bool = @@ -164,16 +164,17 @@ proc detectOsImpl(d: Distribution): bool = else: when defined(linux): case d - of Distribution.Ubuntu, Distribution.Gentoo, Distribution.FreeBSD, - Distribution.OpenBSD, Distribution.Debian, Distribution.Fedora, - Distribution.OpenMandriva, Distribution.CentOS: - result = $d in osrelease() + of Distribution.Gentoo, Distribution.FreeBSD, + Distribution.OpenBSD: + result = ("-" & $d & " ") in uname() + of Distribution.Elementary, Distribution.Ubuntu, Distribution.Debian, Distribution.Fedora, + Distribution.OpenMandriva, Distribution.CentOS, Distribution.Alpine, + Distribution.Mageia, Distribution.Zorin: + result = toLowerAscii($d) in osReleaseID() of Distribution.RedHat: - result = "Red Hat" in osrelease() - of Distribution.Elementary: - result = "elementary OS" in osrelease() + result = "rhel" in osReleaseID() of Distribution.ArchLinux: - result = "Arch Linux" in osrelease() + result = "arch" in osReleaseID() of Distribution.NixOS: result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE") # Check if this is a Nix build or NixOS environment From be5e168d46795e83a7a3133822e7020ee61819c1 Mon Sep 17 00:00:00 2001 From: Arnaud Moura Date: Thu, 16 Jan 2020 13:30:38 +0100 Subject: [PATCH 3/5] Add test to distros module. --- tests/distros/tdistros_detect.nim | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/distros/tdistros_detect.nim diff --git a/tests/distros/tdistros_detect.nim b/tests/distros/tdistros_detect.nim new file mode 100644 index 0000000000000..793a9edd31ee4 --- /dev/null +++ b/tests/distros/tdistros_detect.nim @@ -0,0 +1,21 @@ +import distros + +discard """ + exitcode: 0 + output: "" +""" + +when defined(windows): + doAssert detectOs(Windows) == true + doAssert detectOs(Ubuntu) == false + doAssert detectOs(MacOSX) == false + +when defined(linux): + doAssert detectOs(Ubuntu) == true + doAssert detectOs(Windows) == false + doAssert detectOs(MacOSX) == false + +when defined(macosx): + doAssert detectOs(MacOSX) == true + doAssert detectOs(Windows) == false + doAssert detectOs(Ubuntu) == false \ No newline at end of file From 236cf852fa8f6112cb0a4cc5446aff2ed0d69e33 Mon Sep 17 00:00:00 2001 From: Arnaud Moura Date: Thu, 16 Jan 2020 17:11:26 +0100 Subject: [PATCH 4/5] Fix Linux OS detection in Windows. --- lib/pure/distros.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/distros.nim b/lib/pure/distros.nim index 8bd4256492131..5e0f548f2384e 100644 --- a/lib/pure/distros.nim +++ b/lib/pure/distros.nim @@ -190,7 +190,7 @@ proc detectOsImpl(d: Distribution): bool = else: result = detectOsWithAllCmd(d) else: - result = detectOsWithAllCmd(d) + result = false template detectOs*(d: untyped): bool = ## Distro/OS detection. For convenience the From eea044fe6d485775480c6d7c6c0f97d492c57e8f Mon Sep 17 00:00:00 2001 From: Arnaud Moura Date: Tue, 21 Apr 2020 17:50:03 +0200 Subject: [PATCH 5/5] Fix OS detection for FreeBSD and OpenBSD. --- lib/pure/distros.nim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/pure/distros.nim b/lib/pure/distros.nim index c25928bce0109..dc9194b82907e 100644 --- a/lib/pure/distros.nim +++ b/lib/pure/distros.nim @@ -164,10 +164,15 @@ proc detectOsImpl(d: Distribution): bool = of Distribution.Linux: result = defined(linux) of Distribution.BSD: result = defined(bsd) else: - when defined(linux): + when defined(bsd): case d - of Distribution.Gentoo, Distribution.FreeBSD, - Distribution.OpenBSD: + of Distribution.FreeBSD, Distribution.OpenBSD: + result = $d in uname() + else: + result = false + elif defined(linux): + case d + of Distribution.Gentoo: result = ("-" & $d & " ") in uname() of Distribution.Elementary, Distribution.Ubuntu, Distribution.Debian, Distribution.Fedora, Distribution.OpenMandriva, Distribution.CentOS, Distribution.Alpine,