Skip to content

Commit 12f96dd

Browse files
author
Mykhailo Androsiuk
committed
Defer pyftdi import: only import when --cpld option is used
Previously, the pyftdi module was imported unconditionally, causing errors even when CPLD functionality was not required. Now the import is performed only if the `--cpld` option is used, avoiding unnecessary failures and improving script flexibility. Signed-off-by: Mykhailo Androsiuk <mykhailo_androsiuk@epam.com>
1 parent bcef392 commit 12f96dd

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

rcar_flash/rcar_flash.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")
1717
log = logging.getLogger(__name__)
18-
cpld_available = False
1918

2019

2120
def main():
@@ -192,8 +191,15 @@ def do_flash(conf, args):
192191

193192
# Check if need to nudge CPLD
194193
if args.cpld:
195-
if not cpld_available:
196-
raise Exception("pyftdi is not available")
194+
try:
195+
import pyftdi # noqa: F401
196+
except ModuleNotFoundError:
197+
print(
198+
"The pyftdi module is required for working with CPLD.\n"
199+
"Please use 'sudo apt install python3-pyftdi' or the appropriate package manager."
200+
)
201+
sys.exit(1)
202+
197203
if "cpld_profile" not in board:
198204
raise Exception(
199205
"'cpld_profile' is not set for board, can't control CPLD")
@@ -213,6 +219,7 @@ def do_flash(conf, args):
213219
time.sleep(0.5)
214220
# Need to release port, so pyserial can use it
215221
del cpld
222+
time.sleep(5.5) # Waiting for the recreation of /dev/ttyUSB* and the symlink
216223

217224
conn = open_connection(board, args)
218225
# Upload flash writer if needed
@@ -383,19 +390,6 @@ def get_srec_load_addr(fname):
383390
raise Exception(f"Could not read srec load address (S3) from {fname}")
384391

385392

386-
# CPLD Code begins there
387-
try:
388-
import pyftdi
389-
cpld_available = True
390-
except ModuleNotFoundError:
391-
log.error("pyftdi module not found. CPLD Functinality is disabled.")
392-
log.error(" Please install the module.")
393-
log.error(" You can try \"pip3 install --user pyftdi\"")
394-
log.error(
395-
" Or use your favourite packet manager (\"apt install python3-ftdi\" perhaps?)"
396-
)
397-
398-
399393
def cpld_determine_serial(cpld_profile, args) -> str:
400394
import pyftdi.usbtools
401395
# Try to determine USB device serial number where CPLD resides

0 commit comments

Comments
 (0)