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