[PATCH v2 2/5] bbremote: Convert to python3
Sascha Hauer
s.hauer at pengutronix.de
Thu Sep 8 02:30:02 PDT 2022
This is the long overdue conversion from python2 to python3.
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
Notes:
Changes since v1:
- Also convert and test ratpfs
- Update documentation
Documentation/user/remote-control.rst | 10 ++++------
scripts/bbremote | 2 +-
scripts/remote/controller.py | 21 +++++++++++----------
scripts/remote/main.py | 12 ++++++------
scripts/remote/messages.py | 13 ++++++++++++-
scripts/remote/missing.py | 2 +-
scripts/remote/ratp.py | 4 ++--
scripts/remote/ratpfs.py | 27 +++++++++++++++++----------
scripts/remote/threadstdio.py | 4 ++--
9 files changed, 56 insertions(+), 39 deletions(-)
diff --git a/Documentation/user/remote-control.rst b/Documentation/user/remote-control.rst
index 43f1fb3118..b285c2297b 100644
--- a/Documentation/user/remote-control.rst
+++ b/Documentation/user/remote-control.rst
@@ -35,17 +35,15 @@ can also be enabled.
Running the bbremote tool
-------------------------
-The bbremote host tool is written in python. To run it python2 has to be
+The bbremote host tool is written in python. To run it python3 has to be
installed with the following additional packages:
+----------------+---------------------+
| python package | Debian package name |
+================+=====================+
-| crcmod | python-crcmod |
+| crcmod | python3-crcmod |
+----------------+---------------------+
-| enum | python-enum |
-+----------------+---------------------+
-| enum34 | python-enum34 |
+| pyserial | python3-serial |
+----------------+---------------------+
If your distribution does not provide aforementioned packages, you can
@@ -54,7 +52,7 @@ account via:
.. code-block:: sh
- python2 -m pip install --user crcmod enum enum34
+ python -m pip install --user crcmod pyserial
configuring bbremote
^^^^^^^^^^^^^^^^^^^^
diff --git a/scripts/bbremote b/scripts/bbremote
index bc5351dbae..1eeabd08d1 100755
--- a/scripts/bbremote
+++ b/scripts/bbremote
@@ -1,3 +1,3 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
import remote.main
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index b4493591dd..a3ae260558 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
@@ -8,7 +8,7 @@ import logging
import sys
import os
from threading import Thread
-from Queue import Queue, Empty
+from queue import Queue, Empty
from .ratpfs import RatpFSServer
from .messages import *
from .ratp import RatpError
@@ -105,7 +105,7 @@ class Controller(Thread):
self.rxq = None
self.conn.connect(timeout=5.0)
self._txq = Queue()
- self._stop = False
+ self._stopit = False
self.fsserver = RatpFSServer()
def _send(self, bbpkt):
@@ -147,24 +147,24 @@ class Controller(Thread):
return 0
def command(self, cmd):
- self._send(BBPacketCommand(cmd=cmd))
+ self._send(BBPacketCommand(cmd=cmd.encode()))
r = self._expect(BBPacketCommandReturn, timeout=None)
logging.info("Command: %r", r)
return r.exit_code
def getenv(self, varname):
- self._send(BBPacketGetenv(varname=varname))
+ self._send(BBPacketGetenv(varname=varname.encode()))
r = self._expect(BBPacketGetenvReturn)
return r.text
def md(self, path, addr, size):
- self._send(BBPacketMd(path=path, addr=addr, size=size))
+ self._send(BBPacketMd(path=path.encode(), addr=addr, size=size))
r = self._expect(BBPacketMdReturn)
logging.info("Md return: %r", r)
return (r.exit_code,r.data)
def mw(self, path, addr, data):
- self._send(BBPacketMw(path=path, addr=addr, data=data))
+ self._send(BBPacketMw(path=path.encode(), addr=addr, data=data))
r = self._expect(BBPacketMwReturn)
logging.info("Mw return: %r", r)
return (r.exit_code,r.written)
@@ -208,7 +208,7 @@ class Controller(Thread):
def run(self):
assert self.rxq is not None
try:
- while not self._stop:
+ while not self._stopit:
# receive
pkt = self.conn.recv()
if pkt:
@@ -235,15 +235,16 @@ class Controller(Thread):
Thread.start(self)
def stop(self):
- self._stop = True
+ self._stopit = True
self.join()
- self._stop = False
+ self._stopit = False
self.rxq = None
def send_async(self, pkt):
self._txq.put(pkt)
def send_async_console(self, text):
+ assert isinstance(text, bytes)
self._txq.put(BBPacketConsoleMsg(text=text))
def send_async_ping(self):
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index cef5d92ee2..2f3ce370ae 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
from __future__ import absolute_import, division, print_function
@@ -7,7 +7,7 @@ import os
import argparse
import binascii
import logging
-from Queue import Queue
+from queue import Queue
from .ratp import RatpError
try:
@@ -48,7 +48,7 @@ def get_controller(args):
def handle_run(args):
ctrl = get_controller(args)
- ctrl.export(args.export)
+ ctrl.export(args.export.encode())
res = ctrl.command(' '.join(args.arg))
if res:
res = 1
@@ -160,10 +160,10 @@ def handle_listen(args):
def handle_console(args):
queue = Queue()
ctrl = get_controller(args)
- ctrl.export(args.export)
+ ctrl.export(args.export.encode())
ctrl.start(queue)
- ctrl.send_async_console('\r')
- cons = ConsoleInput(queue, exit='\x14') # CTRL-T
+ ctrl.send_async_console(b'\r')
+ cons = ConsoleInput(queue, exit=b'\x14') # CTRL-T
cons.start()
try:
while True:
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index abd331c8b6..76cccad393 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
@@ -47,6 +47,7 @@ class BBPacket(object):
return "BBPacket(%i, %i)" % (self.p_type, self.p_flags)
def _unpack_payload(self, data):
+ assert isinstance(data, bytes)
self.payload = data
def _pack_payload(self):
@@ -63,6 +64,7 @@ class BBPacket(object):
class BBPacketCommand(BBPacket):
def __init__(self, raw=None, cmd=None):
+ assert isinstance(cmd, bytes)
self.cmd = cmd
super(BBPacketCommand, self).__init__(BBType.command, raw=raw)
@@ -70,6 +72,7 @@ class BBPacketCommand(BBPacket):
return "BBPacketCommand(cmd=%r)" % self.cmd
def _unpack_payload(self, payload):
+ assert isinstance(payload, bytes)
self.cmd = payload
def _pack_payload(self):
@@ -94,6 +97,8 @@ class BBPacketCommandReturn(BBPacket):
class BBPacketConsoleMsg(BBPacket):
def __init__(self, raw=None, text=None):
+ if text is not None:
+ assert isinstance(text, bytes)
self.text = text
super(BBPacketConsoleMsg, self).__init__(BBType.consolemsg, raw=raw)
@@ -101,6 +106,7 @@ class BBPacketConsoleMsg(BBPacket):
return "BBPacketConsoleMsg(text=%r)" % self.text
def _unpack_payload(self, payload):
+ assert isinstance(payload, bytes)
self.text = payload
def _pack_payload(self):
@@ -125,6 +131,7 @@ class BBPacketPong(BBPacket):
class BBPacketGetenv(BBPacket):
def __init__(self, raw=None, varname=None):
+ assert isinstance(varname, bytes)
self.varname = varname
super(BBPacketGetenv, self).__init__(BBType.getenv, raw=raw)
@@ -132,6 +139,7 @@ class BBPacketGetenv(BBPacket):
return "BBPacketGetenv(varname=%r)" % self.varname
def _unpack_payload(self, payload):
+ assert isinstance(payload, bytes)
self.varname = payload
def _pack_payload(self):
@@ -148,6 +156,7 @@ class BBPacketGetenvReturn(BBPacket):
return "BBPacketGetenvReturn(varvalue=%s)" % self.text
def _unpack_payload(self, payload):
+ assert isinstance(payload, bytes)
self.text = payload
def _pack_payload(self):
@@ -172,6 +181,7 @@ class BBPacketFSReturn(BBPacket):
class BBPacketMd(BBPacket):
def __init__(self, raw=None, path=None, addr=None, size=None):
+ assert isinstance(path, bytes)
self.path = path
self.addr = addr
self.size = size
@@ -214,6 +224,7 @@ class BBPacketMdReturn(BBPacket):
class BBPacketMw(BBPacket):
def __init__(self, raw=None, path=None, addr=None, data=None):
+ assert isinstance(path, bytes)
self.path = path
self.addr = addr
self.data = data
diff --git a/scripts/remote/missing.py b/scripts/remote/missing.py
index 67c2dfa8c0..9ed86bc10e 100644
--- a/scripts/remote/missing.py
+++ b/scripts/remote/missing.py
@@ -25,4 +25,4 @@ def monotonic():
return t.tv_sec + t.tv_nsec * 1e-9
if __name__ == "__main__":
- print monotonic()
+ print(monotonic())
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index 44f3e2f40a..25ca442d15 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
@@ -593,7 +593,7 @@ class RatpConnection(object):
# reassemble
if r.c_eor:
logging.info("Reassembling %i frames", len(self._rx_buf))
- self._rx_queue.append(''.join(self._rx_buf))
+ self._rx_queue.append(b''.join(self._rx_buf))
self._rx_buf = []
s = RatpPacket(flags='A')
diff --git a/scripts/remote/ratpfs.py b/scripts/remote/ratpfs.py
index 91ca044540..3e05cf2418 100644
--- a/scripts/remote/ratpfs.py
+++ b/scripts/remote/ratpfs.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
@@ -36,7 +36,9 @@ class RatpFSError(ValueError):
class RatpFSPacket(object):
- def __init__(self, type=RatpFSType.invalid, payload="", raw=None):
+ def __init__(self, type=RatpFSType.invalid, payload=b'', raw=None):
+ if payload is not None:
+ assert isinstance(payload, bytes)
if raw is not None:
type, = struct.unpack('!B', raw[:1])
self.type = RatpFSType(type)
@@ -57,9 +59,11 @@ class RatpFSPacket(object):
class RatpFSServer(object):
def __init__(self, path=None):
- self.path = path
if path:
+ assert isinstance(path, bytes)
self.path = os.path.abspath(os.path.expanduser(path))
+ else:
+ self.path = path
self.next_handle = 1 # 0 is invalid
self.files = {}
self.mounted = False
@@ -71,11 +75,13 @@ class RatpFSServer(object):
return handle
def _resolve(self, path):
- components = path.split('/')
+ assert isinstance(path, bytes)
+ components = path.split(b'/')
components = [x for x in components if x and x != '..']
return os.path.join(self.path, *components)
def handle_stat(self, path):
+ assert isinstance(path, bytes)
try:
logging.info("path: %r", path)
@@ -97,7 +103,7 @@ class RatpFSServer(object):
os.O_TRUNC)
path = params[4:]
try:
- f = os.open(self._resolve(path), flags, 0666)
+ f = os.open(self._resolve(path), flags, 0o666)
except OSError as e:
return struct.pack('!II', 0, e.errno)
h = self._alloc_handle()
@@ -118,24 +124,25 @@ class RatpFSServer(object):
f = self.files[h]
pos = os.lseek(f, pos, os.SEEK_SET)
assert os.write(f, payload) == len(payload)
- return ""
+ return b""
def handle_readdir(self, path):
- res = ""
+ assert isinstance(path, bytes)
+ res = b""
for x in os.listdir(self._resolve(path)):
- res += x+'\0'
+ res += x+b'\0'
return res
def handle_close(self, params):
h, = struct.unpack('!I', params[:4])
os.close(self.files.pop(h))
- return ""
+ return b""
def handle_truncate(self, params):
h, size = struct.unpack('!II', params)
f = self.files[h]
os.ftruncate(f, size)
- return ""
+ return b""
def handle(self, bbcall):
assert isinstance(bbcall, BBPacketFS)
diff --git a/scripts/remote/threadstdio.py b/scripts/remote/threadstdio.py
index db249892ac..d8ad71413c 100644
--- a/scripts/remote/threadstdio.py
+++ b/scripts/remote/threadstdio.py
@@ -1,11 +1,11 @@
-#!/usr/bin/python2
+#!/usr/bin/env python3
import os
import sys
import termios
import atexit
from threading import Thread
-from Queue import Queue, Empty
+from queue import Queue, Empty
class ConsoleInput(Thread):
def __init__(self, queue, exit='\x14'):
--
2.30.2
More information about the barebox
mailing list