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 <rumpf_a@web.de>

* Update lib/posix/posix_utils.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

* Update changelog.md

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Juan Carlos
2021-01-04 11:21:36 -03:00
committed by GitHub
parent 7c2c1ad072
commit 349574d574
2 changed files with 26 additions and 1 deletions

View File

@@ -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`.

View File

@@ -11,7 +11,8 @@
# Where possible, contribute OS-independent procs in `os <os.html>`_ 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")