scripts: modules: Move blob processing to zephyr_module.py

In preparation for the handling of taint flags in zephyr_module.py, move
blob-processing code from the west command to it.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
diff --git a/scripts/zephyr_module.py b/scripts/zephyr_module.py
index be7e127..c8d2e3b 100755
--- a/scripts/zephyr_module.py
+++ b/scripts/zephyr_module.py
@@ -18,6 +18,7 @@
 '''
 
 import argparse
+import hashlib
 import os
 import re
 import subprocess
@@ -131,6 +132,9 @@
 MODULE_YML_PATH = PurePath('zephyr/module.yml')
 # Path to the blobs folder
 MODULE_BLOBS_PATH = PurePath('zephyr/blobs')
+BLOB_PRESENT = 'A'
+BLOB_NOT_PRESENT = 'D'
+BLOB_OUTDATED = 'M'
 
 schema = yaml.safe_load(METADATA_SCHEMA)
 
@@ -225,6 +229,34 @@
     return out_text
 
 
+def get_blob_status(path, sha256):
+    if not path.is_file():
+        return BLOB_NOT_PRESENT
+    with path.open('rb') as f:
+        m = hashlib.sha256()
+        m.update(f.read())
+        if sha256.lower() == m.hexdigest():
+            return BLOB_PRESENT
+        else:
+            return BLOB_OUTDATED
+
+
+def process_blobs(module, meta):
+    blobs = []
+    mblobs = meta.get('blobs', None)
+    if not mblobs:
+        return blobs
+
+    blobs_path = Path(module) / MODULE_BLOBS_PATH
+    for blob in mblobs:
+        blob['module'] = meta.get('name', None)
+        blob['abspath'] = blobs_path / Path(blob['path'])
+        blob['status'] = get_blob_status(blob['abspath'], blob['sha256'])
+        blobs.append(blob)
+
+    return blobs
+
+
 def kconfig_snippet(meta, path, kconfig_file=None):
     name = meta['name']
     name_sanitized = meta['name-sanitized']