diff --git a/pwd/metadata.txt b/pwd/metadata.txt new file mode 100644 index 00000000..55d84737 --- /dev/null +++ b/pwd/metadata.txt @@ -0,0 +1,5 @@ +srctype = micropython-lib +type = module +version = 0.1 +author = Riccardo Magliocchetti +depends = ffilib diff --git a/pwd/pwd.py b/pwd/pwd.py new file mode 100644 index 00000000..c973b9b2 --- /dev/null +++ b/pwd/pwd.py @@ -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) diff --git a/pwd/setup.py b/pwd/setup.py new file mode 100644 index 00000000..25a14aa5 --- /dev/null +++ b/pwd/setup.py @@ -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']) diff --git a/pwd/test_getpwnam.py b/pwd/test_getpwnam.py new file mode 100644 index 00000000..ac836d52 --- /dev/null +++ b/pwd/test_getpwnam.py @@ -0,0 +1,7 @@ +import pwd +import os + +user = os.getenv('USER') +passwd = pwd.getpwnam(user) +assert passwd +assert isinstance(passwd, pwd.struct_passwd)