pytest-embedded-serial-esp 1.18.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.
@@ -0,0 +1,9 @@
1
+ """Make pytest-embedded plugin work with Espressif target boards."""
2
+
3
+ from .serial import EspSerial
4
+
5
+ __all__ = [
6
+ 'EspSerial',
7
+ ]
8
+
9
+ __version__ = '1.18.2'
@@ -0,0 +1,213 @@
1
+ import contextlib
2
+ import functools
3
+ import logging
4
+ import subprocess
5
+ import warnings
6
+ from typing import List, Optional
7
+ from warnings import warn
8
+
9
+ import esptool
10
+ from esptool import __version__ as ESPTOOL_VERSION
11
+ from esptool.targets import CHIP_LIST as ESPTOOL_CHIPS
12
+ from pexpect import TIMEOUT
13
+ from pytest_embedded.log import MessageQueue, PexpectProcess, live_print_call
14
+ from pytest_embedded.utils import Meta
15
+ from pytest_embedded_serial.dut import Serial
16
+
17
+
18
+ def _is_port_mac_verified(pexpect_proc: PexpectProcess, port: str, port_mac: str, msg_queue) -> bool:
19
+ try:
20
+ live_print_call(['esptool.py', '--port', port, 'read_mac'], msg_queue=msg_queue)
21
+ except subprocess.CalledProcessError:
22
+ return False
23
+ else:
24
+ try:
25
+ pexpect_proc.expect(f'MAC: {port_mac.lower()}', timeout=0.1)
26
+ except TIMEOUT:
27
+ return False
28
+ else:
29
+ return True
30
+
31
+
32
+ class EsptoolArgs:
33
+ """
34
+ fake args object, this is a hack until esptool Python API is improved
35
+ """
36
+
37
+ def __init__(self, **kwargs):
38
+ warnings.warn('EsptoolArgs is deprecated and will be removed in 2.0 release.', DeprecationWarning)
39
+
40
+ for key, value in kwargs.items():
41
+ self.__setattr__(key, value)
42
+
43
+
44
+ class EspSerial(Serial):
45
+ """
46
+ Serial class for ports connected to espressif products
47
+ """
48
+
49
+ ESPTOOL_DEFAULT_BAUDRATE = 921600
50
+
51
+ def __init__(
52
+ self,
53
+ pexpect_proc: PexpectProcess,
54
+ msg_queue: MessageQueue,
55
+ target: Optional[str] = None,
56
+ beta_target: Optional[str] = None,
57
+ port: Optional[str] = None,
58
+ port_serial_number: Optional[str] = None,
59
+ port_mac: Optional[str] = None,
60
+ baud: int = Serial.DEFAULT_BAUDRATE,
61
+ esptool_baud: int = ESPTOOL_DEFAULT_BAUDRATE,
62
+ esp_flash_force: bool = False,
63
+ skip_autoflash: bool = False,
64
+ erase_all: bool = False,
65
+ meta: Optional[Meta] = None,
66
+ ports_to_occupy: List[str] = (),
67
+ **kwargs,
68
+ ) -> None:
69
+ self._meta = meta
70
+ filters = {}
71
+ if port_serial_number:
72
+ filters['serials'] = [s.strip() for s in port_serial_number.split(',') if s.strip()]
73
+
74
+ esptool_target = beta_target or target or 'auto'
75
+ if port is None or port.endswith('*'):
76
+ port_filter = port.strip('*') if port else ''
77
+ available_ports = [_p for _p in esptool.get_port_list(**filters) if port_filter in _p]
78
+ ports = list(set(available_ports) - set(self.occupied_ports.keys()) - set(ports_to_occupy))
79
+
80
+ # sort to make /dev/ttyS* ports before /dev/ttyUSB* ports
81
+ # esptool will reverse the list
82
+ ports.sort()
83
+ if port_mac:
84
+ for port in ports:
85
+ if _is_port_mac_verified(pexpect_proc, port, port_mac, msg_queue):
86
+ ports = [port]
87
+ break
88
+ else:
89
+ raise ValueError(f'The specified MAC address {port_mac} cannot be found.')
90
+
91
+ # prioritize the cache recorded target port
92
+ if esptool_target and self._meta:
93
+ ports.sort(key=lambda x: self._meta.hit_port_target_cache(x, esptool_target))
94
+
95
+ logging.debug(f'Detecting ports from {", ".join(ports)}')
96
+ else:
97
+ if port_mac:
98
+ if _is_port_mac_verified(pexpect_proc, port, port_mac, msg_queue):
99
+ ports = [port]
100
+ else:
101
+ raise ValueError(f'The specified MAC address {port_mac} binds with different port, not with {port}')
102
+ else:
103
+ ports = [port]
104
+
105
+ # normal loader
106
+ if esptool_target not in ['auto', *ESPTOOL_CHIPS]:
107
+ raise ValueError(
108
+ f'esptool version {ESPTOOL_VERSION} not support target {esptool_target}\n'
109
+ f'Supported targets: {ESPTOOL_CHIPS}'
110
+ )
111
+
112
+ with contextlib.redirect_stdout(msg_queue):
113
+ self.esp = esptool.get_default_connected_device(
114
+ ports,
115
+ port=port,
116
+ connect_attempts=3,
117
+ initial_baud=baud,
118
+ chip=esptool_target,
119
+ )
120
+
121
+ if not self.esp:
122
+ raise ValueError('Couldn\'t auto detect chip. Please manually specify with "--port"')
123
+
124
+ target = self.esp.CHIP_NAME.lower().replace('-', '')
125
+ logging.info('Target: %s, Port: %s', target, self.esp.serial_port)
126
+
127
+ self.target = target
128
+
129
+ self.skip_autoflash = skip_autoflash
130
+ self.erase_all = erase_all
131
+ self.esptool_baud = esptool_baud
132
+ self.esp_flash_force = esp_flash_force
133
+
134
+ super().__init__(
135
+ msg_queue=msg_queue, port=self.esp._port, baud=baud, meta=meta, ports_to_occupy=ports_to_occupy, **kwargs
136
+ )
137
+
138
+ def _post_init(self):
139
+ if self._meta:
140
+ self._meta.set_port_target_cache(self.port, self.target)
141
+
142
+ if self.erase_all:
143
+ esptool.main(['erase_flash'], esp=self.esp)
144
+
145
+ super()._post_init()
146
+
147
+ def use_esptool(hard_reset_after: Optional[bool] = None, no_stub: Optional[bool] = None):
148
+ """
149
+ 1. tell the redirect serial thread to stop reading from the `pyserial` instance
150
+ 2. esptool reuse the `pyserial` instance and call `esptool.main()` to do the actual work
151
+ 3. tell the redirect serial thread to continue reading from serial
152
+
153
+ Args:
154
+ hard_reset_after: run hard reset after (deprecated)
155
+ no_stub: disable launching the flasher stub (deprecated)
156
+ """
157
+ if hard_reset_after is not None:
158
+ warn(
159
+ "The 'hard_reset_after' parameter is now read directly from `flasher_args.json` "
160
+ 'and does not need to be explicitly set. This parameter will be removed in 2.0 release.',
161
+ DeprecationWarning,
162
+ )
163
+
164
+ if no_stub is not None:
165
+ warn(
166
+ "The 'no_stub' parameter is now read directly from `flasher_args.json` "
167
+ 'and does not need to be explicitly set. This parameter will be removed in 2.0 release.',
168
+ DeprecationWarning,
169
+ )
170
+
171
+ def decorator(func):
172
+ @functools.wraps(func)
173
+ def wrapper(self, *args, **kwargs):
174
+ with self.disable_redirect_thread():
175
+ with contextlib.redirect_stdout(self._q):
176
+ settings = self.proc.get_settings()
177
+ self.esp.connect()
178
+ ret = func(self, *args, **kwargs)
179
+ self.proc.apply_settings(settings)
180
+ return ret
181
+
182
+ return wrapper
183
+
184
+ return decorator
185
+
186
+ def _start(self):
187
+ self.hard_reset()
188
+
189
+ def hard_reset(self):
190
+ """Hard reset your espressif device"""
191
+ self.esp.hard_reset()
192
+
193
+ @use_esptool()
194
+ def erase_flash(self, force: bool = False) -> None:
195
+ """Erase the complete flash"""
196
+ logging.info('Erasing the flash')
197
+ options = ['erase_flash']
198
+
199
+ if force or self.esp_flash_force:
200
+ options.append('--force')
201
+
202
+ esptool.main(options, esp=self.esp)
203
+
204
+ if self._meta:
205
+ self._meta.drop_port_app_cache(self.port)
206
+
207
+ @property
208
+ def stub(self):
209
+ warn(
210
+ 'Please use `self.esp` instead of `self.stub`. `self.stub` will be removed in 2.0 release.',
211
+ DeprecationWarning,
212
+ )
213
+ return self.esp
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-embedded-serial-esp
3
+ Version: 1.18.2
4
+ Summary: Make pytest-embedded plugin work with Espressif target boards.
5
+ Author-email: Fu Hanxi <fuhanxi@espressif.com>
6
+ Requires-Python: >=3.7
7
+ Description-Content-Type: text/markdown
8
+ Classifier: Development Status :: 5 - Production/Stable
9
+ Classifier: Framework :: Pytest
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python
23
+ Classifier: Topic :: Software Development :: Testing
24
+ License-File: LICENSE
25
+ Requires-Dist: pytest-embedded-serial~=1.18.2
26
+ Requires-Dist: esptool~=4.9
27
+ Project-URL: changelog, https://github.com/espressif/pytest-embedded/blob/main/CHANGELOG.md
28
+ Project-URL: documentation, https://docs.espressif.com/projects/pytest-embedded/en/latest/
29
+ Project-URL: homepage, https://github.com/espressif/pytest-embedded
30
+ Project-URL: repository, https://github.com/espressif/pytest-embedded
31
+
32
+ ### pytest-embedded-serial-esp
33
+
34
+ pytest embedded service for testing espressif boards via serial ports
35
+
36
+ Extra Functionalities:
37
+
38
+ - `serial`: detect and confirm target and port by `esptool` automatically.
39
+
@@ -0,0 +1,6 @@
1
+ pytest_embedded_serial_esp/__init__.py,sha256=vwxKZ3_npv1g_22hDERb7iMjQgDSYXASLgsdKp9KUNI,156
2
+ pytest_embedded_serial_esp/serial.py,sha256=_6V6QhIVLadfjoSbhzEIYPjZPh0GqT7zU_9wZiAe4rI,7485
3
+ pytest_embedded_serial_esp-1.18.2.dist-info/licenses/LICENSE,sha256=_Um-MzeKCR0JCDg5Ac_WFmZEfsPTd7PNlNi9JF1GBX8,1094
4
+ pytest_embedded_serial_esp-1.18.2.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
5
+ pytest_embedded_serial_esp-1.18.2.dist-info/METADATA,sha256=d5DvuoiJOILBNUQ8RbXAm3cvr3AgVJmhixsT3m1se40,1658
6
+ pytest_embedded_serial_esp-1.18.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.12.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Espressif Systems (Shanghai) Co. Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.