Skip to content

Commit d4dba59

Browse files
committed
Updating nxp_sdk update script after code review
- dataclass is now used - subprocess dependencies on cwd removed with possible (for west update and west forall cwd is still required) - python logging module is now used Signed-off-by: Gatien Chapon <gatien.chapon@nxp.com>
1 parent fd6ecf2 commit d4dba59

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

scripts/setup/nxp/update_nxp_sdk.py

+57-35
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,104 @@
1616
#
1717

1818
import argparse
19+
import logging
1920
import os
21+
import shutil
2022
import subprocess
2123
import sys
24+
from dataclasses import dataclass
2225

2326
CHIP_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
2427

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+
2647
ALL_PLATFORM_SDK = [
27-
{'plat_name': 'k32w0', 'plat_sdk_manifest_path': 'third_party/nxp/nxp_matter_support/github_sdk/k32w0'},
48+
NxpSdk_k32w0(),
2849
]
2950

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]
3152

3253

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")
3556
# Cleaning all local modifications
3657
git_clean_command = "git reset --hard && git clean -xdf"
3758
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)
3960

4061

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)
4970

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)
5374

5475

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")
5980
sys.exit(1)
6081
command = ['west', 'update', '--fetch', 'smart']
6182
try:
62-
subprocess.run(command, cwd=sdk_target_location, check=True)
83+
subprocess.run(command, cwd=nxp_sdk.sdk_target_location, check=True)
6384
except (RuntimeError, subprocess.CalledProcessError) as exception:
6485
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)
7094
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)
7497

7598

7699
def main():
77100

78101
parser = argparse.ArgumentParser(description='Checkout or update relevant NXP SDK')
79102
parser.add_argument(
80103
"--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,
82105
help='Allows to select which SDK for a particular NXP platform to initialize')
83106
parser.add_argument('--force', action='store_true',
84107
help='Force SDK initialization, hard clean will be done in case of failure - WARNING -- All local SDK modification(s) will be lost')
85108

86109
args = parser.parse_args()
87110

88111
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]
91113
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)
93115
else:
94-
init_platform_sdk_version(os.path.join(CHIP_ROOT, plat_path), args.force)
116+
init_nxp_sdk_version(nxp_sdk, args.force)
95117

96118

97119
if __name__ == '__main__':

0 commit comments

Comments
 (0)