Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [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 |
Hal Canary | 5f73c21 | 2018-02-05 11:36:25 -0500 | [diff] [blame] | 8 | import multiprocessing |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 9 | import os |
| 10 | import shutil |
| 11 | import sys |
Hal Canary | ce243ba | 2018-01-30 16:08:17 -0500 | [diff] [blame] | 12 | import tempfile |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 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 0 == len(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: |
| 33 | # ignore race condition |
| 34 | if not os.path.exists(dirname): |
| 35 | raise |
| 36 | url = 'https://storage.googleapis.com/skia-skqp-assets/' + md5 |
| 37 | with open(path, 'wb') as o: |
| 38 | shutil.copyfileobj(urllib2.urlopen(url), o) |
| 39 | |
Hal Canary | ce243ba | 2018-01-30 16:08:17 -0500 | [diff] [blame] | 40 | def tmp(prefix): |
| 41 | fd, path = tempfile.mkstemp(prefix=prefix) |
| 42 | os.close(fd) |
| 43 | return path |
| 44 | |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 45 | def main(): |
Hal Canary | 703d9c4 | 2018-02-05 15:58:03 -0500 | [diff] [blame] | 46 | target_dir = os.path.join('platform_tools', 'android', 'apps', 'skqp', 'src', 'main', 'assets') |
| 47 | os.chdir(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, target_dir)) |
| 48 | checksum_path = 'files.checksum' |
| 49 | if not os.path.isfile(checksum_path): |
| 50 | sys.stderr.write('Error: "%s" is missing.\n' % os.path.join(target_dir, checksum_path)) |
| 51 | sys.exit(1) |
Hal Canary | ce243ba | 2018-01-30 16:08:17 -0500 | [diff] [blame] | 52 | file_list_file = tmp('files_') |
Hal Canary | 703d9c4 | 2018-02-05 15:58:03 -0500 | [diff] [blame] | 53 | with open(checksum_path, 'r') as f: |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 54 | md5 = f.read().strip() |
| 55 | assert(len(md5) == 32) |
Hal Canary | ce243ba | 2018-01-30 16:08:17 -0500 | [diff] [blame] | 56 | download(md5, file_list_file) |
| 57 | with open(file_list_file, 'r') as f: |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 58 | records = [] |
| 59 | for line in f: |
| 60 | md5, path = line.strip().split(';', 1) |
| 61 | records.append((md5, path)) |
Hal Canary | 5f73c21 | 2018-02-05 11:36:25 -0500 | [diff] [blame] | 62 | sys.stderr.write('Downloading %d files.\n' % len(records)) |
| 63 | pool = multiprocessing.Pool(processes=multiprocessing.cpu_count() * 2) |
| 64 | for record in records: |
| 65 | pool.apply_async(download, record, callback=lambda x: sys.stderr.write('.')) |
| 66 | pool.close() |
| 67 | pool.join() |
Hal Canary | 181ec2f | 2018-01-24 13:42:38 -0500 | [diff] [blame] | 68 | sys.stderr.write('\n') |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main() |