|
1 | 1 | # SPDX-License-Identifier: MIT |
2 | | -# Copyright (c) 2019-2021 The Pybricks Authors |
| 2 | +# Copyright (c) 2019-2022 The Pybricks Authors |
3 | 3 |
|
4 | 4 | """Command line wrapper around pybricksdev library.""" |
5 | 5 |
|
6 | 6 | import argparse |
7 | 7 | import asyncio |
8 | 8 | import contextlib |
9 | | -import json |
10 | 9 | import logging |
11 | 10 | import os |
12 | 11 | import sys |
13 | | -from tempfile import NamedTemporaryFile |
14 | | -from typing import ContextManager, TextIO |
15 | | -import validators |
16 | | -import zipfile |
17 | | - |
18 | 12 | from abc import ABC, abstractmethod |
19 | 13 | from os import PathLike, path |
| 14 | +from tempfile import NamedTemporaryFile |
| 15 | +from typing import ContextManager, TextIO |
20 | 16 |
|
21 | 17 | import argcomplete |
| 18 | +import validators |
22 | 19 | from argcomplete.completers import FilesCompleter |
23 | 20 |
|
24 | | -from .. import __name__ as MODULE_NAME, __version__ as MODULE_VERSION |
25 | | -from ..ble.lwp3 import LWP3_BOOTLOADER_SERVICE_UUID |
26 | | -from ..ble.lwp3.bytecodes import HubKind |
27 | | - |
| 21 | +from .. import __name__ as MODULE_NAME |
| 22 | +from .. import __version__ as MODULE_VERSION |
28 | 23 |
|
29 | 24 | PROG_NAME = ( |
30 | 25 | f"{path.basename(sys.executable)} -m {MODULE_NAME}" |
@@ -170,7 +165,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction): |
170 | 165 |
|
171 | 166 | async def run(self, args: argparse.Namespace): |
172 | 167 | from ..ble import find_device |
173 | | - from ..connections import PybricksHub, EV3Connection, REPLHub |
| 168 | + from ..connections import EV3Connection, PybricksHub, REPLHub |
174 | 169 |
|
175 | 170 | # Pick the right connection |
176 | 171 | if args.conntype == "ssh": |
@@ -223,64 +218,10 @@ def add_parser(self, subparsers: argparse._SubParsersAction): |
223 | 218 | "-n", "--name", metavar="<name>", type=str, help="a custom name for the hub" |
224 | 219 | ) |
225 | 220 |
|
226 | | - async def run(self, args: argparse.Namespace): |
227 | | - from ..flash import create_firmware |
228 | | - |
229 | | - print("Creating firmware") |
230 | | - firmware, metadata = await create_firmware(args.firmware, args.name) |
231 | | - |
232 | | - if metadata["device-id"] in (HubKind.TECHNIC_SMALL, HubKind.TECHNIC_LARGE): |
233 | | - from ..dfu import flash_dfu |
234 | | - from ..connections import REPLHub |
235 | | - |
236 | | - try: |
237 | | - # Connect to the hub and exit the runtime. |
238 | | - hub = REPLHub() |
239 | | - await hub.connect() |
240 | | - await hub.reset_hub() |
241 | | - |
242 | | - # Upload installation script. |
243 | | - archive = zipfile.ZipFile(args.firmware) |
244 | | - await hub.exec_line("import uos; uos.mkdir('_firmware')") |
245 | | - await hub.upload_file( |
246 | | - "_firmware/install_pybricks.py", |
247 | | - bytearray(archive.open("install_pybricks.py").read()), |
248 | | - ) |
249 | | - |
250 | | - # Upload metadata. |
251 | | - await hub.upload_file( |
252 | | - "_firmware/firmware.metadata.json", |
253 | | - json.dumps(metadata, indent=4).encode(), |
254 | | - ) |
255 | | - |
256 | | - # Upload Pybricks firmware |
257 | | - await hub.upload_file("_firmware/firmware.bin", firmware) |
258 | | - |
259 | | - # Run installation script |
260 | | - print("Installing firmware") |
261 | | - await hub.exec_line("from _firmware.install_pybricks import install") |
262 | | - await hub.exec_paste_mode("install()") |
263 | | - |
264 | | - except OSError: |
265 | | - print("Could not find hub in standard firmware mode. Trying DFU.") |
266 | | - flash_dfu(firmware, metadata) |
267 | | - else: |
268 | | - from ..ble import find_device |
269 | | - from ..flash import BootloaderConnection |
270 | | - |
271 | | - print("Searching for LEGO Bootloader...") |
272 | | - |
273 | | - try: |
274 | | - device = await find_device(service=LWP3_BOOTLOADER_SERVICE_UUID) |
275 | | - except asyncio.TimeoutError: |
276 | | - print("timed out") |
277 | | - return |
| 221 | + def run(self, args: argparse.Namespace): |
| 222 | + from .flash import flash_firmware |
278 | 223 |
|
279 | | - print("Found:", device) |
280 | | - updater = BootloaderConnection() |
281 | | - await updater.connect(device) |
282 | | - print("Erasing flash and starting update") |
283 | | - await updater.flash(firmware, metadata) |
| 224 | + return flash_firmware(args.firmware, args.name) |
284 | 225 |
|
285 | 226 |
|
286 | 227 | class DFUBackup(Tool): |
@@ -352,7 +293,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction): |
352 | 293 | parser.tool = self |
353 | 294 |
|
354 | 295 | def run(self, args: argparse.Namespace): |
355 | | - from .lwp3.repl import setup_repl_logging, repl |
| 296 | + from .lwp3.repl import repl, setup_repl_logging |
356 | 297 |
|
357 | 298 | setup_repl_logging() |
358 | 299 | return repl() |
@@ -387,6 +328,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction): |
387 | 328 |
|
388 | 329 | async def run(self, args: argparse.Namespace): |
389 | 330 | from importlib.resources import read_text |
| 331 | + |
390 | 332 | from .. import resources |
391 | 333 |
|
392 | 334 | print(read_text(resources, resources.UDEV_RULES)) |
|
0 commit comments