1#!/usr/bin/python3
2# Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
3# SPDX-License-Identifier: Apache-2.0
4
5import os
6import subprocess
7import shutil
8import ntpath
9import hashlib
10from pathlib import Path
11
12# This relies on this file being in hal_espressif/zephyr/scripts/esp_genblobs.py
13# If you move this file, you'll break it, so be careful.
14MODULE_PATH = Path(Path(__file__).resolve().parents[2], "module.yml")
15SUBMODULES = Path(Path(__file__).resolve().parents[1], "submodules.txt")
16
17socs = ["esp32", "esp32s2", "esp32c3", "esp32s3"]
18
19module_yaml = """\
20name: hal_espressif
21build:
22  cmake: zephyr
23  kconfig: zephyr/Kconfig
24  settings:
25    dts_root: .
26blobs:"""
27
28blob_item = '''
29  - path: lib/{SOC}/{FILENAME}
30    sha256: {SHA256}
31    type: lib
32    version: '1.0'
33    license-path: zephyr/blobs/license.txt
34    url: {URL}/raw/{REV}/{SOC}/{FILENAME}
35    description: "Binary libraries supporting the ESP32 series RF subsystems"
36    doc-url: {URL_BASE}'''
37
38
39def cmd_exec(cmd, cwd=None, shell=False):
40    return subprocess.check_call(cmd, cwd=cwd, shell=shell)
41
42
43def download_repositories():
44    path = os.path.dirname(os.path.abspath(__file__))
45    with open(SUBMODULES) as f:
46        for submodule in f:
47            git_rev, git_dir, git_url = submodule.split()
48            folder = Path(path, "temp", git_dir)
49            if not folder.exists():
50                print("Cloning into {}".format(folder))
51                cmd_exec(("git", "clone", git_url, folder, "--quiet"), cwd=path)
52            print("Checking out revision {} at {}".format(git_rev, folder))
53            cmd_exec(("git", "-C", folder, "fetch"), cwd=path)
54            cmd_exec(("git", "-C", folder, "checkout", git_rev, "--quiet"), cwd=path)
55
56
57def clean_up():
58    print("deleted temporary files..")
59    path = os.path.dirname(os.path.abspath(__file__))
60    folder = Path(path, "temp")
61    shutil.rmtree(folder)
62
63
64def path_leaf(path):
65    head, tail = ntpath.split(path)
66    return tail or ntpath.basename(head)
67
68
69def get_file_sha256(path):
70    with open(path,"rb") as f:
71        f_byte = f.read()
72        result = hashlib.sha256(f_byte)
73        return result.hexdigest()
74
75
76def generate_blob_list():
77    file_out = module_yaml
78
79    path = os.path.dirname(os.path.abspath(__file__))
80    with open(SUBMODULES) as f:
81        for submodule in f:
82            git_rev, git_dir, git_url = submodule.split()
83
84            for s in socs:
85                folder = Path(path, "temp", git_dir, s)
86                pathlist = Path(folder).glob('**/*.a')
87                for item in pathlist:
88                    path_in_str = str(item)
89                    filename = path_leaf(path_in_str)
90                    sha256 = get_file_sha256(path_in_str)
91                    file_out += blob_item.format(SOC=s,
92                                                 FILENAME=filename,
93                                                 SHA256=sha256,
94                                                 URL=git_url,
95                                                 REV=git_rev,
96                                                 URL_BASE=git_url)
97    file_out += "\r\n"
98
99    try:
100        os.remove(filename)
101    except OSError:
102        pass
103
104    with open(MODULE_PATH, "w+") as f:
105        f.write(file_out)
106        f.close()
107
108    print("module.yml updated!")
109
110
111if __name__ == '__main__':
112    download_repositories()
113    generate_blob_list()
114    clean_up()
115