def realpath(path): libc = ffilib.libc() if isinstance(path, str) or isinstance(path, bytes): realpath_ = libc.func("s", "realpath", "ss") # XXX: memory leak! should free() returned pointer, see man realpath return realpath_(path, None) raise TypeError
def get_sim_root_dirs(): # return a single path and list of files to pretend to find there import ffilib, os libc = ffilib.libc() b = bytearray(500) cwd = libc.func("s", "getcwd", "pi")(b, len(b)) assert cwd return cwd, cwd + '/MicroSD'
def realpath(path): libc = ffilib.libc() if isinstance(path, str) or isinstance(path, bytes): realpath_ = libc.func("s", "realpath", "ss") # XXX: memory leak! should free() returned pointer, see man realpath res = realpath_(path, None) if res is not None: return res # Assume that file doesn't exist, return abspath. return abspath(path) raise TypeError
from utime import * import ustruct import uctypes import ffi import ffilib import array libc = ffilib.libc() # struct tm *gmtime(const time_t *timep); # struct tm *localtime(const time_t *timep); # size_t strftime(char *s, size_t max, const char *format, # const struct tm *tm); gmtime_ = libc.func("P", "gmtime", "P") localtime_ = libc.func("P", "localtime", "P") strftime_ = libc.func("i", "strftime", "sisP") mktime_ = libc.func("i", "mktime", "P") def _tuple_to_c_tm(t): return ustruct.pack("@iiiiiiiii", t[5], t[4], t[3], t[2], t[1] - 1, t[0] - 1900, (t[6] + 1) % 7, t[7] - 1, t[8]) def _c_tm_to_tuple(tm): t = ustruct.unpack("@iiiiiiiii", tm) return tuple([ t[5] + 1900, t[4] + 1, t[3], t[2], t[1], t[0], (t[6] - 1) % 7, t[7] + 1, t[8] ])
O_CREAT = 0o0000100 O_EXCL = 0o0000200 O_NOCTTY = 0o0000400 O_TRUNC = 0o0001000 O_APPEND = 0o0002000 O_NONBLOCK = 0o0004000 error = OSError name = "posix" sep = "/" curdir = "." pardir = ".." environ = {"WARNING": "NOT_IMPLEMENTED"} libc = ffilib.libc() if libc: chdir_ = libc.func("i", "chdir", "s") mkdir_ = libc.func("i", "mkdir", "si") rename_ = libc.func("i", "rename", "ss") unlink_ = libc.func("i", "unlink", "s") rmdir_ = libc.func("i", "rmdir", "s") getcwd_ = libc.func("s", "getcwd", "si") opendir_ = libc.func("P", "opendir", "s") readdir_ = libc.func("P", "readdir", "P") open_ = libc.func("i", "open", "sii") read_ = libc.func("i", "read", "ipi") write_ = libc.func("i", "write", "iPi") close_ = libc.func("i", "close", "i") dup_ = libc.func("i", "dup", "i")
import socket INET_ADDRSTRLEN = 16 INET6_ADDRSTRLEN = 46 inet_ntoa = getattr(socket, "inet_ntoa", None) if not inet_ntoa: import ffilib inet_ntoa = ffilib.libc().func("s", "inet_ntoa", "p") inet_ntop = getattr(socket, "inet_ntop", None) if not inet_ntop: import ffilib _inet_ntop = ffilib.libc().func("s", "inet_ntop", "iPpi") def inet_ntop(af, addr): buf = bytearray(INET_ADDRSTRLEN if af == socket.AF_INET else INET6_ADDRSTRLEN) res = _inet_ntop(af, addr, buf, INET_ADDRSTRLEN) return res def get_hostport(addr): if isinstance(addr, tuple): return addr af, addr, port = socket.sockaddr(addr) return inet_ntop(af, addr), port
import socket INET_ADDRSTRLEN = 16 INET6_ADDRSTRLEN = 46 inet_ntoa = getattr(socket, 'inet_ntoa', None) if not inet_ntoa: import ffilib inet_ntoa = ffilib.libc().func("s", "inet_ntoa", "p") inet_ntop = getattr(socket, 'inet_ntop', None) if not inet_ntop: import ffilib _inet_ntop = ffilib.libc().func("s", "inet_ntop", "iPpi") def inet_ntop(af, addr): buf = bytearray(INET_ADDRSTRLEN if af == socket.AF_INET else INET6_ADDRSTRLEN) res = _inet_ntop(af, addr, buf, INET_ADDRSTRLEN) return res def get_hostport(addr): if isinstance(addr, tuple): return addr af, addr, port = socket.sockaddr(addr) return inet_ntop(af, addr), port
from socket import * import usocket as _socket import os import signal import ffilib alarm = ffilib.libc().func("I", "alarm", "I") __socket = socket del socket log = lambda x: print(x) _timeout_methods = ('accept', 'bind', 'connect', 'sendall', 'sendto', 'recvfrom') class timeout(OSError): pass def timeout_handler(sig): raise timeout() signal.signal(14, timeout_handler) class socketRecvfromFixed(__socket): def __init__(self, *a, **b): super().__init__(*a, **b) def recvfrom(self, *a, **b):
import os import sys import time from errno import EINTR from ffilib import libc from uselect import poll from uselect import POLLIN from uctypes import addressof from uctypes import sizeof from uctypes import struct from uctypes import UINT32 from uctypes import UINT64 _libc = libc() _eventfd = _libc.func('i', 'eventfd', 'ii') _EFD_CLOEXEC = 0o2000000 def debug_print(*args, sep=' ', end='\n'): """Print on stderr for debugging Parameters: args: One or more arguments to print sep (str): Separator inserted between arguments end (str): Appended after the last argument """ print(*args, sep=sep, end=end, file=sys.stderr)