micropython-stubber 1.20.0__py3-none-any.whl → 1.20.2__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 (60) hide show
  1. {micropython_stubber-1.20.0.dist-info → micropython_stubber-1.20.2.dist-info}/METADATA +6 -6
  2. {micropython_stubber-1.20.0.dist-info → micropython_stubber-1.20.2.dist-info}/RECORD +58 -51
  3. mpflash/README.md +54 -35
  4. mpflash/libusb_flash.ipynb +203 -203
  5. mpflash/mpflash/add_firmware.py +98 -0
  6. mpflash/mpflash/ask_input.py +106 -114
  7. mpflash/mpflash/cli_download.py +58 -37
  8. mpflash/mpflash/cli_flash.py +77 -35
  9. mpflash/mpflash/cli_group.py +14 -12
  10. mpflash/mpflash/cli_list.py +40 -4
  11. mpflash/mpflash/cli_main.py +20 -8
  12. mpflash/mpflash/common.py +125 -12
  13. mpflash/mpflash/config.py +2 -0
  14. mpflash/mpflash/connected.py +74 -0
  15. mpflash/mpflash/download.py +67 -50
  16. mpflash/mpflash/downloaded.py +9 -9
  17. mpflash/mpflash/flash.py +2 -2
  18. mpflash/mpflash/flash_esp.py +2 -2
  19. mpflash/mpflash/flash_uf2.py +16 -8
  20. mpflash/mpflash/flash_uf2_linux.py +5 -16
  21. mpflash/mpflash/flash_uf2_macos.py +78 -0
  22. mpflash/mpflash/flash_uf2_windows.py +1 -1
  23. mpflash/mpflash/list.py +58 -57
  24. mpflash/mpflash/mpboard_id/__init__.py +37 -44
  25. mpflash/mpflash/mpboard_id/add_boards.py +255 -0
  26. mpflash/mpflash/mpboard_id/board.py +37 -0
  27. mpflash/mpflash/mpboard_id/board_id.py +50 -43
  28. mpflash/mpflash/mpboard_id/board_info.zip +0 -0
  29. mpflash/mpflash/mpboard_id/store.py +42 -0
  30. mpflash/mpflash/mpremoteboard/__init__.py +18 -6
  31. mpflash/mpflash/mpremoteboard/runner.py +12 -12
  32. mpflash/mpflash/uf2disk.py +12 -0
  33. mpflash/mpflash/vendor/basicgit.py +288 -0
  34. mpflash/mpflash/vendor/dfu.py +1 -0
  35. mpflash/mpflash/vendor/versions.py +7 -3
  36. mpflash/mpflash/worklist.py +71 -48
  37. mpflash/poetry.lock +163 -137
  38. mpflash/pyproject.toml +18 -15
  39. stubber/__init__.py +1 -1
  40. stubber/board/createstubs.py +4 -3
  41. stubber/board/createstubs_db.py +5 -7
  42. stubber/board/createstubs_db_min.py +1 -1
  43. stubber/board/createstubs_db_mpy.mpy +0 -0
  44. stubber/board/createstubs_mem.py +6 -7
  45. stubber/board/createstubs_mem_min.py +1 -1
  46. stubber/board/createstubs_mem_mpy.mpy +0 -0
  47. stubber/board/createstubs_min.py +2 -2
  48. stubber/board/createstubs_mpy.mpy +0 -0
  49. stubber/board/modulelist.txt +1 -0
  50. stubber/commands/get_core_cmd.py +7 -6
  51. stubber/commands/get_docstubs_cmd.py +8 -3
  52. stubber/commands/get_frozen_cmd.py +5 -2
  53. stubber/publish/publish.py +18 -7
  54. stubber/utils/makeversionhdr.py +3 -2
  55. stubber/utils/versions.py +2 -1
  56. mpflash/mpflash/mpboard_id/board_info.csv +0 -2213
  57. mpflash/mpflash/mpboard_id/board_info.json +0 -19910
  58. {micropython_stubber-1.20.0.dist-info → micropython_stubber-1.20.2.dist-info}/LICENSE +0 -0
  59. {micropython_stubber-1.20.0.dist-info → micropython_stubber-1.20.2.dist-info}/WHEEL +0 -0
  60. {micropython_stubber-1.20.0.dist-info → micropython_stubber-1.20.2.dist-info}/entry_points.txt +0 -0
