From 349574d5745944ed2183b196113026f804393f29 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Mon, 4 Jan 2021 11:21:36 -0300 Subject: [PATCH] Add posix_utils.osReleaseFile (#16452) * Add posix_utils.osReleaseFile * Update lib/posix/posix_utils.nim Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com> * Update lib/posix/posix_utils.nim Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com> * Add a basic sanity test * Add a basic sanity test * Add a basic sanity test * Add a basic sanity test * https://github.com/nim-lang/Nim/pull/16452#issuecomment-753364096 * Update lib/posix/posix_utils.nim Co-authored-by: Andreas Rumpf * Update lib/posix/posix_utils.nim Co-authored-by: Andreas Rumpf * Update changelog.md Co-authored-by: Andreas Rumpf Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com> Co-authored-by: Andreas Rumpf --- changelog.md | 5 +++++ lib/posix/posix_utils.nim | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index b10b9607a..11ac33872 100644 --- a/changelog.md +++ b/changelog.md @@ -85,6 +85,11 @@ - Added `mimetypes.mimesMaxLen` thats equal to the length of the longest "mime" from `mimes`. + +- Added `posix_utils.osReleaseFile` to get system identification from `os-release` file on Linux and the BSDs. + https://www.freedesktop.org/software/systemd/man/os-release.html + + ## Language changes - `nimscript` now handles `except Exception as e`. diff --git a/lib/posix/posix_utils.nim b/lib/posix/posix_utils.nim index d083e20b9..7fd8b1fc9 100644 --- a/lib/posix/posix_utils.nim +++ b/lib/posix/posix_utils.nim @@ -11,7 +11,8 @@ # Where possible, contribute OS-independent procs in `os `_ instead. -import posix +import posix, parsecfg, os +import std/private/since type Uname* = object sysname*, nodename*, release*, version*, machine*: string @@ -107,3 +108,22 @@ proc mkdtemp*(prefix: string): string = if mkdtemp(tmpl) == nil: raise newException(OSError, $strerror(errno)) return $tmpl + +proc osReleaseFile*(): Config {.since: (1, 5).} = + ## Gets system identification from `os-release` file and returns it as a `parsecfg.Config`. + ## You also need to import the `parsecfg` module to gain access to this object. + ## The `os-release` file is an official Freedesktop.org open standard. + ## Available in Linux and BSD distributions, except Android and Android-based Linux. + ## `os-release` file is not available on Windows and OS X by design. + ## * https://www.freedesktop.org/software/systemd/man/os-release.html + runnableExamples: + import parsecfg + when defined(linux): + let data = osReleaseFile() + doAssert data.getSectionValue("", "NAME").len > 0 ## the data is up to each distro. + + # We do not use a {.strdefine.} because Standard says it *must* be that path. + for osReleaseFile in ["/etc/os-release", "/usr/lib/os-release"]: + if fileExists(osReleaseFile): + return loadConfig(osReleaseFile) + raise newException(IOError, "File not found: /etc/os-release, /usr/lib/os-release")