Hal Canary | 82e3afa | 2019-08-19 15:04:24 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | # Copyright 2018 Google Inc. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import hashlib |
| 8 | import multiprocessing |
| 9 | import os |
| 10 | import shutil |
| 11 | import sys |
| 12 | import tempfile |
| 13 | import urllib2 |
| 14 | |
| 15 | def checksum(path): |
| 16 | if not os.path.exists(path): |
| 17 | return None |
| 18 | m = hashlib.md5() |
| 19 | with open(path, 'rb') as f: |
| 20 | while True: |
| 21 | buf = f.read(4096) |
| 22 | if not buf: |
| 23 | return m.hexdigest() |
| 24 | m.update(buf) |
| 25 | |
| 26 | def download(md5, path): |
| 27 | if not md5 == checksum(path): |
| 28 | dirname = os.path.dirname(path) |
| 29 | if dirname and not os.path.exists(dirname): |
| 30 | try: |
| 31 | os.makedirs(dirname) |
| 32 | except OSError: |
| 33 | pass # ignore race condition |
| 34 | url = 'https://storage.googleapis.com/skia-skqp-assets/' + md5 |
| 35 | with open(path, 'wb') as o: |
| 36 | shutil.copyfileobj(urllib2.urlopen(url), o) |
| 37 | |
| 38 | def tmp(prefix): |
| 39 | fd, path = tempfile.mkstemp(prefix=prefix) |
| 40 | os.close(fd) |
| 41 | return path |
| 42 | |
| 43 | def main(): |
| 44 | target_dir = os.path.join('platform_tools', 'android', 'apps', 'skqp', 'src', 'main', 'assets') |
| 45 | os.chdir(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, target_dir)) |
| 46 | checksum_path = 'files.checksum' |
| 47 | if not os.path.isfile(checksum_path): |
| 48 | sys.stderr.write('Error: "%s" is missing.\n' % os.path.join(target_dir, checksum_path)) |
| 49 | sys.exit(1) |
| 50 | file_list_file = tmp('files_') |
| 51 | with open(checksum_path, 'r') as f: |
| 52 | md5 = f.read().strip() |
| 53 | assert(len(md5) == 32) |
| 54 | download(md5, file_list_file) |
| 55 | with open(file_list_file, 'r') as f: |
| 56 | records = [] |
| 57 | for line in f: |
| 58 | md5, path = line.strip().split(';', 1) |
| 59 | records.append((md5, path)) |
| 60 | sys.stderr.write('Downloading %d files.\n' % len(records)) |
| 61 | pool = multiprocessing.Pool(processes=multiprocessing.cpu_count() * 2) |
| 62 | for record in records: |
| 63 | pool.apply_async(download, record, callback=lambda x: sys.stderr.write('.')) |
| 64 | pool.close() |
| 65 | pool.join() |
| 66 | sys.stderr.write('\n') |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | main() |