| #!/usr/bin/env python |
| # Copyright 2023 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import argparse |
| import dataclasses |
| import enum |
| import json |
| import os |
| import re |
| import sys |
| import urllib.request |
| |
| _URL = ( |
| "https://github.com/project-chip/zap/releases/download/{version}/zap-{platform}.zip" |
| ) |
| |
| |
| class ZapVersion(enum.Enum): |
| RELEASE = 1 |
| PRERELEASE = 2 |
| |
| |
| # Which zap version to consider "latest". |
| _ZAP_VERSION = ZapVersion.PRERELEASE |
| |
| |
| def do_latest(): |
| """Retrieve current version. |
| """ |
| if _ZAP_VERSION == ZapVersion.RELEASE: |
| print( |
| json.load( |
| urllib.request.urlopen( |
| "https://api.github.com/repos/project-chip/zap/releases/latest" |
| ) |
| )["tag_name"] |
| ) |
| return |
| if _ZAP_VERSION == ZapVersion.PRERELEASE: |
| tags = json.load( |
| urllib.request.urlopen("https://api.github.com/repos/project-chip/zap/tags") |
| ) |
| assert len(tags) > 0, "Empty tags list" |
| # Tags look like "v2023.01.17-nightly", so sorting them will produce a |
| # chronological ordering. |
| sorted_tag_names = sorted(t["name"] for t in tags) |
| print(sorted_tag_names[-1]) |
| |
| |
| def get_download_url(version, platform): |
| """Get URL of the given version and platform.""" |
| |
| platform_name = { |
| "linux-amd64": "linux-x64", |
| "linux-arm64": "linux-arm64", |
| "mac-amd64": "mac-x64", |
| "mac-arm64": "mac-arm64", |
| "windows-amd64": "win-x64", |
| }[platform] |
| |
| url = _URL.format(version=version, platform=platform_name) |
| |
| manifest = {"url": [url], "ext": ".zip"} |
| print(json.dumps(manifest)) |
| |
| |
| def main(): |
| ap = argparse.ArgumentParser() |
| sub = ap.add_subparsers() |
| |
| latest = sub.add_parser("latest") |
| latest.set_defaults(func=lambda _opts: do_latest()) |
| |
| download = sub.add_parser("get_url") |
| download.set_defaults( |
| func=lambda opts: get_download_url( |
| os.environ["_3PP_VERSION"], os.environ["_3PP_PLATFORM"] |
| ) |
| ) |
| |
| opts = ap.parse_args() |
| return opts.func(opts) |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main()) |