kopia lustrzana https://github.com/micropython/micropython-lib
pwd: add basic implementation of pwd
The only function implemented is getpwnampull/212/merge
rodzic
83b6c02881
commit
d040285fbd
|
@ -0,0 +1,5 @@
|
||||||
|
srctype = micropython-lib
|
||||||
|
type = module
|
||||||
|
version = 0.1
|
||||||
|
author = Riccardo Magliocchetti
|
||||||
|
depends = ffilib
|
|
@ -0,0 +1,24 @@
|
||||||
|
import ffilib
|
||||||
|
import uctypes
|
||||||
|
import ustruct
|
||||||
|
|
||||||
|
from ucollections import namedtuple
|
||||||
|
|
||||||
|
|
||||||
|
libc = ffilib.libc()
|
||||||
|
|
||||||
|
getpwnam_ = libc.func("P", "getpwnam", "s")
|
||||||
|
|
||||||
|
|
||||||
|
struct_passwd = namedtuple("struct_passwd",
|
||||||
|
["pw_name", "pw_passwd", "pw_uid", "pw_gid", "pw_gecos", "pw_dir", "pw_shell"])
|
||||||
|
|
||||||
|
|
||||||
|
def getpwnam(user):
|
||||||
|
passwd = getpwnam_(user)
|
||||||
|
if not passwd:
|
||||||
|
raise KeyError("getpwnam(): name not found: {}".format(user))
|
||||||
|
passwd_fmt = "SSIISSS"
|
||||||
|
passwd = uctypes.bytes_at(passwd, ustruct.calcsize(passwd_fmt))
|
||||||
|
passwd = ustruct.unpack(passwd_fmt, passwd)
|
||||||
|
return struct_passwd(*passwd)
|
|
@ -0,0 +1,21 @@
|
||||||
|
import sys
|
||||||
|
# Remove current dir from sys.path, otherwise setuptools will peek up our
|
||||||
|
# module instead of system's.
|
||||||
|
sys.path.pop(0)
|
||||||
|
from setuptools import setup
|
||||||
|
sys.path.append("..")
|
||||||
|
import optimize_upip
|
||||||
|
|
||||||
|
setup(name='micropython-pwd',
|
||||||
|
version='0.1',
|
||||||
|
description='pwd module for MicroPython',
|
||||||
|
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
|
||||||
|
url='https://github.com/micropython/micropython-lib',
|
||||||
|
author='Riccardo Magliocchetti',
|
||||||
|
author_email='micro-python@googlegroups.com',
|
||||||
|
maintainer='MicroPython Developers',
|
||||||
|
maintainer_email='micro-python@googlegroups.com',
|
||||||
|
license='MIT',
|
||||||
|
cmdclass={'optimize_upip': optimize_upip.OptimizeUpip},
|
||||||
|
py_modules=['pwd'],
|
||||||
|
install_requires=['micropython-ffilib'])
|
|
@ -0,0 +1,7 @@
|
||||||
|
import pwd
|
||||||
|
import os
|
||||||
|
|
||||||
|
user = os.getenv('USER')
|
||||||
|
passwd = pwd.getpwnam(user)
|
||||||
|
assert passwd
|
||||||
|
assert isinstance(passwd, pwd.struct_passwd)
|
Ładowanie…
Reference in New Issue