kopia lustrzana https://github.com/micropython/micropython-lib
os: Implement listdir().
rodzic
2a2fdd7f43
commit
5e1007cf8b
|
@ -1,11 +1,14 @@
|
||||||
import ffi
|
import ffi
|
||||||
import array
|
import array
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
libc = ffi.open("libc.so.6")
|
libc = ffi.open("libc.so.6")
|
||||||
|
|
||||||
errno = libc.var("i", "errno")
|
errno = libc.var("i", "errno")
|
||||||
mkdir_ = libc.func("i", "mkdir", "si")
|
mkdir_ = libc.func("i", "mkdir", "si")
|
||||||
|
opendir_ = libc.func("P", "opendir", "s")
|
||||||
|
readdir_ = libc.func("P", "readdir", "P")
|
||||||
read_ = libc.func("i", "read", "ipi")
|
read_ = libc.func("i", "read", "ipi")
|
||||||
write_ = libc.func("i", "write", "iPi")
|
write_ = libc.func("i", "write", "iPi")
|
||||||
close_ = libc.func("i", "close", "i")
|
close_ = libc.func("i", "close", "i")
|
||||||
|
@ -25,6 +28,23 @@ def mkdir(name, mode=0o777):
|
||||||
e = mkdir_(name, mode)
|
e = mkdir_(name, mode)
|
||||||
check_error(e)
|
check_error(e)
|
||||||
|
|
||||||
|
def listdir(path="."):
|
||||||
|
dir = opendir_(path)
|
||||||
|
if not dir:
|
||||||
|
check_error(e)
|
||||||
|
res = []
|
||||||
|
dirent_fmt = "iiHB256s"
|
||||||
|
while True:
|
||||||
|
dirent = readdir_(dir)
|
||||||
|
if not dirent:
|
||||||
|
break
|
||||||
|
dirent = ffi.as_bytearray(dirent, struct.calcsize(dirent_fmt))
|
||||||
|
dirent = struct.unpack(dirent_fmt, dirent)
|
||||||
|
fname = str(dirent[4].split('\0', 1)[0], "ascii")
|
||||||
|
if fname != "." and fname != "..":
|
||||||
|
res.append(fname)
|
||||||
|
return res
|
||||||
|
|
||||||
def read(fd, n):
|
def read(fd, n):
|
||||||
buf = bytearray(n)
|
buf = bytearray(n)
|
||||||
r = read_(fd, buf, n)
|
r = read_(fd, buf, n)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
|
|
||||||
setup(name='micropython-os',
|
setup(name='micropython-os',
|
||||||
version='0.0.5',
|
version='0.0.6',
|
||||||
description='os module for MicroPython',
|
description='os module for MicroPython',
|
||||||
url='https://github.com/micropython/micropython/issues/405',
|
url='https://github.com/micropython/micropython/issues/405',
|
||||||
author='MicroPython Developers',
|
author='MicroPython Developers',
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
l = os.listdir()
|
||||||
|
print(l)
|
||||||
|
assert "test_listdir.py" in l
|
||||||
|
assert "os" in l
|
Ładowanie…
Reference in New Issue