Skip to content

Commit 24d0314

Browse files
[nrf noup] Fix zap instalation for MacOs
Fix updating get_zap.py script, which ensure zap will work on the MacOs systems. Signed-off-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
1 parent 1e9f9b2 commit 24d0314

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

scripts/setup/nrfconnect/get_zap.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
2-
32
#
4-
# Copyright (c) 2023 Project CHIP Authors
3+
# Copyright (c) 2024 Project CHIP Authors
54
#
65
# Licensed under the Apache License, Version 2.0 (the "License");
76
# you may not use this file except in compliance with the License.
@@ -15,7 +14,6 @@
1514
# See the License for the specific language governing permissions and
1615
# limitations under the License.
1716
#
18-
1917
# The script parses integrations/docker/images/chip-build/Dockerfile file
2018
# from the Matter repository and looks for ENV ZAP_VERSION= string to find
2119
# currently recommended ZAP version. After that the package matching this version
@@ -32,7 +30,6 @@
3230
import argparse
3331
from zipfile import ZipFile
3432

35-
3633
def get_zap_recommended_version():
3734
matter_root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.normpath('../../..')))
3835
zap_version_file = os.path.join(matter_root, 'scripts/tools/zap/zap_execution.py')
@@ -48,11 +45,11 @@ def get_zap_recommended_version():
4845
raise RuntimeError("Found multiple patterns matching ZAP_VERSION.")
4946

5047
return result[0]
48+
5149
except Exception as e:
5250
raise RuntimeError(
5351
f"Encountered problem when trying to read {zap_version_file} file. {e}")
5452

55-
5653
def get_zap_current_version():
5754
try:
5855
cmd_out = subprocess.check_output(['zap', '--version'])
@@ -69,7 +66,6 @@ def get_zap_current_version():
6966
print(f"ZAP file not found {e}")
7067
return None
7168

72-
7369
def download_recommended_zap_package(version, package_name, location):
7470
try:
7571
print("Trying to download ZAP tool package matching your system and recommended version.")
@@ -88,49 +84,55 @@ def download_recommended_zap_package(version, package_name, location):
8884
merged_version = f"{splitted_version[0]}.{month}.{day}"
8985

9086
url = f"https://github.com/project-chip/zap/releases/download/v{merged_version}-nightly/{package_name}.zip"
87+
9188
print(f"Downloading {url} into {os.path.join(location, f'{package_name}.zip')}")
9289
wget.download(url, out=location)
9390
print("\n")
91+
9492
except Exception as e:
9593
raise RuntimeError("Invalid URL to download ZAP tool package {}".format(e))
9694

97-
9895
def clear_old_artifacts(location, overwrite):
9996
if os.path.exists(location):
10097
# Ask for user consent if the overwrite flag was not provided
10198
if not overwrite:
10299
consent = input("The ZAP directory already exists in this location. Do you agree to overwrite it? Yes[y]/No[n]:")
103100
if consent.lower() != 'yes' and consent.lower() != 'y':
104101
raise RuntimeError("Couldn't download ZAP package, as the file already exists in this location.")
105-
shutil.rmtree(location)
106102

103+
shutil.rmtree(location)
107104

108105
def remove_zip(location, package_name):
109106
path = os.path.join(location, f"{package_name}.zip")
110107
print(f"Deleting zip file: {path}")
111108
os.remove(path)
112109

113-
114110
def set_executable(location, package_name, filename):
115111
file = os.path.join(location, package_name, filename)
116112
st = os.stat(file)
117113
os.chmod(file, st.st_mode | stat.S_IEXEC)
118114

