• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2020 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""
6This script is used to download YAJSV (yet another json schema validator). It
7runs as a gclient hook.
8"""
9
10import argparse
11import curlish
12import os
13import stat
14import sys
15
16RELEASES_DOWNLOAD_URL = 'https://github.com/neilpa/yajsv/releases/download/'
17VERSION = 'v1.4.0'
18YAJSV_FLAVOR_DICT = {
19    'linux32': 'yajsv.linux.386',
20    'linux64': 'yajsv.linux.amd64',
21    'mac64': 'yajsv.darwin.amd64'
22}
23
24PLATFORM_MAP = {'linux2': 'linux', 'darwin': 'mac'}
25
26
27def get_bitness():
28    # According to the python docs, this is more reliable than
29    # querying platform.architecture().
30    if sys.maxsize > 2**32:
31        return '64'
32    return '32'
33
34
35def get_platform():
36    return PLATFORM_MAP.get(sys.platform, sys.platform)
37
38
39def get_flavor():
40    return "{}{}".format(get_platform(), get_bitness())
41
42
43def main():
44    parser = argparse.ArgumentParser(description='Download a YAJSV release.')
45    parser.add_argument('--flavor',
46                        help='Flavor to download (currently one of {})'.format(
47                            ', '.join(YAJSV_FLAVOR_DICT.keys())))
48    args = parser.parse_args()
49
50    flavor = args.flavor
51    if not flavor:
52        flavor = get_flavor()
53        if flavor in YAJSV_FLAVOR_DICT:
54            print('flavor not provided, defaulting to ' + flavor)
55
56    if flavor not in YAJSV_FLAVOR_DICT:
57        print('could not find an appropriate flavor, "{}" is invalid'.format(
58            flavor))
59        return 1
60
61    output_path = os.path.abspath(
62        os.path.join(os.path.dirname(os.path.relpath(__file__)), 'yajsv'))
63    download_url = '{}{}/{}'.format(RELEASES_DOWNLOAD_URL, VERSION,
64                                    YAJSV_FLAVOR_DICT[flavor])
65    result = curlish.curlish(download_url, output_path)
66
67    # YAJSV isn't useful if it's not executable.
68    if result:
69        current_mode = os.stat(output_path).st_mode
70        os.chmod(output_path, current_mode | stat.S_IEXEC)
71
72    return 0 if result else 1
73
74
75if __name__ == '__main__':
76    sys.exit(main())
77