Skip to content

Commit 50156e6

Browse files
committed
feat(Lib/os): uname
1 parent 3153a57 commit 50156e6

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

src/pylib/Lib/n_os.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ when not defined(js):
99
term, inheritable]
1010
export term, set_inheritable, get_inheritable
1111

12-
12+
genUname string
1313

src/pylib/Lib/os.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import ./os_impl/private/platform_utils
1111
import ../pyconfig/bootstrap_hash
1212

1313
import ./n_os
14-
export n_os except scandir, DirEntry, urandom, getrandom
14+
export n_os except scandir, DirEntry, urandom, getrandom, genUname, uname, uname_result
1515

1616
import ../version
17-
17+
genUname PyStr
1818
template scandir*(): untyped{.pysince(3,5).} = n_os.scandir()
1919
template scandir*[T](p: PathLike[T]): untyped{.pysince(3,5).} = n_os.scandir(p)
2020
template scandir*(p: int): untyped{.pysince(3,5).} = n_os.scandir(p)

src/pylib/Lib/os_impl/posix_like.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import ./posix_like/[
44
fdopen, open_close, lseek, seek_consts, truncate, stat, scandirImpl, mkrmdir, unlink,
5-
rename, isatty, links, utime, get_id, chmods, umaskImpl
5+
rename, isatty, links, utime, get_id, chmods, umaskImpl, unameImpl,
66
]
77

88
export seek_consts except toCSEEK
99
export stat except statAttr
1010
export
1111
fdopen, open_close, lseek, truncate, scandirImpl, mkrmdir, unlink,
12-
rename, isatty, links, utime, get_id, chmods, umaskImpl
12+
rename, isatty, links, utime, get_id, chmods, umaskImpl, unameImpl
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
import ./pyCfg
3+
importConfig [os]
4+
template genUnameResult(S){.dirty.} =
5+
type uname_result* = tuple[
6+
sysname,
7+
nodename,
8+
release,
9+
version,
10+
machine: S
11+
]
12+
when HAVE_UNAME:
13+
import std/posix
14+
import ./errnoHandle
15+
proc utsFieldToString[T](field: T): string =
16+
# Implementation of utsFieldToString goes here
17+
when T is array: # array of char
18+
result = newStringOfCap(field.len)
19+
var i = 0
20+
while field[i] != '\0':
21+
assert i < field.len
22+
result.add field[i]
23+
i.inc
24+
else: $field
25+
template posix_uname(u): cint =
26+
bind uname
27+
uname(u)
28+
template genUname*(S){.dirty.} =
29+
bind posix_uname, utsFieldToString, Utsname
30+
bind genUnameResult
31+
genUnameResult(S)
32+
proc uname*(): uname_result =
33+
var u{.noInit.}: Utsname
34+
let res = posix_uname(u)
35+
if res < 0:
36+
raiseErrno()
37+
template SET(field) =
38+
result.field = S utsFieldToString(u.field)
39+
{.push hint[ConvFromXtoItselfNotNeeded]: off.}
40+
SET sysname
41+
SET nodename
42+
SET release
43+
SET version
44+
SET machine
45+
{.pop.}
46+
47+
elif InJs:
48+
import ../common
49+
template genUname*(S){.dirty.} =
50+
bind genUnameResult
51+
genUnameResult(S)
52+
proc uname*(): uname_result =
53+
proc getSysname: cstring{.importInNodeModOrDeno(os, "type", "build.os$").}
54+
proc getNodename: cstring{.importDenoOrNodeMod(os, hostname).}
55+
proc getRelease: cstring{.importInNodeModOrDeno(os, release, osRelease).}
56+
proc getVersion: cstring{.importNode(os, version).}
57+
proc getMachine: cstring{.importInNodeModOrDeno(os, machine, "build.arch$").}
58+
template SET(field) =
59+
result.field = S $`get field`()
60+
SET sysname
61+
SET nodename
62+
SET release
63+
SET version
64+
SET machine
65+
66+
#SET sysname, `type`, `build.os` # attr # "windows": "Windows_NT", caption
67+
else:
68+
template genUname*(S){.dirty.} =
69+
bind genUnameResult
70+
genUnameResult(S)
71+
# do nothing other than define the type
72+
73+
when isMainModule:
74+
genUname string
75+
echo uname()

src/pylib/pyconfig/os.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const
99
HAVE_FTRUNCATE* = true
1010

1111
AC_CHECK_FUNCS(
12+
uname,
1213
unlinkat,
1314
fdopendir,
1415
)

0 commit comments

Comments
 (0)