1#!/usr/bin/env python3 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 platform 14 15if sys.version_info[0] < 3: 16 from urllib2 import urlopen 17else: 18 from urllib.request import urlopen 19 20os.chdir(os.path.join(os.path.dirname(__file__), os.pardir)) 21 22def fetch(target): 23 deps_path = 'buildtools/DEPS' 24 try: 25 with open(deps_path, 'r') as contents: 26 deps_globals = {} 27 exec(contents.read(), deps_globals) 28 deps = deps_globals['deps'] 29 except: 30 print(deps_path, 'could not be read/parsed. Did you run `tools/git-sync-deps`?') 31 exit(1) 32 33 if 'arm' in platform.processor(): 34 if 'darwin' in sys.platform: 35 obj = 'mac_arm64-format' 36 target_path = 'buildtools/mac_arm64/' 37 else: 38 print('unsupported platform') 39 exit(1) 40 else: 41 if 'linux' in sys.platform: 42 obj = 'linux64-format' 43 target_path = 'buildtools/linux64/' 44 elif 'darwin' in sys.platform: 45 obj = 'mac-format' 46 target_path = 'buildtools/mac/clang-format' 47 else: 48 obj = 'win-format' 49 target_path = 'buildtools\\win\\' 50 51 sha256sum = deps[obj]['objects'][0]['sha256sum'] 52 object_name = deps[obj]['objects'][0]['object_name'] 53 output_file = os.path.join('bin', deps[obj]['objects'][0]['output_file']) 54 target_path = os.path.join(target_path, deps[obj]['objects'][0]['output_file']) 55 56 def sha256_of_file(path): 57 h = hashlib.sha256() 58 if os.path.isfile(path): 59 with open(path, 'rb') as f: 60 h.update(f.read()) 61 return h.hexdigest() 62 63 if sha256_of_file(output_file) != sha256sum: 64 with open(output_file, 'wb') as f: 65 url = 'https://chromium-%s.storage-download.googleapis.com/%s' % (target, object_name) 66 f.write(urlopen(url).read()) 67 68 os.chmod(output_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | 69 stat.S_IRGRP | stat.S_IXGRP | 70 stat.S_IROTH | stat.S_IXOTH ) 71 if sha256_of_file(output_file) != sha256sum: 72 print(output_file, 'was corrupted during download. Please try again.') 73 exit(1) 74 75 if sha256_of_file(target_path) != sha256sum: 76 shutil.copy(output_file, target_path) 77 78fetch('clang-format') 79