Skip to content

Commit 3533608

Browse files
andy31415Copilotgemini-code-assist[bot]
authored
Support zap version CIPD uploads from CIPD releases. (project-chip#41173)
* Support zap version CIPD uploads from CIPD releases. * Update scripts/tools/zap/cipd_upload.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update scripts/tools/zap/cipd_upload.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Fix some restyle issues. * Minor restyle ... again * Update versioning to be the right content. * Addressing review comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 7ed954f commit 3533608

2 files changed

Lines changed: 129 additions & 25 deletions

File tree

scripts/tools/zap/cipd_upload.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
3+
import logging
4+
import os
5+
import shlex
6+
import subprocess
7+
import sys
8+
import tempfile
9+
10+
import click
11+
12+
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
13+
from zap_download import DownloadReleasedZap # noqa: E402 isort:skip
14+
15+
16+
try:
17+
import coloredlogs
18+
_has_coloredlogs = True
19+
except ImportError:
20+
_has_coloredlogs = False
21+
22+
# Supported log levels, mapping string values required for argument
23+
# parsing into logging constants
24+
__LOG_LEVELS__ = {
25+
'debug': logging.DEBUG,
26+
'info': logging.INFO,
27+
'warn': logging.WARN,
28+
'fatal': logging.FATAL,
29+
}
30+
31+
# A list of things to copy. Tuples of
32+
# (zap_platform, zap_architecture, cipd_platform)
33+
__ZAP_ARCHITECTURES__ = [
34+
('linux', 'arm64', 'linux-arm64'),
35+
('linux', 'x64', 'linux-amd64'),
36+
('mac', 'arm64', 'mac-arm64'),
37+
('mac', 'x64', 'mac-amd64'),
38+
('win', 'x64', 'windows-amd64'),
39+
]
40+
41+
42+
@click.command()
43+
@click.option(
44+
'--log-level',
45+
default='INFO',
46+
show_default=True,
47+
type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
48+
callback=lambda c, p, v: __LOG_LEVELS__[v],
49+
help='Determines the verbosity of script output')
50+
@click.option('--no-temp-clean', is_flag=True)
51+
@click.option('--version', help='Zap version to use', required=True)
52+
def main(log_level: str, version: str, no_temp_clean: bool):
53+
if _has_coloredlogs:
54+
coloredlogs.install(level=log_level, fmt='%(asctime)s %(name)s %(levelname)-7s %(message)s')
55+
else:
56+
logging.basicConfig(
57+
level=log_level,
58+
format='%(asctime)s %(name)s %(levelname)-7s %(message)s',
59+
datefmt='%Y-%m-%d %H:%M:%S'
60+
)
61+
62+
with tempfile.TemporaryDirectory(prefix="zap_", suffix="_cipd", delete=(not no_temp_clean)) as tmpdir:
63+
logging.info("Temporary Directory: %s", tmpdir)
64+
65+
for platform, arch, cipd_dir in __ZAP_ARCHITECTURES__:
66+
download_dir = f"zap-{platform}-{arch}"
67+
download_path = os.path.join(tmpdir, download_dir)
68+
logging.info("Downloading %s-%s into %s", platform, arch, download_path)
69+
DownloadReleasedZap(download_path, version, platform, arch)
70+
71+
# Release downloaded, create a CIPD definition and upload it
72+
cipd_def_file = f"cipd_{platform}_{arch}.yaml"
73+
with open(os.path.join(download_path, cipd_def_file), "wt") as f:
74+
f.write(f"package: experimental/matter/zap/{cipd_dir}\n")
75+
f.write(f"description: ZAP release {version} of {platform}-{arch}\n")
76+
f.write("install_mode: copy\n")
77+
f.write("data:\n")
78+
f.write(" - dir: .\n")
79+
80+
cmd = [
81+
"cipd",
82+
"create",
83+
f"-pkg-def={cipd_def_file}",
84+
"-tag",
85+
f"version:{version}.2",
86+
]
87+
logging.info("Creating CIPD: %s", shlex.join(cmd))
88+
89+
subprocess.check_call(cmd, cwd=download_path)
90+
91+
92+
if __name__ == '__main__':
93+
main()

scripts/tools/zap/zap_download.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,43 @@ def _SetupSourceZap(install_directory: str, zap_version: str):
9797
_ExecuteProcess("npm ci".split(), install_directory)
9898

9999

100-
def _SetupReleaseZap(install_directory: str, zap_version: str):
100+
def _GetDefaultPlatform():
101+
match sys.platform:
102+
case 'linux':
103+
return 'linux'
104+
case 'darwin':
105+
return 'mac'
106+
case 'win32':
107+
return 'win'
108+
case _:
109+
raise Exception('Unknown platform - do not know what zip file to download.')
110+
111+
112+
def _GetDefaultArch():
113+
arch = None
114+
115+
match sys.platform:
116+
case 'win32':
117+
# os.uname is not implemented on Windows, so use an alternative instead.
118+
import platform
119+
arch = platform.uname().machine
120+
case _:
121+
arch = os.uname().machine
122+
123+
if arch == 'x86_64' or arch == 'AMD64':
124+
return 'x64'
125+
126+
# this should be `arm64` ...
127+
return arch
128+
129+
130+
def DownloadReleasedZap(install_directory: str, zap_version: str, zap_platform: str, zap_arch: str):
101131
"""
102132
Downloads the given [zap_version] into "[install_directory]/zap-[zap_version]/".
103133
104134
Will download the given release from github releases.
105135
"""
106136

107-
if sys.platform == 'linux':
108-
zap_platform = 'linux'
109-
arch = os.uname().machine
110-
elif sys.platform == 'darwin':
111-
zap_platform = 'mac'
112-
arch = os.uname().machine
113-
elif sys.platform == 'win32':
114-
zap_platform = 'win'
115-
# os.uname is not implemented on Windows, so use an alternative instead.
116-
import platform
117-
arch = platform.uname().machine
118-
else:
119-
raise Exception('Unknown platform - do not know what zip file to download.')
120-
121-
if arch == 'arm64':
122-
zap_arch = 'arm64'
123-
elif arch == 'x86_64' or arch == 'AMD64':
124-
zap_arch = 'x64'
125-
else:
126-
raise Exception(f'Unknown architecture "${arch}" - do not know what zip file to download.')
127-
128137
url = f"https://github.com/project-chip/zap/releases/download/{zap_version}/zap-{zap_platform}-{zap_arch}.zip"
129138

130139
logging.info("Fetching: %s", url)
@@ -149,7 +158,7 @@ def _SetupReleaseZap(install_directory: str, zap_version: str):
149158
logging.info("Done extracting.")
150159

151160

152-
def _GetZapVersionToUse(project_root):
161+
def _GetZapVersionToUse(project_root) -> str:
153162
"""
154163
Heuristic to figure out what zap version should be used.
155164
@@ -212,7 +221,9 @@ def _GetZapVersionToUse(project_root):
212221
type=click.Choice(DownloadType.__members__, case_sensitive=False),
213222
callback=lambda c, p, v: getattr(DownloadType, v),
214223
help='What type of zap download to perform')
215-
def main(log_level: str, sdk_root: str, extract_root: str, zap_version: Optional[str], zap: DownloadType):
224+
@click.option('--platform', default=_GetDefaultPlatform(), show_default=True, help='ZAP Platform to download')
225+
@click.option('--arch', default=_GetDefaultArch(), show_default=True, help='ZAP Architecture to download')
226+
def main(log_level: str, sdk_root: str, extract_root: str, zap_version: Optional[str], zap: DownloadType, platform: str, arch: str):
216227
if _has_coloredlogs:
217228
coloredlogs.install(level=log_level, fmt='%(asctime)s %(name)s %(levelname)-7s %(message)s')
218229
else:
@@ -245,7 +256,7 @@ def main(log_level: str, sdk_root: str, extract_root: str, zap_version: Optional
245256
# Make sure the results can be used in scripts
246257
print(f"{export_cmd} ZAP_DEVELOPMENT_PATH={shlex.quote(install_directory)}")
247258
else:
248-
_SetupReleaseZap(install_directory, zap_version)
259+
DownloadReleasedZap(install_directory, zap_version, platform, arch)
249260

250261
# Make sure the results can be used in scripts
251262
print(f"{export_cmd} ZAP_INSTALL_PATH={shlex.quote(install_directory)}")

0 commit comments

Comments
 (0)