mpflash 0.8.5__py3-none-any.whl → 0.8.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. mpflash/add_firmware.py +98 -98
  2. mpflash/ask_input.py +236 -236
  3. mpflash/bootloader/__init__.py +37 -36
  4. mpflash/bootloader/manual.py +102 -102
  5. mpflash/bootloader/micropython.py +10 -10
  6. mpflash/bootloader/touch1200.py +45 -45
  7. mpflash/cli_download.py +129 -129
  8. mpflash/cli_flash.py +219 -219
  9. mpflash/cli_group.py +98 -98
  10. mpflash/cli_list.py +81 -81
  11. mpflash/cli_main.py +41 -41
  12. mpflash/common.py +164 -164
  13. mpflash/config.py +47 -47
  14. mpflash/connected.py +74 -74
  15. mpflash/download.py +360 -360
  16. mpflash/downloaded.py +129 -129
  17. mpflash/errors.py +9 -9
  18. mpflash/flash.py +52 -52
  19. mpflash/flash_esp.py +59 -59
  20. mpflash/flash_stm32.py +24 -24
  21. mpflash/flash_stm32_cube.py +111 -111
  22. mpflash/flash_stm32_dfu.py +101 -101
  23. mpflash/flash_uf2.py +67 -67
  24. mpflash/flash_uf2_boardid.py +15 -15
  25. mpflash/flash_uf2_linux.py +123 -123
  26. mpflash/flash_uf2_macos.py +34 -34
  27. mpflash/flash_uf2_windows.py +34 -34
  28. mpflash/list.py +89 -89
  29. mpflash/logger.py +41 -41
  30. mpflash/mpboard_id/__init__.py +93 -93
  31. mpflash/mpboard_id/add_boards.py +255 -255
  32. mpflash/mpboard_id/board.py +37 -37
  33. mpflash/mpboard_id/board_id.py +86 -86
  34. mpflash/mpboard_id/store.py +43 -43
  35. mpflash/mpremoteboard/__init__.py +221 -221
  36. mpflash/mpremoteboard/mpy_fw_info.py +141 -141
  37. mpflash/mpremoteboard/runner.py +140 -140
  38. mpflash/uf2disk.py +12 -12
  39. mpflash/vendor/basicgit.py +288 -288
  40. mpflash/vendor/click_aliases.py +91 -91
  41. mpflash/vendor/dfu.py +165 -165
  42. mpflash/vendor/pydfu.py +605 -605
  43. mpflash/vendor/readme.md +2 -2
  44. mpflash/vendor/versions.py +119 -117
  45. mpflash/worklist.py +170 -170
  46. {mpflash-0.8.5.dist-info → mpflash-0.8.7.dist-info}/LICENSE +20 -20
  47. {mpflash-0.8.5.dist-info → mpflash-0.8.7.dist-info}/METADATA +1 -1
  48. mpflash-0.8.7.dist-info/RECORD +52 -0
  49. mpflash-0.8.5.dist-info/RECORD +0 -52
  50. {mpflash-0.8.5.dist-info → mpflash-0.8.7.dist-info}/WHEEL +0 -0
  51. {mpflash-0.8.5.dist-info → mpflash-0.8.7.dist-info}/entry_points.txt +0 -0
