pwd: add basic implementation of pwd

The only function implemented is getpwnam
pull/212/merge
Riccardo Magliocchetti 2017-10-03 13:36:17 +02:00 zatwierdzone przez Paul Sokolovsky
rodzic 83b6c02881
commit d040285fbd
4 zmienionych plików z 57 dodań i 0 usunięć

5
pwd/metadata.txt 100644
Wyświetl plik

@ -0,0 +1,5 @@
srctype = micropython-lib
type = module
version = 0.1
author = Riccardo Magliocchetti
depends = ffilib

24
pwd/pwd.py 100644
Wyświetl plik

@ -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)

21
pwd/setup.py 100644
Wyświetl plik

@ -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'])

Wyświetl plik

@ -0,0 +1,7 @@
import pwd
import os
user = os.getenv('USER')
passwd = pwd.getpwnam(user)
assert passwd
assert isinstance(passwd, pwd.struct_passwd)