@@ -1,203 +1,203 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 3,
6
- "metadata": {},
7
- "outputs": [
8
- {
9
- "data": {
10
- "text/plain": [
11
- "[<DEVICE ID 0a05:7211 on Bus 002 Address 054>,\n",
12
- " <DEVICE ID 0a05:7211 on Bus 002 Address 032>,\n",
13
- " <DEVICE ID 045e:0c1e on Bus 002 Address 002>,\n",
14
- " <DEVICE ID 045e:07b2 on Bus 002 Address 028>,\n",
15
- " <DEVICE ID 045e:0904 on Bus 002 Address 015>,\n",
16
- " <DEVICE ID 043e:9a39 on Bus 002 Address 057>,\n",
17
- " <DEVICE ID 045e:0901 on Bus 002 Address 006>,\n",
18
- " <DEVICE ID 05e3:0610 on Bus 002 Address 010>,\n",
19
- " <DEVICE ID 2109:0817 on Bus 001 Address 001>,\n",
20
- " <DEVICE ID 045e:0902 on Bus 002 Address 008>,\n",
21
- " <DEVICE ID 1000:2000 on Bus 002 Address 016>,\n",
22
- " <DEVICE ID 1189:8890 on Bus 002 Address 027>,\n",
23
- " <DEVICE ID 2109:0817 on Bus 001 Address 002>,\n",
24
- " <DEVICE ID 2109:2812 on Bus 002 Address 025>,\n",
25
- " <DEVICE ID 046d:085e on Bus 002 Address 017>,\n",
26
- " <DEVICE ID 045e:07c6 on Bus 002 Address 056>,\n",
27
- " <DEVICE ID 0bda:5401 on Bus 002 Address 009>,\n",
28
- " <DEVICE ID 10c4:ea60 on Bus 002 Address 055>,\n",
29
- " <DEVICE ID 2109:2817 on Bus 002 Address 004>,\n",
30
- " <DEVICE ID 0bda:5411 on Bus 002 Address 005>,\n",
31
- " <DEVICE ID 045e:0900 on Bus 002 Address 003>,\n",
32
- " <DEVICE ID 04e8:61f5 on Bus 002 Address 023>,\n",
33
- " <DEVICE ID 045e:0903 on Bus 002 Address 020>,\n",
34
- " <DEVICE ID 2109:2812 on Bus 002 Address 026>,\n",
35
- " <DEVICE ID 8086:a0ed on Bus 002 Address 000>,\n",
36
- " <DEVICE ID 2109:2817 on Bus 002 Address 013>,\n",
37
- " <DEVICE ID 8087:0029 on Bus 002 Address 001>,\n",
38
- " <DEVICE ID 8086:9a13 on Bus 001 Address 000>]"
39
- ]
40
- },
41
- "execution_count": 3,
42
- "metadata": {},
43
- "output_type": "execute_result"
44
- }
45
- ],
46
- "source": [
47
- "import usb.core\n",
48
- "import usb.util\n",
49
- "import usb.backend.libusb1 as libusb1\n",
50
- "from usb.core import USBError, Device\n",
51
- "\n",
52
- "from pathlib import Path\n",
53
- "import platform\n",
54
- "\n",
55
- "if platform.system() == \"Windows\":\n",
56
- " # on windows you need to use the libusb1 backend\n",
57
- " import libusb\n",
58
- "\n",
59
- " arch = \"x64\" if platform.architecture()[0] == \"64bit\" else \"x86\"\n",
60
- " libusb1_dll = Path(libusb.__file__).parent / f\"_platform\\\\_windows\\\\{arch}\\\\libusb-1.0.dll\"\n",
61
- "\n",
62
- " backend = libusb1.get_backend(find_library=lambda x: libusb1_dll.as_posix())\n",
63
- "usb_devices = usb.core.find(backend=backend, find_all=True)\n",
64
- "\n",
65
- "list(usb_devices)"
66
- ]
67
- },
68
- {
69
- "cell_type": "code",
70
- "execution_count": null,
71
- "metadata": {},
72
- "outputs": [],
73
- "source": [
74
- "for d in usb.core.find(backend=backend, find_all=True):\n",
75
- " print(f\"Device {d.idVendor:04x}:{d.idProduct:04x}\")\n",
76
- " print(f\"{d.iManufacturer=}\")\n",
77
- " print(f\"{d.iProduct=}\")\n",
78
- " print(f\"{d.bDeviceClass=}\")\n",
79
- " print(f\"{d.bDescriptorType=}\")\n",
80
- " print(f\"{d.bcdDevice=}\")\n",
81
- " print(f\"{d.bcdUSB=}\")\n",
82
- " # print(dir(d))\n",
83
- " # print(f\" Manufacturer: {usb.util.get_string(dev, dev.iManufacturer)}\")\n",
84
- "\n",
85
- " print()"
86
- ]
87
- },
88
- {
89
- "cell_type": "code",
90
- "execution_count": null,
91
- "metadata": {},
92
- "outputs": [],
93
- "source": [
94
- "import serial.tools.list_ports\n",
95
- "\n",
96
- "ports = serial.tools.list_ports.comports()\n",
97
- "\n",
98
- "for port in ports:\n",
99
- " print(f\"Port: {port.device}\")\n",
100
- " print(f\"Description: {port.description}\")\n",
101
- " print(f\"Hardware ID: {port.hwid}\")\n",
102
- " print()"
103
- ]
104
- },
105
- {
106
- "cell_type": "code",
107
- "execution_count": null,
108
- "metadata": {},
109
- "outputs": [],
110
- "source": [
111
- "from mpflash.vendor import pydfu as pydfu\n",
112
- "\n",
113
- "try:\n",
114
- " pydfu.list_dfu_devices()\n",
115
- "except SystemExit:\n",
116
- " print(\"No DFU devices found\")"
117
- ]
118
- },
119
- {
120
- "cell_type": "code",
121
- "execution_count": null,
122
- "metadata": {},
123
- "outputs": [],
124
- "source": [
125
- "pydfu.init()"
126
- ]
127
- },
128
- {
129
- "cell_type": "code",
130
- "execution_count": null,
131
- "metadata": {},
132
- "outputs": [],
133
- "source": [
134
- "dfu_file = Path(\"C:\\\\Users\\\\josverl\\\\Downloads\\\\firmware\\\\stm32\\\\PYBV11-THREAD-v1.23.0-preview.203.dfu\")\n",
135
- "\n",
136
- "print(\"Read DFU file...\")\n",
137
- "elements = pydfu.read_dfu_file(dfu_file)\n",
138
- "if not elements:\n",
139
- " print(\"No data in dfu file\")"
140
- ]
141
- },
142
- {
143
- "cell_type": "code",
144
- "execution_count": 1,
145
- "metadata": {},
146
- "outputs": [
147
- {
148
- "name": "stdout",
149
- "output_type": "stream",
150
- "text": [
151
- "Writing memory...\n"
152
- ]
153
- },
154
- {
155
- "ename": "NameError",
156
- "evalue": "name 'pydfu' is not defined",
157
- "output_type": "error",
158
- "traceback": [
159
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
160
- "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
161
- "Cell \u001b[1;32mIn[1], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWriting memory...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m \u001b[43mpydfu\u001b[49m\u001b[38;5;241m.\u001b[39mwrite_elements(elements, \u001b[38;5;28;01mFalse\u001b[39;00m, progress\u001b[38;5;241m=\u001b[39mpydfu\u001b[38;5;241m.\u001b[39mcli_progress)\n",
162
- "\u001b[1;31mNameError\u001b[0m: name 'pydfu' is not defined"
163
- ]
164
- }
165
- ],
166
- "source": [
167
- "print(\"Writing memory...\")\n",
168
- "pydfu.write_elements(elements, False, progress=pydfu.cli_progress)"
169
- ]
170
- },
171
- {
172
- "cell_type": "code",
173
- "execution_count": null,
174
- "metadata": {},
175
- "outputs": [],
176
- "source": [
177
- "print(\"Exiting DFU...\")\n",
178
- "pydfu.exit_dfu()"
179
- ]
180
- }
181
- ],
182
- "metadata": {
183
- "kernelspec": {
184
- "display_name": ".venv",
185
- "language": "python",
186
- "name": "python3"
187
- },
188
- "language_info": {
189
- "codemirror_mode": {
190
- "name": "ipython",
191
- "version": 3
192
- },
193
- "file_extension": ".py",
194
- "mimetype": "text/x-python",
195
- "name": "python",
196
- "nbconvert_exporter": "python",
197
- "pygments_lexer": "ipython3",
198
- "version": "3.11.7"
199
- }
200
- },
201
- "nbformat": 4,
202
- "nbformat_minor": 2
203
- }
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "data": {
10
+ "text/plain": [
11
+ "[<DEVICE ID 0a05:7211 on Bus 002 Address 054>,\n",
12
+ " <DEVICE ID 0a05:7211 on Bus 002 Address 032>,\n",
13
+ " <DEVICE ID 045e:0c1e on Bus 002 Address 002>,\n",
14
+ " <DEVICE ID 045e:07b2 on Bus 002 Address 028>,\n",
15
+ " <DEVICE ID 045e:0904 on Bus 002 Address 015>,\n",
16
+ " <DEVICE ID 043e:9a39 on Bus 002 Address 057>,\n",
17
+ " <DEVICE ID 045e:0901 on Bus 002 Address 006>,\n",
18
+ " <DEVICE ID 05e3:0610 on Bus 002 Address 010>,\n",
19
+ " <DEVICE ID 2109:0817 on Bus 001 Address 001>,\n",
20
+ " <DEVICE ID 045e:0902 on Bus 002 Address 008>,\n",
21
+ " <DEVICE ID 1000:2000 on Bus 002 Address 016>,\n",
22
+ " <DEVICE ID 1189:8890 on Bus 002 Address 027>,\n",
23
+ " <DEVICE ID 2109:0817 on Bus 001 Address 002>,\n",
24
+ " <DEVICE ID 2109:2812 on Bus 002 Address 025>,\n",
25
+ " <DEVICE ID 046d:085e on Bus 002 Address 017>,\n",
26
+ " <DEVICE ID 045e:07c6 on Bus 002 Address 056>,\n",
27
+ " <DEVICE ID 0bda:5401 on Bus 002 Address 009>,\n",
28
+ " <DEVICE ID 10c4:ea60 on Bus 002 Address 055>,\n",
29
+ " <DEVICE ID 2109:2817 on Bus 002 Address 004>,\n",
30
+ " <DEVICE ID 0bda:5411 on Bus 002 Address 005>,\n",
31
+ " <DEVICE ID 045e:0900 on Bus 002 Address 003>,\n",
32
+ " <DEVICE ID 04e8:61f5 on Bus 002 Address 023>,\n",
33
+ " <DEVICE ID 045e:0903 on Bus 002 Address 020>,\n",
34
+ " <DEVICE ID 2109:2812 on Bus 002 Address 026>,\n",
35
+ " <DEVICE ID 8086:a0ed on Bus 002 Address 000>,\n",
36
+ " <DEVICE ID 2109:2817 on Bus 002 Address 013>,\n",
37
+ " <DEVICE ID 8087:0029 on Bus 002 Address 001>,\n",
38
+ " <DEVICE ID 8086:9a13 on Bus 001 Address 000>]"
39
+ ]
40
+ },
41
+ "execution_count": 3,
42
+ "metadata": {},
43
+ "output_type": "execute_result"
44
+ }
45
+ ],
46
+ "source": [
47
+ "import usb.core\n",
48
+ "import usb.util\n",
49
+ "import usb.backend.libusb1 as libusb1\n",
50
+ "from usb.core import USBError, Device\n",
51
+ "\n",
52
+ "from pathlib import Path\n",
53
+ "import platform\n",
54
+ "\n",
55
+ "if platform.system() == \"Windows\":\n",
56
+ " # on windows you need to use the libusb1 backend\n",
57
+ " import libusb\n",
58
+ "\n",
59
+ " arch = \"x64\" if platform.architecture()[0] == \"64bit\" else \"x86\"\n",
60
+ " libusb1_dll = Path(libusb.__file__).parent / f\"_platform\\\\_windows\\\\{arch}\\\\libusb-1.0.dll\"\n",
61
+ "\n",
62
+ " backend = libusb1.get_backend(find_library=lambda x: libusb1_dll.as_posix())\n",
63
+ "usb_devices = usb.core.find(backend=backend, find_all=True)\n",
64
+ "\n",
65
+ "list(usb_devices)"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": null,
71
+ "metadata": {},
72
+ "outputs": [],
73
+ "source": [
74
+ "for d in usb.core.find(backend=backend, find_all=True):\n",
75
+ " print(f\"Device {d.idVendor:04x}:{d.idProduct:04x}\")\n",
76
+ " print(f\"{d.iManufacturer=}\")\n",
77
+ " print(f\"{d.iProduct=}\")\n",
78
+ " print(f\"{d.bDeviceClass=}\")\n",
79
+ " print(f\"{d.bDescriptorType=}\")\n",
80
+ " print(f\"{d.bcdDevice=}\")\n",
81
+ " print(f\"{d.bcdUSB=}\")\n",
82
+ " # print(dir(d))\n",
83
+ " # print(f\" Manufacturer: {usb.util.get_string(dev, dev.iManufacturer)}\")\n",
84
+ "\n",
85
+ " print()"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": null,
91
+ "metadata": {},
92
+ "outputs": [],
93
+ "source": [
94
+ "import serial.tools.list_ports\n",
95
+ "\n",
96
+ "ports = serial.tools.list_ports.comports()\n",
97
+ "\n",
98
+ "for port in ports:\n",
99
+ " print(f\"Port: {port.device}\")\n",
100
+ " print(f\"Description: {port.description}\")\n",
101
+ " print(f\"Hardware ID: {port.hwid}\")\n",
102
+ " print()"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "metadata": {},
109
+ "outputs": [],
110
+ "source": [
111
+ "from mpflash.vendor import pydfu as pydfu\n",
112
+ "\n",
113
+ "try:\n",
114
+ " pydfu.list_dfu_devices()\n",
115
+ "except SystemExit:\n",
116
+ " print(\"No DFU devices found\")"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "metadata": {},
123
+ "outputs": [],
124
+ "source": [
125
+ "pydfu.init()"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": null,
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "dfu_file = Path(\"C:\\\\Users\\\\josverl\\\\Downloads\\\\firmware\\\\stm32\\\\PYBV11-THREAD-v1.23.0-preview.203.dfu\")\n",
135
+ "\n",
136
+ "print(\"Read DFU file...\")\n",
137
+ "elements = pydfu.read_dfu_file(dfu_file)\n",
138
+ "if not elements:\n",
139
+ " print(\"No data in dfu file\")"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 1,
145
+ "metadata": {},
146
+ "outputs": [
147
+ {
148
+ "name": "stdout",
149
+ "output_type": "stream",
150
+ "text": [
151
+ "Writing memory...\n"
152
+ ]
153
+ },
154
+ {
155
+ "ename": "NameError",
156
+ "evalue": "name 'pydfu' is not defined",
157
+ "output_type": "error",
158
+ "traceback": [
159
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
160
+ "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
161
+ "Cell \u001b[1;32mIn[1], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWriting memory...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m \u001b[43mpydfu\u001b[49m\u001b[38;5;241m.\u001b[39mwrite_elements(elements, \u001b[38;5;28;01mFalse\u001b[39;00m, progress\u001b[38;5;241m=\u001b[39mpydfu\u001b[38;5;241m.\u001b[39mcli_progress)\n",
162
+ "\u001b[1;31mNameError\u001b[0m: name 'pydfu' is not defined"
163
+ ]
164
+ }
165
+ ],
166
+ "source": [
167
+ "print(\"Writing memory...\")\n",
168
+ "pydfu.write_elements(elements, False, progress=pydfu.cli_progress)"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": null,
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "print(\"Exiting DFU...\")\n",
178
+ "pydfu.exit_dfu()"
179
+ ]
180
+ }
181
+ ],
182
+ "metadata": {
183
+ "kernelspec": {
184
+ "display_name": ".venv",
185
+ "language": "python",
186
+ "name": "python3"
187
+ },
188
+ "language_info": {
189
+ "codemirror_mode": {
190
+ "name": "ipython",
191
+ "version": 3
192
+ },
193
+ "file_extension": ".py",
194
+ "mimetype": "text/x-python",
195
+ "name": "python",
196
+ "nbconvert_exporter": "python",
197
+ "pygments_lexer": "ipython3",
198
+ "version": "3.11.7"
199
+ }
200
+ },
201
+ "nbformat": 4,
202
+ "nbformat_minor": 2
203
+ }
@@ -0,0 +1,98 @@
1
+ import shutil
2
+ from pathlib import Path
3
+ from typing import Union
4
+
5
+ import jsonlines
6
+ import requests
7
+ from loguru import logger as log
8
+
9
+ # re-use logic from mpremote
10
+ from mpremote.mip import _rewrite_url as rewrite_url # type: ignore
11
+
12
+ from mpflash.common import FWInfo
13
+ from mpflash.config import config
14
+ from mpflash.vendor.versions import get_preview_mp_version, get_stable_mp_version
15
+
16
+
17
+ def add_firmware(
18
+ source: Union[Path, str],
19
+ new_fw: FWInfo,
20
+ *,
21
+ force: bool = False,
22
+ custom: bool = False,
23
+ description: str = "",
24
+ ) -> bool:
25
+ """Add a firmware to the firmware folder.
26
+
27
+ stored in the port folder, with the same filename as the source.
28
+
29
+ """
30
+ # Check minimal info needed
31
+ if not new_fw.port or not new_fw.board:
32
+ log.error("Port and board are required")
33
+ return False
34
+ if not isinstance(source, Path) and not source.startswith("http"):
35
+ log.error(f"Invalid source {source}")
36
+ return False
37
+
38
+ # use sensible defaults
39
+ source_2 = Path(source)
40
+ new_fw.ext = new_fw.ext or source_2.suffix
41
+ new_fw.variant = new_fw.variant or new_fw.board
42
+ new_fw.custom = new_fw.custom or custom
43
+ new_fw.description = new_fw.description or description
44
+ if not new_fw.version:
45
+ # TODO: Get version from filename
46
+ # or use the last preview version
47
+ new_fw.version = get_preview_mp_version() if new_fw.preview else get_stable_mp_version()
48
+
49
+ config.firmware_folder.mkdir(exist_ok=True)
50
+
51
+ fw_filename = config.firmware_folder / new_fw.port / source_2.name
52
+
53
+ new_fw.filename = str(fw_filename.relative_to(config.firmware_folder))
54
+ new_fw.firmware = source.as_uri() if isinstance(source, Path) else source
55
+
56
+ if not copy_firmware(source, fw_filename, force):
57
+ log.error(f"Failed to copy {source} to {fw_filename}")
58
+ return False
59
+ # add to inventory
60
+ with jsonlines.open(config.firmware_folder / "firmware.jsonl", "a") as writer:
61
+ log.info(f"Adding {new_fw.port} {new_fw.board}")
62
+ log.info(f" to {fw_filename}")
63
+
64
+ writer.write(new_fw.to_dict())
65
+ return True
66
+
67
+
68
+ def copy_firmware(source: Union[Path, str], fw_filename: Path, force: bool = False):
69
+ """Add a firmware to the firmware folder.
70
+ stored in the port folder, with the same filename as the source.
71
+ """
72
+ if fw_filename.exists() and not force:
73
+ log.error(f" {fw_filename} already exists. Use --force to overwrite")
74
+ return False
75
+ fw_filename.parent.mkdir(exist_ok=True)
76
+ if isinstance(source, Path):
77
+ if not source.exists():
78
+ log.error(f"File {source} does not exist")
79
+ return False
80
+ # file copy
81
+ log.debug(f"Copy {source} to {fw_filename}")
82
+ shutil.copy(source, fw_filename)
83
+ return True
84
+ # handle github urls
85
+ url = rewrite_url(source)
86
+ if str(source).startswith("http://") or str(source).startswith("https://"):
87
+ log.debug(f"Download {url} to {fw_filename}")
88
+ response = requests.get(url)
89
+
90
+ if response.status_code == 200:
91
+ with open(fw_filename, "wb") as file:
92
+ file.write(response.content)
93
+ log.info("File downloaded and saved successfully.")
94
+ return True
95
+ else:
96
+ print("Failed to download the file.")
97
+ return False
98
+ return False