@@ -1,221 +1,221 @@
1
- """
2
- Module to run mpremote commands, and retry on failure or timeout
3
- """
4
-
5
- import sys
6
- import time
7
- from pathlib import Path
8
- from typing import List, Optional, Union
9
-
10
- import serial.tools.list_ports
11
- from loguru import logger as log
12
- from rich.progress import track
13
- from tenacity import retry, stop_after_attempt, wait_fixed
14
-
15
- from mpflash.errors import MPFlashError
16
- from mpflash.mpboard_id.board_id import find_board_id_by_description
17
- from mpflash.mpremoteboard.runner import run
18
-
19
- ###############################################################################################
20
- # TODO : make this a bit nicer
21
- HERE = Path(__file__).parent
22
-
23
- OK = 0
24
- ERROR = -1
25
- RETRIES = 3
26
- ###############################################################################################
27
-
28
-
29
- class MPRemoteBoard:
30
- """Class to run mpremote commands"""
31
-
32
- def __init__(self, serialport: str = "", update: bool = False):
33
- """
34
- Initialize MPRemoteBoard object.
35
-
36
- Parameters:
37
- - serialport (str): The serial port to connect to. Default is an empty string.
38
- - update (bool): Whether to update the MCU information. Default is False.
39
- """
40
- self.serialport = serialport
41
- self.firmware = {}
42
-
43
- self.connected = False
44
- self.path: Optional[Path] = None
45
- self.family = "unknown"
46
- self.description = ""
47
- self.version = ""
48
- self.port = ""
49
- self.board = ""
50
- self.cpu = ""
51
- self.arch = ""
52
- self.mpy = ""
53
- self.build = ""
54
- if update:
55
- self.get_mcu_info()
56
-
57
- def __str__(self):
58
- """
59
- Return a string representation of the MPRemoteBoard object.
60
-
61
- Returns:
62
- - str: The string representation of the object.
63
- """
64
- return f"MPRemoteBoard({self.serialport}, {self.family} {self.port}, {self.board}, {self.version})"
65
-
66
- @staticmethod
67
- def connected_boards(bluetooth: bool = False, description: bool = False) -> List[str]:
68
- # TODO: rename to connected_comports
69
- """
70
- Get a list of connected comports.
71
-
72
- Parameters:
73
- - bluetooth (bool): Whether to include Bluetooth ports. Default is False.
74
-
75
- Returns:
76
- - List[str]: A list of connected board ports.
77
- """
78
- comports = serial.tools.list_ports.comports()
79
-
80
- if not bluetooth:
81
- # filter out bluetooth ports
82
- comports = [p for p in comports if "bluetooth" not in p.description.lower()]
83
- comports = [p for p in comports if "BTHENUM" not in p.hwid]
84
- if description:
85
- output = [
86
- f"{p.device} {(p.manufacturer + ' ') if p.manufacturer and not p.description.startswith(p.manufacturer) else ''}{p.description}"
87
- for p in comports
88
- ]
89
- else:
90
- output = [p.device for p in comports]
91
-
92
- if sys.platform == "win32":
93
- # Windows sort of comports by number - but fallback to device name
94
- return sorted(output, key=lambda x: int(x.split()[0][3:]) if x.split()[0][3:].isdigit() else x)
95
- # sort by device name
96
- return sorted(output)
97
-
98
- @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(1), reraise=True) # type: ignore ## retry_error_cls=ConnectionError,
99
- def get_mcu_info(self, timeout: int = 2):
100
- """
101
- Get MCU information from the connected board.
102
-
103
- Parameters:
104
- - timeout (int): The timeout value in seconds. Default is 2.
105
-
106
- Raises:
107
- - ConnectionError: If failed to get mcu_info for the serial port.
108
- """
109
- rc, result = self.run_command(
110
- ["run", str(HERE / "mpy_fw_info.py")],
111
- no_info=True,
112
- timeout=timeout,
113
- )
114
- if rc != OK:
115
- raise ConnectionError(f"Failed to get mcu_info for {self.serialport}")
116
- # Ok we have the info, now parse it
117
- s = result[0].strip()
118
- if s.startswith("{") and s.endswith("}"):
119
- info = eval(s)
120
- self.family = info["family"]
121
- self.version = info["version"]
122
- self.build = info["build"]
123
- self.port = info["port"]
124
- self.cpu = info["cpu"]
125
- self.arch = info["arch"]
126
- self.mpy = info["mpy"]
127
- self.description = descr = info["board"]
128
- pos = descr.rfind(" with")
129
- short_descr = descr[:pos].strip() if pos != -1 else ""
130
- if board_name := find_board_id_by_description(descr, short_descr, version=self.version):
131
- self.board = board_name
132
- else:
133
- self.board = "UNKNOWN_BOARD"
134
-
135
- def disconnect(self) -> bool:
136
- """
137
- Disconnect from a board.
138
-
139
- Returns:
140
- - bool: True if successfully disconnected, False otherwise.
141
- """
142
- if not self.connected:
143
- return True
144
- if not self.serialport:
145
- log.error("No port connected")
146
- self.connected = False
147
- return False
148
- log.info(f"Disconnecting from {self.serialport}")
149
- result = self.run_command(["disconnect"])[0] == OK
150
- self.connected = False
151
- return result
152
-
153
- @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(2), reraise=True)
154
- def run_command(
155
- self,
156
- cmd: Union[str, List[str]],
157
- *,
158
- log_errors: bool = True,
159
- no_info: bool = False,
160
- timeout: int = 60,
161
- **kwargs,
162
- ):
163
- """
164
- Run mpremote with the given command.
165
-
166
- Parameters:
167
- - cmd (Union[str, List[str]]): The command to run, either a string or a list of strings.
168
- - log_errors (bool): Whether to log errors. Default is True.
169
- - no_info (bool): Whether to skip printing info. Default is False.
170
- - timeout (int): The timeout value in seconds. Default is 60.
171
-
172
- Returns:
173
- - bool: True if the command succeeded, False otherwise.
174
- """
175
- if isinstance(cmd, str):
176
- cmd = cmd.split(" ")
177
- prefix = [sys.executable, "-m", "mpremote"]
178
- if self.serialport:
179
- prefix += ["connect", self.serialport]
180
- # if connected add resume to keep state between commands
181
- if self.connected:
182
- prefix += ["resume"]
183
- cmd = prefix + cmd
184
- log.debug(" ".join(cmd))
185
- result = run(cmd, timeout, log_errors, no_info, **kwargs)
186
- self.connected = result[0] == OK
187
- return result
188
-
189
- @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(1))
190
- def mip_install(self, name: str) -> bool:
191
- """
192
- Install a micropython package.
193
-
194
- Parameters:
195
- - name (str): The name of the package to install.
196
-
197
- Returns:
198
- - bool: True if the installation succeeded, False otherwise.
199
- """
200
- # install createstubs to the board
201
- cmd = ["mip", "install", name]
202
- result = self.run_command(cmd)[0] == OK
203
- self.connected = True
204
- return result
205
-
206
- def wait_for_restart(self, timeout: int = 10):
207
- """wait for the board to restart"""
208
- for _ in track(
209
- range(timeout),
210
- description="Waiting for the board to restart",
211
- transient=True,
212
- get_time=lambda: time.time(),
213
- show_speed=False,
214
- refresh_per_second=1,
215
- ):
216
- time.sleep(1)
217
- try:
218
- self.get_mcu_info()
219
- break
220
- except (ConnectionError, MPFlashError):
221
- pass
1
+ """
2
+ Module to run mpremote commands, and retry on failure or timeout
3
+ """
4
+
5
+ import sys
6
+ import time
7
+ from pathlib import Path
8
+ from typing import List, Optional, Union
9
+
10
+ import serial.tools.list_ports
11
+ from loguru import logger as log
12
+ from rich.progress import track
13
+ from tenacity import retry, stop_after_attempt, wait_fixed
14
+
15
+ from mpflash.errors import MPFlashError
16
+ from mpflash.mpboard_id.board_id import find_board_id_by_description
17
+ from mpflash.mpremoteboard.runner import run
18
+
19
+ ###############################################################################################
20
+ # TODO : make this a bit nicer
21
+ HERE = Path(__file__).parent
22
+
23
+ OK = 0
24
+ ERROR = -1
25
+ RETRIES = 3
26
+ ###############################################################################################
27
+
28
+
29
+ class MPRemoteBoard:
30
+ """Class to run mpremote commands"""
31
+
32
+ def __init__(self, serialport: str = "", update: bool = False):
33
+ """
34
+ Initialize MPRemoteBoard object.
35
+
36
+ Parameters:
37
+ - serialport (str): The serial port to connect to. Default is an empty string.
38
+ - update (bool): Whether to update the MCU information. Default is False.
39
+ """
40
+ self.serialport = serialport
41
+ self.firmware = {}
42
+
43
+ self.connected = False
44
+ self.path: Optional[Path] = None
45
+ self.family = "unknown"
46
+ self.description = ""
47
+ self.version = ""
48
+ self.port = ""
49
+ self.board = ""
50
+ self.cpu = ""
51
+ self.arch = ""
52
+ self.mpy = ""
53
+ self.build = ""
54
+ if update:
55
+ self.get_mcu_info()
56
+
57
+ def __str__(self):
58
+ """
59
+ Return a string representation of the MPRemoteBoard object.
60
+
61
+ Returns:
62
+ - str: The string representation of the object.
63
+ """
64
+ return f"MPRemoteBoard({self.serialport}, {self.family} {self.port}, {self.board}, {self.version})"
65
+
66
+ @staticmethod
67
+ def connected_boards(bluetooth: bool = False, description: bool = False) -> List[str]:
68
+ # TODO: rename to connected_comports
69
+ """
70
+ Get a list of connected comports.
71
+
72
+ Parameters:
73
+ - bluetooth (bool): Whether to include Bluetooth ports. Default is False.
74
+
75
+ Returns:
76
+ - List[str]: A list of connected board ports.
77
+ """
78
+ comports = serial.tools.list_ports.comports()
79
+
80
+ if not bluetooth:
81
+ # filter out bluetooth ports
82
+ comports = [p for p in comports if "bluetooth" not in p.description.lower()]
83
+ comports = [p for p in comports if "BTHENUM" not in p.hwid]
84
+ if description:
85
+ output = [
86
+ f"{p.device} {(p.manufacturer + ' ') if p.manufacturer and not p.description.startswith(p.manufacturer) else ''}{p.description}"
87
+ for p in comports
88
+ ]
89
+ else:
90
+ output = [p.device for p in comports]
91
+
92
+ if sys.platform == "win32":
93
+ # Windows sort of comports by number - but fallback to device name
94
+ return sorted(output, key=lambda x: int(x.split()[0][3:]) if x.split()[0][3:].isdigit() else x)
95
+ # sort by device name
96
+ return sorted(output)
97
+
98
+ @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(1), reraise=True) # type: ignore ## retry_error_cls=ConnectionError,
99
+ def get_mcu_info(self, timeout: int = 2):
100
+ """
101
+ Get MCU information from the connected board.
102
+
103
+ Parameters:
104
+ - timeout (int): The timeout value in seconds. Default is 2.
105
+
106
+ Raises:
107
+ - ConnectionError: If failed to get mcu_info for the serial port.
108
+ """
109
+ rc, result = self.run_command(
110
+ ["run", str(HERE / "mpy_fw_info.py")],
111
+ no_info=True,
112
+ timeout=timeout,
113
+ )
114
+ if rc != OK:
115
+ raise ConnectionError(f"Failed to get mcu_info for {self.serialport}")
116
+ # Ok we have the info, now parse it
117
+ s = result[0].strip()
118
+ if s.startswith("{") and s.endswith("}"):
119
+ info = eval(s)
120
+ self.family = info["family"]
121
+ self.version = info["version"]
122
+ self.build = info["build"]
123
+ self.port = info["port"]
124
+ self.cpu = info["cpu"]
125
+ self.arch = info["arch"]
126
+ self.mpy = info["mpy"]
127
+ self.description = descr = info["board"]
128
+ pos = descr.rfind(" with")
129
+ short_descr = descr[:pos].strip() if pos != -1 else ""
130
+ if board_name := find_board_id_by_description(descr, short_descr, version=self.version):
131
+ self.board = board_name
132
+ else:
133
+ self.board = "UNKNOWN_BOARD"
134
+
135
+ def disconnect(self) -> bool:
136
+ """
137
+ Disconnect from a board.
138
+
139
+ Returns:
140
+ - bool: True if successfully disconnected, False otherwise.
141
+ """
142
+ if not self.connected:
143
+ return True
144
+ if not self.serialport:
145
+ log.error("No port connected")
146
+ self.connected = False
147
+ return False
148
+ log.info(f"Disconnecting from {self.serialport}")
149
+ result = self.run_command(["disconnect"])[0] == OK
150
+ self.connected = False
151
+ return result
152
+
153
+ @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(2), reraise=True)
154
+ def run_command(
155
+ self,
156
+ cmd: Union[str, List[str]],
157
+ *,
158
+ log_errors: bool = True,
159
+ no_info: bool = False,
160
+ timeout: int = 60,
161
+ **kwargs,
162
+ ):
163
+ """
164
+ Run mpremote with the given command.
165
+
166
+ Parameters:
167
+ - cmd (Union[str, List[str]]): The command to run, either a string or a list of strings.
168
+ - log_errors (bool): Whether to log errors. Default is True.
169
+ - no_info (bool): Whether to skip printing info. Default is False.
170
+ - timeout (int): The timeout value in seconds. Default is 60.
171
+
172
+ Returns:
173
+ - bool: True if the command succeeded, False otherwise.
174
+ """
175
+ if isinstance(cmd, str):
176
+ cmd = cmd.split(" ")
177
+ prefix = [sys.executable, "-m", "mpremote"]
178
+ if self.serialport:
179
+ prefix += ["connect", self.serialport]
180
+ # if connected add resume to keep state between commands
181
+ if self.connected:
182
+ prefix += ["resume"]
183
+ cmd = prefix + cmd
184
+ log.debug(" ".join(cmd))
185
+ result = run(cmd, timeout, log_errors, no_info, **kwargs)
186
+ self.connected = result[0] == OK
187
+ return result
188
+
189
+ @retry(stop=stop_after_attempt(RETRIES), wait=wait_fixed(1))
190
+ def mip_install(self, name: str) -> bool:
191
+ """
192
+ Install a micropython package.
193
+
194
+ Parameters:
195
+ - name (str): The name of the package to install.
196
+
197
+ Returns:
198
+ - bool: True if the installation succeeded, False otherwise.
199
+ """
200
+ # install createstubs to the board
201
+ cmd = ["mip", "install", name]
202
+ result = self.run_command(cmd)[0] == OK
203
+ self.connected = True
204
+ return result
205
+
206
+ def wait_for_restart(self, timeout: int = 10):
207
+ """wait for the board to restart"""
208
+ for _ in track(
209
+ range(timeout),
210
+ description="Waiting for the board to restart",
211
+ transient=True,
212
+ get_time=lambda: time.time(),
213
+ show_speed=False,
214
+ refresh_per_second=1,
215
+ ):
216
+ time.sleep(1)
217
+ try:
218
+ self.get_mcu_info()
219
+ break
220
+ except (ConnectionError, MPFlashError):
221
+ pass