1
1
#!/usr/bin/env python3
2
-
3
2
#
4
- # Copyright (c) 2023 Project CHIP Authors
3
+ # Copyright (c) 2024 Project CHIP Authors
5
4
#
6
5
# Licensed under the Apache License, Version 2.0 (the "License");
7
6
# you may not use this file except in compliance with the License.
15
14
# See the License for the specific language governing permissions and
16
15
# limitations under the License.
17
16
#
18
-
19
17
# The script parses integrations/docker/images/chip-build/Dockerfile file
20
18
# from the Matter repository and looks for ENV ZAP_VERSION= string to find
21
19
# currently recommended ZAP version. After that the package matching this version
32
30
import argparse
33
31
from zipfile import ZipFile
34
32
35
-
36
33
def get_zap_recommended_version ():
37
34
matter_root = os .path .abspath (os .path .join (os .path .dirname (__file__ ), os .path .normpath ('../../..' )))
38
35
zap_version_file = os .path .join (matter_root , 'scripts/tools/zap/zap_execution.py' )
@@ -48,11 +45,11 @@ def get_zap_recommended_version():
48
45
raise RuntimeError ("Found multiple patterns matching ZAP_VERSION." )
49
46
50
47
return result [0 ]
48
+
51
49
except Exception as e :
52
50
raise RuntimeError (
53
51
f"Encountered problem when trying to read { zap_version_file } file. { e } " )
54
52
55
-
56
53
def get_zap_current_version ():
57
54
try :
58
55
cmd_out = subprocess .check_output (['zap' , '--version' ])
@@ -69,7 +66,6 @@ def get_zap_current_version():
69
66
print (f"ZAP file not found { e } " )
70
67
return None
71
68
72
-
73
69
def download_recommended_zap_package (version , package_name , location ):
74
70
try :
75
71
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):
88
84
merged_version = f"{ splitted_version [0 ]} .{ month } .{ day } "
89
85
90
86
url = f"https://github.com/project-chip/zap/releases/download/v{ merged_version } -nightly/{ package_name } .zip"
87
+
91
88
print (f"Downloading { url } into { os .path .join (location , f'{ package_name } .zip' )} " )
92
89
wget .download (url , out = location )
93
90
print ("\n " )
91
+
94
92
except Exception as e :
95
93
raise RuntimeError ("Invalid URL to download ZAP tool package {}" .format (e ))
96
94
97
-
98
95
def clear_old_artifacts (location , overwrite ):
99
96
if os .path .exists (location ):
100
97
# Ask for user consent if the overwrite flag was not provided
101
98
if not overwrite :
102
99
consent = input ("The ZAP directory already exists in this location. Do you agree to overwrite it? Yes[y]/No[n]:" )
103
100
if consent .lower () != 'yes' and consent .lower () != 'y' :
104
101
raise RuntimeError ("Couldn't download ZAP package, as the file already exists in this location." )
105
- shutil .rmtree (location )
106
102
103
+ shutil .rmtree (location )
107
104
108
105
def remove_zip (location , package_name ):
109
106
path = os .path .join (location , f"{ package_name } .zip" )
110
107
print (f"Deleting zip file: { path } " )
111
108
os .remove (path )
112
109
113
-
114
110
def set_executable (location , package_name , filename ):
115
111
file = os .path .join (location , package_name , filename )
116
112
st = os .stat (file )
117
113
os .chmod (file , st .st_mode | stat .S_IEXEC )
118
114
119
-
120
115
def unzip_zap_package (location , package_name ):
116
+ package = location + f"/{ package_name } .zip"
117
+ destination = location + "/" + package_name
118
+
121
119
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
+
125
127
except Exception as e :
126
128
raise RuntimeError ("Encountered problem when trying to unzip the ZAP tool package. {}" .format (e ))
129
+
127
130
finally :
128
131
remove_zip (location , package_name )
129
132
130
-
131
133
def print_paths_warning (paths_to_print ):
132
- messages = ["Please add the following location(s) to the system PATH:" ] + paths_to_print
133
134
135
+ messages = ["Please add the following location(s) to the system PATH:" ] + paths_to_print
134
136
longest_message = max (messages , key = len )
135
137
136
138
for item in range (len (messages )):
@@ -143,23 +145,27 @@ def print_paths_warning(paths_to_print):
143
145
print (f"\33 [33m{ message } \x1b [0m" )
144
146
print (f"\33 [33m{ frame_message } \x1b [0m" )
145
147
146
-
147
148
def install_zap_package (version , location , overwrite ):
148
149
current_os = platform .system ()
150
+
149
151
if current_os == 'Linux' :
150
152
package = 'zap-linux-x64'
151
153
zap_executable = 'zap'
152
154
zap_cli_executable = 'zap-cli'
155
+
153
156
elif current_os == 'Windows' :
154
157
package = 'zap-win-x64'
155
158
zap_executable = 'zap.exe'
156
159
zap_cli_executable = 'zap-cli.exe'
160
+
157
161
elif current_os == 'Darwin' :
158
162
package = 'zap-mac-x64'
159
163
zap_executable = 'zap.app/Contents/MacOS/zap'
160
164
zap_cli_executable = 'zap-cli'
165
+
161
166
else :
162
167
raise RuntimeError (f"Couldn't find the proper ZAP tool package for the currently used operating system: { current_os } " )
168
+
163
169
clear_old_artifacts (os .path .join (location , package ), overwrite )
164
170
download_recommended_zap_package (version , package , location )
165
171
unzip_zap_package (location , package )
@@ -168,19 +174,16 @@ def install_zap_package(version, location, overwrite):
168
174
169
175
print ("ZAP tool package was downloaded and extracted in the given location." )
170
176
177
+
171
178
if current_os == 'Darwin' :
172
179
print_paths_warning ([os .path .join (location , package , zap_executable ), os .path .join (location , package )])
173
180
else :
174
181
print_paths_warning ([os .path .join (location , package )])
175
182
176
-
177
183
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" )
184
187
args = parser .parse_args ()
185
188
186
189
location = os .path .abspath (args .location )
@@ -190,12 +193,14 @@ def main():
190
193
if not zap_current_version :
191
194
print ("No ZAP tool version was found installed on this device." )
192
195
install_zap_package (zap_recommended_version , location , args .overwrite )
196
+
193
197
elif zap_current_version == zap_recommended_version :
194
198
print (f"Your currenly installed ZAP tool version: { zap_current_version } matches the recommended one." )
199
+
195
200
else :
196
201
print (f"Your currenly installed ZAP tool version: { zap_current_version } does not match the recommended one: { zap_recommended_version } " )
197
202
install_zap_package (zap_recommended_version , location , args .overwrite )
198
203
199
-
200
204
if __name__ == '__main__' :
205
+
201
206
main ()
0 commit comments