Soft fail dfu when missing dependencies
Catches instances where users haven't installed construct and/or bitstring and fails with a soft error so cmake can still finish gracefully since not everyone needs a .dfu.
@@ -5,10 +5,33 @@ import sys
import pathlib
import zlib
-import construct
+missing_modules = []
+try:
from bitstring import BitArray
+except ImportError:
+ missing_modules.append("bitstring")
+
+try:
+ import construct
from construct import this, len_, Struct, Padded, CString, Rebuild, Computed, RawCopy, Checksum, Hex, Bytes, Int8ul, Int16ul, Int32ul, Const, Flag, Padding, Array, Prefixed, GreedyBytes, GreedyRange, PaddedString
+except ImportError:
+ missing_modules.append("construct")
+
+if len(missing_modules) > 0:
+ error_text = ""
+ if 'build' in sys.argv:
+ error_text += """
+Warning: no dfu file has been generated (missing python3 dependencies)
+"""
+ error_text += f"""
+dfu requires the following python module(s): {', '.join(missing_modules)}
+install with: python3 -m pip install {' '.join(missing_modules)}
+"""
+ # Soft fail to avoid cmake error
+ print(error_text)
+ sys.exit(0)
+
DFU_SIGNATURE = b'DfuSe'
DFU_size = Rebuild(Int32ul, 0)
