mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 13:10:21 +01:00
all: Replace all uses of umodule in Python code.
Applies to drivers/examples/extmod/port-modules/tools. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
@@ -137,14 +137,14 @@ def do_filesystem(state, args):
|
||||
raise CommandError("'cp -r' source files must be local")
|
||||
_list_recursive(src_files, path)
|
||||
known_dirs = {""}
|
||||
state.transport.exec_("import uos")
|
||||
state.transport.exec_("import os")
|
||||
for dir, file in src_files:
|
||||
dir_parts = dir.split("/")
|
||||
for i in range(len(dir_parts)):
|
||||
d = "/".join(dir_parts[: i + 1])
|
||||
if d not in known_dirs:
|
||||
state.transport.exec_(
|
||||
"try:\n uos.mkdir('%s')\nexcept OSError as e:\n print(e)" % d
|
||||
"try:\n os.mkdir('%s')\nexcept OSError as e:\n print(e)" % d
|
||||
)
|
||||
known_dirs.add(d)
|
||||
state.transport.filesystem_command(
|
||||
|
||||
@@ -317,7 +317,7 @@ _BUILTIN_COMMAND_EXPANSIONS = {
|
||||
# Disk used/free.
|
||||
"df": [
|
||||
"exec",
|
||||
"import uos\nprint('mount \\tsize \\tused \\tavail \\tuse%')\nfor _m in [''] + uos.listdir('/'):\n _s = uos.stat('/' + _m)\n if not _s[0] & 1 << 14: continue\n _s = uos.statvfs(_m)\n if _s[0]:\n _size = _s[0] * _s[2]; _free = _s[0] * _s[3]; print(_m, _size, _size - _free, _free, int(100 * (_size - _free) / _size), sep='\\t')",
|
||||
"import os\nprint('mount \\tsize \\tused \\tavail \\tuse%')\nfor _m in [''] + os.listdir('/'):\n _s = os.stat('/' + _m)\n if not _s[0] & 1 << 14: continue\n _s = os.statvfs(_m)\n if _s[0]:\n _size = _s[0] * _s[2]; _free = _s[0] * _s[3]; print(_m, _size, _size - _free, _free, int(100 * (_size - _free) / _size), sep='\\t')",
|
||||
],
|
||||
# Other shortcuts.
|
||||
"reset": {
|
||||
|
||||
@@ -292,14 +292,14 @@ class SerialTransport(Transport):
|
||||
|
||||
def fs_exists(self, src):
|
||||
try:
|
||||
self.exec("import uos\nuos.stat(%s)" % (("'%s'" % src) if src else ""))
|
||||
self.exec("import os\nos.stat(%s)" % (("'%s'" % src) if src else ""))
|
||||
return True
|
||||
except TransportError:
|
||||
return False
|
||||
|
||||
def fs_ls(self, src):
|
||||
cmd = (
|
||||
"import uos\nfor f in uos.ilistdir(%s):\n"
|
||||
"import os\nfor f in os.ilistdir(%s):\n"
|
||||
" print('{:12} {}{}'.format(f[3]if len(f)>3 else 0,f[0],'/'if f[1]&0x4000 else ''))"
|
||||
% (("'%s'" % src) if src else "")
|
||||
)
|
||||
@@ -311,7 +311,7 @@ class SerialTransport(Transport):
|
||||
def repr_consumer(b):
|
||||
buf.extend(b.replace(b"\x04", b""))
|
||||
|
||||
cmd = "import uos\nfor f in uos.ilistdir(%s):\n" " print(repr(f), end=',')" % (
|
||||
cmd = "import os\nfor f in os.ilistdir(%s):\n" " print(repr(f), end=',')" % (
|
||||
("'%s'" % src) if src else ""
|
||||
)
|
||||
try:
|
||||
@@ -328,8 +328,8 @@ class SerialTransport(Transport):
|
||||
|
||||
def fs_stat(self, src):
|
||||
try:
|
||||
self.exec("import uos")
|
||||
return os.stat_result(self.eval("uos.stat(%s)" % (("'%s'" % src)), parse=True))
|
||||
self.exec("import os")
|
||||
return os.stat_result(self.eval("os.stat(%s)" % (("'%s'" % src)), parse=True))
|
||||
except TransportError as e:
|
||||
reraise_filesystem_error(e, src)
|
||||
|
||||
@@ -422,13 +422,13 @@ class SerialTransport(Transport):
|
||||
self.exec("f.close()")
|
||||
|
||||
def fs_mkdir(self, dir):
|
||||
self.exec("import uos\nuos.mkdir('%s')" % dir)
|
||||
self.exec("import os\nos.mkdir('%s')" % dir)
|
||||
|
||||
def fs_rmdir(self, dir):
|
||||
self.exec("import uos\nuos.rmdir('%s')" % dir)
|
||||
self.exec("import os\nos.rmdir('%s')" % dir)
|
||||
|
||||
def fs_rm(self, src):
|
||||
self.exec("import uos\nuos.remove('%s')" % src)
|
||||
self.exec("import os\nos.remove('%s')" % src)
|
||||
|
||||
def fs_touch(self, src):
|
||||
self.exec("f=open('%s','a')\nf.close()" % src)
|
||||
@@ -595,7 +595,7 @@ class SerialTransport(Transport):
|
||||
|
||||
def umount_local(self):
|
||||
if self.mounted:
|
||||
self.exec('uos.umount("/remote")')
|
||||
self.exec('os.umount("/remote")')
|
||||
self.mounted = False
|
||||
self.serial = self.serial.orig_serial
|
||||
|
||||
@@ -616,18 +616,18 @@ fs_hook_cmds = {
|
||||
}
|
||||
|
||||
fs_hook_code = """\
|
||||
import uos, uio, ustruct, micropython
|
||||
import os, io, struct, micropython
|
||||
|
||||
SEEK_SET = 0
|
||||
|
||||
class RemoteCommand:
|
||||
def __init__(self):
|
||||
import uselect, usys
|
||||
import select, sys
|
||||
self.buf4 = bytearray(4)
|
||||
self.fout = usys.stdout.buffer
|
||||
self.fin = usys.stdin.buffer
|
||||
self.poller = uselect.poll()
|
||||
self.poller.register(self.fin, uselect.POLLIN)
|
||||
self.fout = sys.stdout.buffer
|
||||
self.fin = sys.stdin.buffer
|
||||
self.poller = select.poll()
|
||||
self.poller.register(self.fin, select.POLLIN)
|
||||
|
||||
def poll_in(self):
|
||||
for _ in self.poller.ipoll(1000):
|
||||
@@ -710,7 +710,7 @@ class RemoteCommand:
|
||||
self.fout.write(self.buf4, 1)
|
||||
|
||||
def wr_s32(self, i):
|
||||
ustruct.pack_into('<i', self.buf4, 0, i)
|
||||
struct.pack_into('<i', self.buf4, 0, i)
|
||||
self.fout.write(self.buf4)
|
||||
|
||||
def wr_bytes(self, b):
|
||||
@@ -721,7 +721,7 @@ class RemoteCommand:
|
||||
wr_str = wr_bytes
|
||||
|
||||
|
||||
class RemoteFile(uio.IOBase):
|
||||
class RemoteFile(io.IOBase):
|
||||
def __init__(self, cmd, fd, is_text):
|
||||
self.cmd = cmd
|
||||
self.fd = fd
|
||||
@@ -934,8 +934,8 @@ class RemoteFS:
|
||||
|
||||
|
||||
def __mount():
|
||||
uos.mount(RemoteFS(RemoteCommand()), '/remote')
|
||||
uos.chdir('/remote')
|
||||
os.mount(RemoteFS(RemoteCommand()), '/remote')
|
||||
os.chdir('/remote')
|
||||
"""
|
||||
|
||||
# Apply basic compression on hook code.
|
||||
|
||||
@@ -509,14 +509,14 @@ class Pyboard:
|
||||
|
||||
def fs_exists(self, src):
|
||||
try:
|
||||
self.exec_("import uos\nuos.stat(%s)" % (("'%s'" % src) if src else ""))
|
||||
self.exec_("import os\nos.stat(%s)" % (("'%s'" % src) if src else ""))
|
||||
return True
|
||||
except PyboardError:
|
||||
return False
|
||||
|
||||
def fs_ls(self, src):
|
||||
cmd = (
|
||||
"import uos\nfor f in uos.ilistdir(%s):\n"
|
||||
"import os\nfor f in os.ilistdir(%s):\n"
|
||||
" print('{:12} {}{}'.format(f[3]if len(f)>3 else 0,f[0],'/'if f[1]&0x4000 else ''))"
|
||||
% (("'%s'" % src) if src else "")
|
||||
)
|
||||
@@ -528,7 +528,7 @@ class Pyboard:
|
||||
def repr_consumer(b):
|
||||
buf.extend(b.replace(b"\x04", b""))
|
||||
|
||||
cmd = "import uos\nfor f in uos.ilistdir(%s):\n" " print(repr(f), end=',')" % (
|
||||
cmd = "import os\nfor f in os.ilistdir(%s):\n" " print(repr(f), end=',')" % (
|
||||
("'%s'" % src) if src else ""
|
||||
)
|
||||
try:
|
||||
@@ -545,8 +545,8 @@ class Pyboard:
|
||||
|
||||
def fs_stat(self, src):
|
||||
try:
|
||||
self.exec_("import uos")
|
||||
return os.stat_result(self.eval("uos.stat(%s)" % (("'%s'" % src)), parse=True))
|
||||
self.exec_("import os")
|
||||
return os.stat_result(self.eval("os.stat(%s)" % (("'%s'" % src)), parse=True))
|
||||
except PyboardError as e:
|
||||
raise e.convert(src)
|
||||
|
||||
@@ -639,13 +639,13 @@ class Pyboard:
|
||||
self.exec_("f.close()")
|
||||
|
||||
def fs_mkdir(self, dir):
|
||||
self.exec_("import uos\nuos.mkdir('%s')" % dir)
|
||||
self.exec_("import os\nos.mkdir('%s')" % dir)
|
||||
|
||||
def fs_rmdir(self, dir):
|
||||
self.exec_("import uos\nuos.rmdir('%s')" % dir)
|
||||
self.exec_("import os\nos.rmdir('%s')" % dir)
|
||||
|
||||
def fs_rm(self, src):
|
||||
self.exec_("import uos\nuos.remove('%s')" % src)
|
||||
self.exec_("import os\nos.remove('%s')" % src)
|
||||
|
||||
def fs_touch(self, src):
|
||||
self.exec_("f=open('%s','a')\nf.close()" % src)
|
||||
@@ -737,9 +737,9 @@ def filesystem_command(pyb, args, progress_callback=None, verbose=False):
|
||||
|
||||
|
||||
_injected_import_hook_code = """\
|
||||
import uos, uio
|
||||
import os, io
|
||||
class _FS:
|
||||
class File(uio.IOBase):
|
||||
class File(io.IOBase):
|
||||
def __init__(self):
|
||||
self.off = 0
|
||||
def ioctl(self, request, arg):
|
||||
@@ -756,10 +756,10 @@ class _FS:
|
||||
raise OSError(-2) # ENOENT
|
||||
def open(self, path, mode):
|
||||
return self.File()
|
||||
uos.mount(_FS(), '/_')
|
||||
uos.chdir('/_')
|
||||
os.mount(_FS(), '/_')
|
||||
os.chdir('/_')
|
||||
from _injected import *
|
||||
uos.umount('/_')
|
||||
os.umount('/_')
|
||||
del _injected_buf, _FS
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user