Skip to content

Commit a8a3956

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 e23bd4f commit a8a3956

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
@@ -16,7 +16,6 @@
1616

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

2120

2221
def main():
@@ -193,8 +192,15 @@ def do_flash(conf, args):
193192

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

218225
conn = open_connection(board, args)
219226
# Upload flash writer if needed
@@ -385,19 +392,6 @@ def get_srec_load_addr(fname):
385392
raise Exception(f"Could not read srec load address (S3) from {fname}")
386393

387394

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

0 commit comments

Comments
 (0)