|
16 | 16 | #
|
17 | 17 |
|
18 | 18 | import argparse
|
| 19 | +import logging |
19 | 20 | import os
|
| 21 | +import shutil |
20 | 22 | import subprocess
|
21 | 23 | import sys
|
| 24 | +from dataclasses import dataclass |
22 | 25 |
|
23 | 26 | CHIP_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
|
24 | 27 |
|
25 |
| -# Array listing all SDK and default targeted path for NXP SDK storage |
| 28 | + |
| 29 | +@dataclass(init=False) |
| 30 | +class NxpSdk: |
| 31 | + sdk_name: str |
| 32 | + sdk_target_location: str |
| 33 | + sdk_manifest_path: str |
| 34 | + |
| 35 | + def __init__(self, name, sdk_target_location): |
| 36 | + self.sdk_name = name |
| 37 | + self.sdk_target_location = sdk_target_location |
| 38 | + self.sdk_manifest_path = os.path.abspath(os.path.join(sdk_target_location, 'manifest')) |
| 39 | + |
| 40 | + |
| 41 | +def NxpSdk_k32w0(): |
| 42 | + rel_path_k32w0 = 'third_party/nxp/nxp_matter_support/github_sdk/k32w0' |
| 43 | + sdk = NxpSdk('k32w0', os.path.abspath(os.path.join(CHIP_ROOT, rel_path_k32w0))) |
| 44 | + return sdk |
| 45 | + |
| 46 | + |
26 | 47 | ALL_PLATFORM_SDK = [
|
27 |
| - {'plat_name': 'k32w0', 'plat_sdk_manifest_path': 'third_party/nxp/nxp_matter_support/github_sdk/k32w0'}, |
| 48 | + NxpSdk_k32w0(), |
28 | 49 | ]
|
29 | 50 |
|
30 |
| -all_platform_sdk_list = list(map(lambda plat: plat["plat_name"], ALL_PLATFORM_SDK)) |
| 51 | +ALL_PLATFORM_NAME = [p.sdk_name for p in ALL_PLATFORM_SDK] |
31 | 52 |
|
32 | 53 |
|
33 |
| -def clean_sdk_local_changes(sdk_target_location): |
34 |
| - print("SDK will be cleaned all local modification(s) will be lost") |
| 54 | +def clean_sdk_local_changes(sdk_location): |
| 55 | + logging.warning("SDK will be cleaned all local modification(s) will be lost") |
35 | 56 | # Cleaning all local modifications
|
36 | 57 | git_clean_command = "git reset --hard && git clean -xdf"
|
37 | 58 | command = ['west', 'forall', '-c', git_clean_command, '-a']
|
38 |
| - subprocess.run(command, cwd=sdk_target_location, check=True) |
| 59 | + subprocess.run(command, cwd=sdk_location, check=True) |
39 | 60 |
|
40 | 61 |
|
41 |
| -def init_platform_sdk_version(sdk_target_location, force): |
42 |
| - print("Init SDK in: " + sdk_target_location) |
43 |
| - if not force and os.path.exists(os.path.join(sdk_target_location, '.west')): |
44 |
| - print("Error SDK is already initialized, use --force to force init") |
45 |
| - print("WARNING -- All local SDK modification(s) will be lost") |
46 |
| - sys.exit(1) |
47 |
| - command = ['rm', '-rf', '.west'] |
48 |
| - subprocess.run(command, cwd=sdk_target_location, check=True) |
| 62 | +def init_nxp_sdk_version(nxp_sdk, force): |
| 63 | + print("Init SDK in: " + nxp_sdk.sdk_target_location) |
| 64 | + west_path = os.path.join(nxp_sdk.sdk_target_location, '.west') |
| 65 | + if os.path.exists(west_path): |
| 66 | + if not force: |
| 67 | + logging.error("SDK is already initialized, use --force to force init") |
| 68 | + sys.exit(1) |
| 69 | + shutil.rmtree(west_path) |
49 | 70 |
|
50 |
| - command = ['west', 'init', '-l', 'manifest', '--mf', 'west.yml'] |
51 |
| - subprocess.run(command, cwd=sdk_target_location, check=True) |
52 |
| - update_platform_sdk_version(sdk_target_location, force) |
| 71 | + command = ['west', 'init', '-l', nxp_sdk.sdk_manifest_path, '--mf', 'west.yml'] |
| 72 | + subprocess.run(command, check=True) |
| 73 | + update_nxp_sdk_version(nxp_sdk, force) |
53 | 74 |
|
54 | 75 |
|
55 |
| -def update_platform_sdk_version(sdk_target_location, force): |
56 |
| - print("Update SDK in " + sdk_target_location) |
57 |
| - if not os.path.exists(os.path.join(sdk_target_location, '.west')): |
58 |
| - print("--update-only error SDK is not initialized") |
| 76 | +def update_nxp_sdk_version(nxp_sdk, force): |
| 77 | + print("Update SDK in " + nxp_sdk.sdk_target_location) |
| 78 | + if not os.path.exists(os.path.join(nxp_sdk.sdk_target_location, '.west')): |
| 79 | + logging.error("--update-only error SDK is not initialized") |
59 | 80 | sys.exit(1)
|
60 | 81 | command = ['west', 'update', '--fetch', 'smart']
|
61 | 82 | try:
|
62 |
| - subprocess.run(command, cwd=sdk_target_location, check=True) |
| 83 | + subprocess.run(command, cwd=nxp_sdk.sdk_target_location, check=True) |
63 | 84 | except (RuntimeError, subprocess.CalledProcessError) as exception:
|
64 | 85 | if force:
|
65 |
| - # In case of force update option and in case of update failure: |
66 |
| - # 1. try to clean all local modications if any |
67 |
| - # 2. Retry the west update command. It should be successfull now as all local modifications have been cleaned |
68 |
| - clean_sdk_local_changes(sdk_target_location) |
69 |
| - subprocess.run(command, cwd=sdk_target_location, check=True) |
| 86 | + if nxp_sdk.sdk_name == 'k32w0': |
| 87 | + logging.error('Force update not yet supported for %s platform', nxp_sdk.sdk_name) |
| 88 | + else: |
| 89 | + # In case of force update option and in case of update failure: |
| 90 | + # 1. try to clean all local modications if any |
| 91 | + # 2. Retry the west update command. It should be successfull now as all local modifications have been cleaned |
| 92 | + clean_sdk_local_changes(nxp_sdk.sdk_target_location) |
| 93 | + subprocess.run(command, cwd=nxp_sdk.sdk_target_location, check=True) |
70 | 94 | else:
|
71 |
| - print(exception) |
72 |
| - print("Error SDK cannot be updated, local changes should be cleaned manually or use --force to force update") |
73 |
| - print("WARNING -- With --force all local SDK modification(s) will be lost") |
| 95 | + logging.exception( |
| 96 | + 'Error SDK cannot be updated, local changes should be cleaned manually or use --force to force update %s', exception) |
74 | 97 |
|
75 | 98 |
|
76 | 99 | def main():
|
77 | 100 |
|
78 | 101 | parser = argparse.ArgumentParser(description='Checkout or update relevant NXP SDK')
|
79 | 102 | parser.add_argument(
|
80 | 103 | "--update-only", help="Update NXP SDK to the correct version. Would fail if the SDK does not exist", action="store_true")
|
81 |
| - parser.add_argument('--platform', nargs='+', choices=all_platform_sdk_list, default=all_platform_sdk_list, |
| 104 | + parser.add_argument('--platform', nargs='+', choices=ALL_PLATFORM_NAME, default=ALL_PLATFORM_NAME, |
82 | 105 | help='Allows to select which SDK for a particular NXP platform to initialize')
|
83 | 106 | parser.add_argument('--force', action='store_true',
|
84 | 107 | help='Force SDK initialization, hard clean will be done in case of failure - WARNING -- All local SDK modification(s) will be lost')
|
85 | 108 |
|
86 | 109 | args = parser.parse_args()
|
87 | 110 |
|
88 | 111 | for current_plat in args.platform:
|
89 |
| - plat_path = list(map(lambda res: res['plat_sdk_manifest_path'], filter( |
90 |
| - lambda cmd: cmd["plat_name"] == current_plat, ALL_PLATFORM_SDK)))[0] |
| 112 | + nxp_sdk = [p for p in ALL_PLATFORM_SDK if p.sdk_name == current_plat][0] |
91 | 113 | if args.update_only:
|
92 |
| - update_platform_sdk_version(os.path.join(CHIP_ROOT, plat_path), args.force) |
| 114 | + update_nxp_sdk_version(nxp_sdk, args.force) |
93 | 115 | else:
|
94 |
| - init_platform_sdk_version(os.path.join(CHIP_ROOT, plat_path), args.force) |
| 116 | + init_nxp_sdk_version(nxp_sdk, args.force) |
95 | 117 |
|
96 | 118 |
|
97 | 119 | if __name__ == '__main__':
|
|
0 commit comments