mpflash 1.0.1__py3-none-any.whl → 1.0.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.
- mpflash/add_firmware.py +98 -98
- mpflash/ask_input.py +236 -236
- mpflash/basicgit.py +284 -284
- mpflash/bootloader/__init__.py +2 -2
- mpflash/bootloader/activate.py +60 -60
- mpflash/bootloader/detect.py +82 -82
- mpflash/bootloader/manual.py +101 -101
- mpflash/bootloader/micropython.py +12 -12
- mpflash/bootloader/touch1200.py +36 -36
- mpflash/cli_download.py +129 -129
- mpflash/cli_flash.py +224 -216
- mpflash/cli_group.py +111 -111
- mpflash/cli_list.py +87 -87
- mpflash/cli_main.py +39 -39
- mpflash/common.py +210 -177
- mpflash/config.py +44 -44
- mpflash/connected.py +104 -77
- mpflash/download.py +364 -364
- mpflash/downloaded.py +130 -130
- mpflash/errors.py +9 -9
- mpflash/flash/__init__.py +55 -55
- mpflash/flash/esp.py +59 -59
- mpflash/flash/stm32.py +19 -19
- mpflash/flash/stm32_dfu.py +104 -104
- mpflash/flash/uf2/__init__.py +88 -88
- mpflash/flash/uf2/boardid.py +15 -15
- mpflash/flash/uf2/linux.py +136 -130
- mpflash/flash/uf2/macos.py +42 -42
- mpflash/flash/uf2/uf2disk.py +12 -12
- mpflash/flash/uf2/windows.py +43 -43
- mpflash/flash/worklist.py +170 -170
- mpflash/list.py +106 -106
- mpflash/logger.py +41 -41
- mpflash/mpboard_id/__init__.py +93 -93
- mpflash/mpboard_id/add_boards.py +251 -251
- mpflash/mpboard_id/board.py +37 -37
- mpflash/mpboard_id/board_id.py +86 -86
- mpflash/mpboard_id/store.py +43 -43
- mpflash/mpremoteboard/__init__.py +266 -266
- mpflash/mpremoteboard/mpy_fw_info.py +141 -141
- mpflash/mpremoteboard/runner.py +140 -140
- mpflash/vendor/click_aliases.py +91 -91
- mpflash/vendor/dfu.py +165 -165
- mpflash/vendor/pydfu.py +605 -605
- mpflash/vendor/readme.md +2 -2
- mpflash/versions.py +135 -135
- {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/LICENSE +20 -20
- {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/METADATA +1 -1
- mpflash-1.0.2.dist-info/RECORD +53 -0
- mpflash-1.0.1.dist-info/RECORD +0 -53
- {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/WHEEL +0 -0
- {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/entry_points.txt +0 -0
mpflash/cli_list.py
CHANGED
@@ -1,87 +1,87 @@
|
|
1
|
-
import json
|
2
|
-
from typing import List
|
3
|
-
|
4
|
-
import rich_click as click
|
5
|
-
from rich import print
|
6
|
-
|
7
|
-
from .cli_group import cli
|
8
|
-
from .connected import list_mcus
|
9
|
-
from .list import show_mcus
|
10
|
-
from .logger import make_quiet
|
11
|
-
|
12
|
-
|
13
|
-
@cli.command(
|
14
|
-
"list",
|
15
|
-
help="List the connected MCU boards. alias: devs",
|
16
|
-
aliases=["devs"],
|
17
|
-
)
|
18
|
-
@click.option(
|
19
|
-
"--json",
|
20
|
-
"-j",
|
21
|
-
"as_json",
|
22
|
-
is_flag=True,
|
23
|
-
default=False,
|
24
|
-
show_default=True,
|
25
|
-
help="""Output in json format""",
|
26
|
-
)
|
27
|
-
@click.option(
|
28
|
-
"--serial",
|
29
|
-
"--serial-port",
|
30
|
-
"-s",
|
31
|
-
"serial",
|
32
|
-
default=["*"],
|
33
|
-
multiple=True,
|
34
|
-
show_default=True,
|
35
|
-
help="Serial port(s) (or globs) to list. ",
|
36
|
-
metavar="SERIALPORT",
|
37
|
-
)
|
38
|
-
@click.option(
|
39
|
-
"--ignore",
|
40
|
-
"-i",
|
41
|
-
is_eager=True,
|
42
|
-
help="Serial port(s) (or globs) to ignore. Defaults to MPFLASH_IGNORE.",
|
43
|
-
multiple=True,
|
44
|
-
default=[],
|
45
|
-
envvar="MPFLASH_IGNORE",
|
46
|
-
show_default=True,
|
47
|
-
metavar="SERIALPORT",
|
48
|
-
)
|
49
|
-
@click.option(
|
50
|
-
"--bluetooth/--no-bluetooth",
|
51
|
-
"-b/-nb",
|
52
|
-
is_flag=True,
|
53
|
-
default=False,
|
54
|
-
show_default=True,
|
55
|
-
help="""Include bluetooth ports in the list""",
|
56
|
-
)
|
57
|
-
@click.option(
|
58
|
-
"--progress/--no-progress",
|
59
|
-
# "-p/-np", -p is already used for --port
|
60
|
-
"progress",
|
61
|
-
is_flag=True,
|
62
|
-
default=True,
|
63
|
-
show_default=True,
|
64
|
-
help="""Show progress""",
|
65
|
-
)
|
66
|
-
def cli_list_mcus(serial: List[str], ignore: List[str], bluetooth: bool, as_json: bool, progress: bool = True) -> int:
|
67
|
-
"""List the connected MCU boards, and output in a nice table or json."""
|
68
|
-
serial = list(serial)
|
69
|
-
ignore = list(ignore)
|
70
|
-
if as_json:
|
71
|
-
# avoid noise in json output
|
72
|
-
make_quiet()
|
73
|
-
# TODO? Ask user to select a serialport if [?] is given ?
|
74
|
-
|
75
|
-
conn_mcus = list_mcus(ignore=ignore, include=serial, bluetooth=bluetooth)
|
76
|
-
# ignore boards that have the [micropython-stubber] ignore flag set
|
77
|
-
conn_mcus = [item for item in conn_mcus if not (item.toml.get("mpflash", {}).get("ignore", False))]
|
78
|
-
if as_json:
|
79
|
-
# remove the path and firmware attibutes from the json output as they are always empty
|
80
|
-
for mcu in conn_mcus:
|
81
|
-
del mcu.path
|
82
|
-
del mcu.firmware
|
83
|
-
print(json.dumps([mcu.__dict__ for mcu in conn_mcus], indent=4))
|
84
|
-
progress = False
|
85
|
-
if progress:
|
86
|
-
show_mcus(conn_mcus, refresh=False)
|
87
|
-
return 0 if conn_mcus else 1
|
1
|
+
import json
|
2
|
+
from typing import List
|
3
|
+
|
4
|
+
import rich_click as click
|
5
|
+
from rich import print
|
6
|
+
|
7
|
+
from .cli_group import cli
|
8
|
+
from .connected import list_mcus
|
9
|
+
from .list import show_mcus
|
10
|
+
from .logger import make_quiet
|
11
|
+
|
12
|
+
|
13
|
+
@cli.command(
|
14
|
+
"list",
|
15
|
+
help="List the connected MCU boards. alias: devs",
|
16
|
+
aliases=["devs"],
|
17
|
+
)
|
18
|
+
@click.option(
|
19
|
+
"--json",
|
20
|
+
"-j",
|
21
|
+
"as_json",
|
22
|
+
is_flag=True,
|
23
|
+
default=False,
|
24
|
+
show_default=True,
|
25
|
+
help="""Output in json format""",
|
26
|
+
)
|
27
|
+
@click.option(
|
28
|
+
"--serial",
|
29
|
+
"--serial-port",
|
30
|
+
"-s",
|
31
|
+
"serial",
|
32
|
+
default=["*"],
|
33
|
+
multiple=True,
|
34
|
+
show_default=True,
|
35
|
+
help="Serial port(s) (or globs) to list. ",
|
36
|
+
metavar="SERIALPORT",
|
37
|
+
)
|
38
|
+
@click.option(
|
39
|
+
"--ignore",
|
40
|
+
"-i",
|
41
|
+
is_eager=True,
|
42
|
+
help="Serial port(s) (or globs) to ignore. Defaults to MPFLASH_IGNORE.",
|
43
|
+
multiple=True,
|
44
|
+
default=[],
|
45
|
+
envvar="MPFLASH_IGNORE",
|
46
|
+
show_default=True,
|
47
|
+
metavar="SERIALPORT",
|
48
|
+
)
|
49
|
+
@click.option(
|
50
|
+
"--bluetooth/--no-bluetooth",
|
51
|
+
"-b/-nb",
|
52
|
+
is_flag=True,
|
53
|
+
default=False,
|
54
|
+
show_default=True,
|
55
|
+
help="""Include bluetooth ports in the list""",
|
56
|
+
)
|
57
|
+
@click.option(
|
58
|
+
"--progress/--no-progress",
|
59
|
+
# "-p/-np", -p is already used for --port
|
60
|
+
"progress",
|
61
|
+
is_flag=True,
|
62
|
+
default=True,
|
63
|
+
show_default=True,
|
64
|
+
help="""Show progress""",
|
65
|
+
)
|
66
|
+
def cli_list_mcus(serial: List[str], ignore: List[str], bluetooth: bool, as_json: bool, progress: bool = True) -> int:
|
67
|
+
"""List the connected MCU boards, and output in a nice table or json."""
|
68
|
+
serial = list(serial)
|
69
|
+
ignore = list(ignore)
|
70
|
+
if as_json:
|
71
|
+
# avoid noise in json output
|
72
|
+
make_quiet()
|
73
|
+
# TODO? Ask user to select a serialport if [?] is given ?
|
74
|
+
|
75
|
+
conn_mcus = list_mcus(ignore=ignore, include=serial, bluetooth=bluetooth)
|
76
|
+
# ignore boards that have the [micropython-stubber] ignore flag set
|
77
|
+
conn_mcus = [item for item in conn_mcus if not (item.toml.get("mpflash", {}).get("ignore", False))]
|
78
|
+
if as_json:
|
79
|
+
# remove the path and firmware attibutes from the json output as they are always empty
|
80
|
+
for mcu in conn_mcus:
|
81
|
+
del mcu.path
|
82
|
+
del mcu.firmware
|
83
|
+
print(json.dumps([mcu.__dict__ for mcu in conn_mcus], indent=4))
|
84
|
+
progress = False
|
85
|
+
if progress:
|
86
|
+
show_mcus(conn_mcus, refresh=False)
|
87
|
+
return 0 if conn_mcus else 1
|
mpflash/cli_main.py
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
"""mpflash is a CLI to download and flash MicroPython firmware to various boards."""
|
2
|
-
|
3
|
-
import os
|
4
|
-
|
5
|
-
import click.exceptions as click_exceptions
|
6
|
-
from loguru import logger as log
|
7
|
-
|
8
|
-
from .cli_download import cli_download
|
9
|
-
from .cli_flash import cli_flash_board
|
10
|
-
from .cli_group import cli
|
11
|
-
from .cli_list import cli_list_mcus
|
12
|
-
|
13
|
-
|
14
|
-
def mpflash():
|
15
|
-
cli.add_command(cli_list_mcus)
|
16
|
-
cli.add_command(cli_download)
|
17
|
-
cli.add_command(cli_flash_board)
|
18
|
-
|
19
|
-
# cli(auto_envvar_prefix="MPFLASH")
|
20
|
-
if False and os.environ.get("COMPUTERNAME").startswith("JOSVERL"):
|
21
|
-
# intentional less error suppression on dev machine
|
22
|
-
result = cli(standalone_mode=False)
|
23
|
-
else:
|
24
|
-
try:
|
25
|
-
result = cli(standalone_mode=True)
|
26
|
-
exit(result)
|
27
|
-
except AttributeError as e:
|
28
|
-
log.error(f"Error: {e}")
|
29
|
-
exit(-1)
|
30
|
-
except click_exceptions.ClickException as e:
|
31
|
-
log.error(f"Error: {e}")
|
32
|
-
exit(-2)
|
33
|
-
except click_exceptions.Abort as e:
|
34
|
-
# Aborted - Ctrl-C
|
35
|
-
exit(-3)
|
36
|
-
|
37
|
-
|
38
|
-
if __name__ == "__main__":
|
39
|
-
mpflash()
|
1
|
+
"""mpflash is a CLI to download and flash MicroPython firmware to various boards."""
|
2
|
+
|
3
|
+
import os
|
4
|
+
|
5
|
+
import click.exceptions as click_exceptions
|
6
|
+
from loguru import logger as log
|
7
|
+
|
8
|
+
from .cli_download import cli_download
|
9
|
+
from .cli_flash import cli_flash_board
|
10
|
+
from .cli_group import cli
|
11
|
+
from .cli_list import cli_list_mcus
|
12
|
+
|
13
|
+
|
14
|
+
def mpflash():
|
15
|
+
cli.add_command(cli_list_mcus)
|
16
|
+
cli.add_command(cli_download)
|
17
|
+
cli.add_command(cli_flash_board)
|
18
|
+
|
19
|
+
# cli(auto_envvar_prefix="MPFLASH")
|
20
|
+
if False and os.environ.get("COMPUTERNAME").startswith("JOSVERL"):
|
21
|
+
# intentional less error suppression on dev machine
|
22
|
+
result = cli(standalone_mode=False)
|
23
|
+
else:
|
24
|
+
try:
|
25
|
+
result = cli(standalone_mode=True)
|
26
|
+
exit(result)
|
27
|
+
except AttributeError as e:
|
28
|
+
log.error(f"Error: {e}")
|
29
|
+
exit(-1)
|
30
|
+
except click_exceptions.ClickException as e:
|
31
|
+
log.error(f"Error: {e}")
|
32
|
+
exit(-2)
|
33
|
+
except click_exceptions.Abort as e:
|
34
|
+
# Aborted - Ctrl-C
|
35
|
+
exit(-3)
|
36
|
+
|
37
|
+
|
38
|
+
if __name__ == "__main__":
|
39
|
+
mpflash()
|
mpflash/common.py
CHANGED
@@ -1,177 +1,210 @@
|
|
1
|
-
import fnmatch
|
2
|
-
import
|
3
|
-
import
|
4
|
-
|
5
|
-
|
6
|
-
from
|
7
|
-
from
|
8
|
-
|
9
|
-
from
|
10
|
-
|
11
|
-
from
|
12
|
-
|
13
|
-
from .
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
23
|
-
|
24
|
-
"
|
25
|
-
|
26
|
-
"
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
if "
|
72
|
-
data["
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
"""
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
comports =
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
1
|
+
import fnmatch
|
2
|
+
import glob
|
3
|
+
import os
|
4
|
+
import platform
|
5
|
+
import sys
|
6
|
+
from dataclasses import dataclass, field
|
7
|
+
from enum import Enum
|
8
|
+
from pathlib import Path
|
9
|
+
from typing import List, Optional, Union
|
10
|
+
|
11
|
+
from github import Auth, Github
|
12
|
+
from serial.tools import list_ports
|
13
|
+
from serial.tools.list_ports_common import ListPortInfo
|
14
|
+
|
15
|
+
from .logger import log
|
16
|
+
|
17
|
+
# from mpflash.mpremoteboard import MPRemoteBoard
|
18
|
+
|
19
|
+
PORT_FWTYPES = {
|
20
|
+
"stm32": [".dfu"], # need .dfu for pydfu.py - .hex for cube cli/GUI
|
21
|
+
"esp32": [".bin"],
|
22
|
+
"esp8266": [".bin"],
|
23
|
+
"rp2": [".uf2"],
|
24
|
+
"samd": [".uf2"],
|
25
|
+
# below this not yet implemented / tested
|
26
|
+
"mimxrt": [".hex"],
|
27
|
+
"nrf": [".uf2"],
|
28
|
+
"renesas-ra": [".hex"],
|
29
|
+
}
|
30
|
+
|
31
|
+
# Token with no permissions to avoid throttling
|
32
|
+
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#getting-a-higher-rate-limit
|
33
|
+
PAT_NO_ACCESS = (
|
34
|
+
"github_pat"
|
35
|
+
+ "_11AAHPVFQ0qAkDnSUaMKSp"
|
36
|
+
+ "_ZkDl5NRRwBsUN6EYg9ahp1Dvj4FDDONnXVgimxC2EtpY7Q7BUKBoQ0Jq72X"
|
37
|
+
)
|
38
|
+
PAT = os.environ.get("GITHUB_TOKEN") or PAT_NO_ACCESS
|
39
|
+
GH_CLIENT = Github(auth=Auth.Token(PAT))
|
40
|
+
|
41
|
+
|
42
|
+
@dataclass
|
43
|
+
class FWInfo:
|
44
|
+
"""
|
45
|
+
Downloaded Firmware information
|
46
|
+
is somewhat related to the BOARD class in the mpboard_id module
|
47
|
+
"""
|
48
|
+
|
49
|
+
port: str # MicroPython port
|
50
|
+
board: str # MicroPython board
|
51
|
+
filename: str = field(default="") # relative filename of the firmware image
|
52
|
+
firmware: str = field(default="") # url or path to original firmware image
|
53
|
+
variant: str = field(default="") # MicroPython variant
|
54
|
+
preview: bool = field(default=False) # True if the firmware is a preview version
|
55
|
+
version: str = field(default="") # MicroPython version (NO v prefix)
|
56
|
+
url: str = field(default="") # url to the firmware image download folder
|
57
|
+
build: str = field(default="0") # The build = number of commits since the last release
|
58
|
+
ext: str = field(default="") # the file extension of the firmware
|
59
|
+
family: str = field(default="micropython") # The family of the firmware
|
60
|
+
custom: bool = field(default=False) # True if the firmware is a custom build
|
61
|
+
description: str = field(default="") # Description used by this firmware (custom only)
|
62
|
+
|
63
|
+
def to_dict(self) -> dict:
|
64
|
+
"""Convert the object to a dictionary"""
|
65
|
+
return self.__dict__
|
66
|
+
|
67
|
+
@classmethod
|
68
|
+
def from_dict(cls, data: dict) -> "FWInfo":
|
69
|
+
"""Create a FWInfo object from a dictionary"""
|
70
|
+
# add missing keys
|
71
|
+
if "ext" not in data:
|
72
|
+
data["ext"] = Path(data["firmware"]).suffix
|
73
|
+
if "family" not in data:
|
74
|
+
data["family"] = "micropython"
|
75
|
+
return cls(**data)
|
76
|
+
|
77
|
+
|
78
|
+
@dataclass
|
79
|
+
class Params:
|
80
|
+
"""Common parameters for downloading and flashing firmware"""
|
81
|
+
|
82
|
+
ports: List[str] = field(default_factory=list)
|
83
|
+
boards: List[str] = field(default_factory=list)
|
84
|
+
versions: List[str] = field(default_factory=list)
|
85
|
+
fw_folder: Path = Path()
|
86
|
+
serial: List[str] = field(default_factory=list)
|
87
|
+
ignore: List[str] = field(default_factory=list)
|
88
|
+
bluetooth: bool = False
|
89
|
+
|
90
|
+
|
91
|
+
@dataclass
|
92
|
+
class DownloadParams(Params):
|
93
|
+
"""Parameters for downloading firmware"""
|
94
|
+
|
95
|
+
clean: bool = False
|
96
|
+
force: bool = False
|
97
|
+
|
98
|
+
|
99
|
+
class BootloaderMethod(Enum):
|
100
|
+
AUTO = "auto"
|
101
|
+
MANUAL = "manual"
|
102
|
+
MPY = "mpy"
|
103
|
+
TOUCH_1200 = "touch1200"
|
104
|
+
NONE = "none"
|
105
|
+
|
106
|
+
|
107
|
+
@dataclass
|
108
|
+
class FlashParams(Params):
|
109
|
+
"""Parameters for flashing a board"""
|
110
|
+
|
111
|
+
erase: bool = True
|
112
|
+
bootloader: BootloaderMethod = BootloaderMethod.NONE
|
113
|
+
cpu: str = ""
|
114
|
+
|
115
|
+
def __post_init__(self):
|
116
|
+
if isinstance(self.bootloader, str):
|
117
|
+
self.bootloader = BootloaderMethod(self.bootloader)
|
118
|
+
|
119
|
+
|
120
|
+
ParamType = Union[DownloadParams, FlashParams]
|
121
|
+
|
122
|
+
|
123
|
+
def filtered_comports(
|
124
|
+
ignore: Optional[List[str]] = None,
|
125
|
+
include: Optional[List[str]] = None,
|
126
|
+
bluetooth: bool = False,
|
127
|
+
) -> List[ListPortInfo]: # sourcery skip: assign-if-exp
|
128
|
+
"""
|
129
|
+
Get a list of filtered comports using the include and ignore lists.
|
130
|
+
both can be globs (e.g. COM*) or exact port names (e.g. COM1)
|
131
|
+
"""
|
132
|
+
if not ignore:
|
133
|
+
ignore = []
|
134
|
+
elif not isinstance(ignore, list): # type: ignore
|
135
|
+
ignore = list(ignore)
|
136
|
+
if not include:
|
137
|
+
include = ["*"]
|
138
|
+
elif not isinstance(include, list): # type: ignore
|
139
|
+
include = list(include)
|
140
|
+
|
141
|
+
# remove ports that are to be ignored
|
142
|
+
log.trace(f"{include=}, {ignore=}, {bluetooth=}")
|
143
|
+
|
144
|
+
comports = [
|
145
|
+
p for p in list_ports.comports() if not any(fnmatch.fnmatch(p.device, i) for i in ignore)
|
146
|
+
]
|
147
|
+
if platform.system() == "Linux":
|
148
|
+
# use p.location to filter out the bogus ports on newer Linux kernels
|
149
|
+
# filter out the bogus ports on newer Linux kernels
|
150
|
+
comports = [p for p in comports if p.location]
|
151
|
+
|
152
|
+
log.trace(f"comports: {[p.device for p in comports]}")
|
153
|
+
# remove bluetooth ports
|
154
|
+
|
155
|
+
if include != ["*"]:
|
156
|
+
# if there are explicit ports to include, add them to the list
|
157
|
+
explicit = [
|
158
|
+
p for p in list_ports.comports() if any(fnmatch.fnmatch(p.device, i) for i in include)
|
159
|
+
]
|
160
|
+
log.trace(f"explicit: {[p.device for p in explicit]}")
|
161
|
+
if ignore == []:
|
162
|
+
# if nothing to ignore, just use the explicit list as a sinple sane default
|
163
|
+
comports = explicit
|
164
|
+
else:
|
165
|
+
# if there are ports to ignore, add the explicit list to the filtered list
|
166
|
+
comports = list(set(explicit) | set(comports))
|
167
|
+
if not bluetooth:
|
168
|
+
# filter out bluetooth ports
|
169
|
+
comports = [p for p in comports if "bluetooth" not in p.description.lower()]
|
170
|
+
comports = [p for p in comports if "BTHENUM" not in p.hwid]
|
171
|
+
if sys.platform == "darwin":
|
172
|
+
comports = [p for p in comports if ".Bluetooth" not in p.device]
|
173
|
+
log.trace(f"no Bluetooth: {[p.device for p in comports]}")
|
174
|
+
log.debug(f"filtered_comports: {[p.device for p in comports]}")
|
175
|
+
# sort
|
176
|
+
if sys.platform == "win32":
|
177
|
+
# Windows sort of comports by number - but fallback to device name
|
178
|
+
return sorted(
|
179
|
+
comports,
|
180
|
+
key=lambda x: int(x.device.split()[0][3:]) if x.device.split()[0][3:].isdigit() else x,
|
181
|
+
)
|
182
|
+
# sort by device name
|
183
|
+
return sorted(comports, key=lambda x: x.device)
|
184
|
+
|
185
|
+
|
186
|
+
def find_serial_by_path(target_port: str):
|
187
|
+
"""Find the symbolic link path of a serial port by its device path."""
|
188
|
+
# sourcery skip: use-next
|
189
|
+
|
190
|
+
if os.name == "nt":
|
191
|
+
return None
|
192
|
+
# List all available serial ports
|
193
|
+
available_ports = list_ports.comports()
|
194
|
+
# Filter to get the device path of the target port
|
195
|
+
target_device_path = None
|
196
|
+
for port in available_ports:
|
197
|
+
if port.device == target_port:
|
198
|
+
target_device_path = port.device
|
199
|
+
break
|
200
|
+
|
201
|
+
if not target_device_path:
|
202
|
+
return None # Target port not found among available ports
|
203
|
+
|
204
|
+
# Search for all symbolic links in /dev/serial/by-path/
|
205
|
+
for symlink in glob.glob("/dev/serial/by-path/*"):
|
206
|
+
# Resolve the symbolic link to its target
|
207
|
+
if os.path.realpath(symlink) == target_device_path:
|
208
|
+
return symlink # Return the matching symlink path
|
209
|
+
|
210
|
+
return None # Return None if no match is found
|