Skip to content

Commit 9879273

Browse files
markaj-nordicpull[bot]
authored andcommitted
[scripts] created bootstrap script for ZAP (#22757)
Extracted bootstrap code from run_zaptool.sh and reuse it in other Python scripts. This allows to run ZAP convert/generate tools out of the box without necessity to run zaptool beforehand. Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no> Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
1 parent db26fda commit 9879273

File tree

6 files changed

+126
-20
lines changed

6 files changed

+126
-20
lines changed

scripts/tools/zap/convert.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ def runArgumentsParser():
6060
parser = argparse.ArgumentParser(
6161
description='Convert .zap files to the current zap version')
6262
parser.add_argument('zap', help='Path to the application .zap file')
63+
parser.add_argument('--run-bootstrap', default=None, action='store_true',
64+
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
6365
args = parser.parse_args()
6466

6567
zap_file = getFilePath(args.zap)
6668

67-
return zap_file
69+
return zap_file, args.run_bootstrap
6870

6971

7072
def detectZclFile(zapFile):
@@ -96,11 +98,17 @@ def runConversion(zap_file):
9698
'-z', zcl_file, '-g', templates_file, '-o', zap_file, zap_file])
9799

98100

101+
def runBootstrap():
102+
subprocess.check_call(getFilePath("scripts/tools/zap/zap_bootstrap.sh"), shell=True)
103+
104+
99105
def main():
100106
checkPythonVersion()
107+
zap_file, run_bootstrap = runArgumentsParser()
108+
if run_bootstrap:
109+
runBootstrap()
101110
os.chdir(CHIP_ROOT_DIR)
102111

103-
zap_file = runArgumentsParser()
104112
runConversion(zap_file)
105113

106114

scripts/tools/zap/generate.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
import subprocess
2323
import sys
2424
import urllib.request
25+
from dataclasses import dataclass
26+
27+
28+
@dataclass
29+
class CmdLineArgs:
30+
zapFile: str
31+
zclFile: str
32+
templateFile: str
33+
outputDir: str
34+
runBootstrap: bool
35+
2536

2637
CHIP_ROOT_DIR = os.path.realpath(
2738
os.path.join(os.path.dirname(__file__), '../../..'))
@@ -77,7 +88,7 @@ def detectZclFile(zapFile):
7788
return getFilePath(path)
7889

7990

80-
def runArgumentsParser():
91+
def runArgumentsParser() -> CmdLineArgs:
8192
default_templates = 'src/app/zap-templates/app-templates.json'
8293
default_output_dir = 'zap-generated/'
8394

