aboutsummaryrefslogtreecommitdiff
path: root/tools/download-yajsv.py
blob: d42d3f3ab6c7a33b82a0450d67693910a9e7d0b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
This script is used to download YAJSV (yet another json schema validator). It
runs as a gclient hook.
"""

import argparse
import curlish
import os
import stat
import sys

RELEASES_DOWNLOAD_URL = 'https://github.com/neilpa/yajsv/releases/download/'
VERSION = 'v1.4.0'
YAJSV_FLAVOR_DICT = {
    'linux32': 'yajsv.linux.386',
    'linux64': 'yajsv.linux.amd64',
    'mac64': 'yajsv.darwin.amd64'
}

PLATFORM_MAP = {'linux2': 'linux', 'darwin': 'mac'}


def get_bitness():
    # According to the python docs, this is more reliable than
    # querying platform.architecture().
    if sys.maxsize > 2**32:
        return '64'
    return '32'


def get_platform():
    return PLATFORM_MAP.get(sys.platform, sys.platform)


def get_flavor():
    return "{}{}".format(get_platform(), get_bitness())


def main():
    parser = argparse.ArgumentParser(description='Download a YAJSV release.')
    parser.add_argument('--flavor',
                        help='Flavor to download (currently one of {})'.format(
                            ', '.join(YAJSV_FLAVOR_DICT.keys())))
    args = parser.parse_args()

    flavor = args.flavor
    if not flavor:
        flavor = get_flavor()
        if flavor in YAJSV_FLAVOR_DICT:
            print('flavor not provided, defaulting to ' + flavor)

    if flavor not in YAJSV_FLAVOR_DICT:
        print('could not find an appropriate flavor, "{}" is invalid'.format(
            flavor))
        return 1

    output_path = os.path.abspath(
        os.path.join(os.path.dirname(os.path.relpath(__file__)), 'yajsv'))
    download_url = '{}{}/{}'.format(RELEASES_DOWNLOAD_URL, VERSION,
                                    YAJSV_FLAVOR_DICT[flavor])
    result = curlish.curlish(download_url, output_path)

    # YAJSV isn't useful if it's not executable.
    if result:
        current_mode = os.stat(output_path).st_mode
        os.chmod(output_path, current_mode | stat.S_IEXEC)

    return 0 if result else 1


if __name__ == '__main__':
    sys.exit(main())