119-
120115
def unzip_zap_package(location, package_name):
116+
package = location + f"/{package_name}.zip"
117+
destination = location + "/" + package_name
118+
121119
try:
122-
zip = ZipFile(os.path.join(location, f"{package_name}.zip"))
123-
zip.extractall(os.path.join(location, package_name))
124-
zip.close()
120+
if (platform.system() == 'Darwin'):
121+
subprocess.run(['unzip', package, '-d', destination], stdout = subprocess.PIPE)
122+
else:
123+
zip = ZipFile(os.path.join(location, f"{package_name}.zip"))
124+
zip.extractall(os.path.join(location, package_name))
125+
zip.close()
126+
125127
except Exception as e:
126128
raise RuntimeError("Encountered problem when trying to unzip the ZAP tool package. {}".format(e))
129+
127130
finally:
128131
remove_zip(location, package_name)
129132

130-
131133
def print_paths_warning(paths_to_print):
132-
messages = ["Please add the following location(s) to the system PATH:"] + paths_to_print
133134

135+
messages = ["Please add the following location(s) to the system PATH:"] + paths_to_print
134136
longest_message = max(messages, key=len)
135137

136138
for item in range(len(messages)):
@@ -143,23 +145,27 @@ def print_paths_warning(paths_to_print):
143145
print(f"\33[33m{message}\x1b[0m")
144146
print(f"\33[33m{frame_message}\x1b[0m")
145147

146-
147148
def install_zap_package(version, location, overwrite):
148149
current_os = platform.system()
150+
149151
if current_os == 'Linux':
150152
package = 'zap-linux-x64'
151153
zap_executable = 'zap'
152154
zap_cli_executable = 'zap-cli'
155+
153156
elif current_os == 'Windows':
154157
package = 'zap-win-x64'
155158
zap_executable = 'zap.exe'
156159
zap_cli_executable = 'zap-cli.exe'
160+
157161
elif current_os == 'Darwin':
158162
package = 'zap-mac-x64'
159163
zap_executable = 'zap.app/Contents/MacOS/zap'
160164
zap_cli_executable = 'zap-cli'
165+
161166
else:
162167
raise RuntimeError(f"Couldn't find the proper ZAP tool package for the currently used operating system: {current_os}")
168+
163169
clear_old_artifacts(os.path.join(location, package), overwrite)
164170
download_recommended_zap_package(version, package, location)
165171
unzip_zap_package(location, package)
@@ -168,19 +174,16 @@ def install_zap_package(version, location, overwrite):
168174

169175
print("ZAP tool package was downloaded and extracted in the given location.")
170176

177+
171178
if current_os == 'Darwin':
172179
print_paths_warning([os.path.join(location, package, zap_executable), os.path.join(location, package)])
173180
else:
174181
print_paths_warning([os.path.join(location, package)])
175182

176-
177183
def main():
178-
parser = argparse.ArgumentParser(
179-
description='Script helping to download the ZAP tool in the currently recommended revision.')
180-
parser.add_argument(
181-
"-l", "--location", help="Path to the location that should be used for storing ZAP tool package.", type=str, required=True)
182-
parser.add_argument(
183-
"-o", "--overwrite", help="Overwrite files without asking, in case they already exist in given location", action="store_true")
184+
parser = argparse.ArgumentParser(description='Script helping to download the ZAP tool in the currently recommended revision.')
185+
parser.add_argument("-l", "--location", help="Path to the location that should be used for storing ZAP tool package.", type=str, required=True)
186+
parser.add_argument("-o", "--overwrite", help="Overwrite files without asking, in case they already exist in given location", action="store_true")
184187
args = parser.parse_args()
185188

186189
location = os.path.abspath(args.location)
@@ -190,12 +193,14 @@ def main():
190193
if not zap_current_version:
191194
print("No ZAP tool version was found installed on this device.")
192195
install_zap_package(zap_recommended_version, location, args.overwrite)
196+
193197
elif zap_current_version == zap_recommended_version:
194198
print(f"Your currenly installed ZAP tool version: {zap_current_version} matches the recommended one.")
199+
195200
else:
196201
print(f"Your currenly installed ZAP tool version: {zap_current_version} does not match the recommended one: {zap_recommended_version}")
197202
install_zap_package(zap_recommended_version, location, args.overwrite)
198203

199-
200204
if __name__ == '__main__':
205+
201206
main()

0 commit comments

Comments
 (0)