@@ -90,6 +101,8 @@ def runArgumentsParser():
90101
help='Path to the zcl templates records to use for generating artifacts (default: autodetect read from zap file)')
91102
parser.add_argument('-o', '--output-dir', default=None,
92103
help='Output directory for the generated files (default: automatically selected)')
104+
parser.add_argument('--run-bootstrap', default=None, action='store_true',
105+
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
93106
args = parser.parse_args()
94107

95108
# By default, this script assumes that the global CHIP template is used with
@@ -113,7 +126,7 @@ def runArgumentsParser():
113126
templates_file = getFilePath(args.templates)
114127
output_dir = getDirPath(output_dir)
115128

116-
return (zap_file, zcl_file, templates_file, output_dir)
129+
return CmdLineArgs(zap_file, zcl_file, templates_file, output_dir, args.run_bootstrap)
117130

118131

119132
def extractGeneratedIdl(output_dir, zap_config_path):
@@ -209,21 +222,26 @@ def runJavaPrettifier(templates_file, output_dir):
209222
print('google-java-format error:', err)
210223

211224

225+
def runBootstrap():
226+
subprocess.check_call(getFilePath("scripts/tools/zap/zap_bootstrap.sh"), shell=True)
227+
228+
212229
def main():
213230
checkPythonVersion()
214-
215-
# The maximum meory usage is over 4GB (#15620)
231+
cmdLineArgs = runArgumentsParser()
232+
if cmdLineArgs.runBootstrap:
233+
runBootstrap()
234+
# The maximum memory usage is over 4GB (#15620)
216235
os.environ["NODE_OPTIONS"] = "--max-old-space-size=8192"
217-
zap_file, zcl_file, templates_file, output_dir = runArgumentsParser()
218-
runGeneration(zap_file, zcl_file, templates_file, output_dir)
236+
runGeneration(cmdLineArgs.zapFile, cmdLineArgs.zclFile, cmdLineArgs.templateFile, cmdLineArgs.outputDir)
219237

220238
prettifiers = [
221239
runClangPrettifier,
222240
runJavaPrettifier,
223241
]
224242

225243
for prettifier in prettifiers:
226-
prettifier(templates_file, output_dir)
244+
prettifier(cmdLineArgs.templateFile, cmdLineArgs.outputDir)
227245

228246

229247
if __name__ == '__main__':

scripts/tools/zap/run_zaptool.sh

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,9 @@ CHIP_ROOT="${SCRIPT_PATH%/scripts/tools/zap/run_zaptool.sh}"
3333

3434
(
3535

36-
cd "$CHIP_ROOT" &&
37-
git submodule update --init third_party/zap/repo
36+
"$CHIP_ROOT"/scripts/tools/zap/zap_bootstrap.sh
3837

39-
cd "third_party/zap/repo"
40-
if ! npm list installed-check &>/dev/null; then
41-
npm install installed-check
42-
fi
43-
44-
if ! ./node_modules/.bin/installed-check -c &>/dev/null; then
45-
npm install
46-
fi
38+
cd "$CHIP_ROOT/third_party/zap/repo"
4739

4840
echo "ARGS: ${ZAP_ARGS[@]}"
4941

scripts/tools/zap/zap_bootstrap.sh

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
#
4+
# Copyright (c) 2022 Project CHIP Authors
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
function _get_fullpath() {
20+
cd "$(dirname "$1")" && echo "$PWD/$(basename "$1")"
21+
}
22+
23+
function _usage() {
24+
cat <<EOF
25+
Invalid arguments passed.
26+
Usage:
27+
zap_bootstrap.sh -> install and update required packages
28+
zap_bootstrap.sh -c -> run a clean bootstrap, install packages from scratch
29+
EOF
30+
exit 1
31+
}
32+
33+
set -e
34+
35+
SCRIPT_PATH="$(_get_fullpath "$0")"
36+
CHIP_ROOT="${SCRIPT_PATH%/scripts/tools/zap/zap_bootstrap.sh}"
37+
38+
(
39+
cd "$CHIP_ROOT" &&
40+
git submodule update --init third_party/zap/repo
41+
42+
cd "third_party/zap/repo"
43+
44+
if [ $# -eq 0 ]; then
45+
echo "Running ZAP bootstrap"
46+
if ! npm list installed-check &>/dev/null; then
47+
npm install installed-check
48+
fi
49+
50+
if ! ./node_modules/.bin/installed-check -c &>/dev/null; then
51+
npm install
52+
fi
53+
elif [ $# -eq 1 ] && [ "$1" = "-c" ]; then
54+
echo "Running clean ZAP bootstrap"
55+
npm ci
56+
npm run version-stamp
57+
npm rebuild canvas --update-binary
58+
npm run build-spa
59+
else
60+
_usage
61+
fi
62+
)
63+
64+
echo "ZAP bootstrap done!"

scripts/tools/zap_convert_all.py

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pathlib import Path
2020
import sys
2121
import subprocess
22+
import argparse
2223

2324
CHIP_ROOT_DIR = os.path.realpath(
2425
os.path.join(os.path.dirname(__file__), '../..'))
@@ -53,8 +54,23 @@ def getTargets():
5354
return targets
5455

5556

57+
def runArgumentsParser():
58+
parser = argparse.ArgumentParser(
59+
description='Convert all .zap files to the current zap version')
60+
parser.add_argument('--run-bootstrap', default=None, action='store_true',
61+
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
62+
return parser.parse_args()
63+
64+
65+
def runBootstrap():
66+
subprocess.check_call(os.path.join(CHIP_ROOT_DIR, "scripts/tools/zap/zap_bootstrap.sh"), shell=True)
67+
68+
5669
def main():
70+
args = runArgumentsParser()
5771
checkPythonVersion()
72+
if args.run_bootstrap:
73+
runBootstrap()
5874
os.chdir(CHIP_ROOT_DIR)
5975

6076
targets = getTargets()

scripts/tools/zap_regen_all.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def setupArgumentsParser():
9393
parser.add_argument('--tests', default='all', choices=['all', 'chip-tool', 'darwin-framework-tool', 'app1', 'app2'],
9494
help='When generating tests only target, Choose which tests to generate (default: all)')
9595
parser.add_argument('--dry-run', default=False, action='store_true',
96-
help="Don't do any generationl just log what targets would be generated (default: False)")
96+
help="Don't do any generation, just log what targets would be generated (default: False)")
97+
parser.add_argument('--run-bootstrap', default=None, action='store_true',
98+
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
9799
return parser.parse_args()
98100

99101

@@ -224,6 +226,10 @@ def getTargets(type, test_target):
224226
return targets
225227

226228

229+
def runBootstrap():
230+
subprocess.check_call(os.path.join(CHIP_ROOT_DIR, "scripts/tools/zap/zap_bootstrap.sh"), shell=True)
231+
232+
227233
def main():
228234
logging.basicConfig(
229235
level=logging.INFO,
@@ -236,6 +242,8 @@ def main():
236242
targets = getTargets(args.type, args.tests)
237243

238244
if (not args.dry_run):
245+
if (args.run_bootstrap):
246+
runBootstrap()
239247
for target in targets:
240248
target.generate()
241249

0 commit comments

Comments
 (0)