1#!/usr/bin/env python
2
3# Copyright 2017 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8import hashlib
9import os
10import shutil
11import stat
12import sys
13import urllib2
14
15os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
16
17def fetch(target):
18  target_path = 'buildtools/linux64/' + target if 'linux'  in sys.platform else \
19                'buildtools/mac/' + target     if 'darwin' in sys.platform else \
20                'buildtools/win/'+ target + '.exe'
21
22  sha1_path = target_path + '.sha1'
23  if not os.path.exists(sha1_path):
24    print sha1_path, 'is missing. Did you run `tools/git-sync-deps`?'
25    exit(1)
26  sha1 = open(sha1_path).read().strip()
27
28  def sha1_of_file(path):
29    h = hashlib.sha1()
30    if os.path.isfile(path):
31      with open(path, 'rb') as f:
32        h.update(f.read())
33    return h.hexdigest()
34
35  if sha1_of_file(target_path) != sha1:
36    with open(target_path, 'wb') as f:
37      url = 'https://chromium-%s.storage-download.googleapis.com/%s' % (target, sha1)
38      f.write(urllib2.urlopen(url).read())
39
40    os.chmod(target_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
41                          stat.S_IRGRP                | stat.S_IXGRP |
42                          stat.S_IROTH                | stat.S_IXOTH )
43
44  target_copy_path = os.path.join('bin', os.path.basename(target_path))
45  if sha1_of_file(target_copy_path) != sha1:
46    shutil.copy(target_path, target_copy_path)
47
48fetch('clang-format')
49