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 |
| 8 | import os |
| 9 | import shutil |
| 10 | import sys |
| 11 | import threading |
| 12 | import urllib2 |
| 13 | |
| 14 | def checksum(path): |
| 15 | if not os.path.exists(path): |
| 16 | return None |
| 17 | m = hashlib.md5() |
| 18 | with open(path, 'rb') as f: |
| 19 | while True: |
| 20 | buf = f.read(4096) |
| 21 | if 0 == len(buf): |
| 22 | return m.hexdigest() |
| 23 | m.update(buf) |
| 24 | |
| 25 | def download(md5, path): |
| 26 | if not md5 == checksum(path): |
| 27 | dirname = os.path.dirname(path) |
| 28 | if dirname and not os.path.exists(dirname): |
| 29 | try: |
| 30 | os.makedirs(dirname) |
| 31 | except: |
| 32 | # ignore race condition |
| 33 | if not os.path.exists(dirname): |
| 34 | raise |
| 35 | url = 'https://storage.googleapis.com/skia-skqp-assets/' + md5 |
| 36 | with open(path, 'wb') as o: |
| 37 | shutil.copyfileobj(urllib2.urlopen(url), o) |
| 38 | |
| 39 | def main(): |
| 40 | os.chdir(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
| 41 | 'platform_tools', 'android', 'apps', 'skqp', 'src', |
| 42 | 'main', 'assets')) |
| 43 | sys.stderr.write('\n 0 / ???? ') |
| 44 | with open('files.checksum', 'r') as f: |
| 45 | md5 = f.read().strip() |
| 46 | assert(len(md5) == 32) |
| 47 | download(md5, 'files.txt') |
| 48 | with open('files.txt', 'r') as f: |
| 49 | records = [] |
| 50 | for line in f: |
| 51 | md5, path = line.strip().split(';', 1) |
| 52 | records.append((md5, path)) |
| 53 | threads = set() |
| 54 | sys.stderr.write('\r 0 / %d ' % len(records)) |
| 55 | for i, record in enumerate(records): |
| 56 | t = threading.Thread(target=download, args=record) |
| 57 | t.start() |
| 58 | threads.add(t) |
| 59 | left = -1 |
| 60 | while left != 0: |
| 61 | count = sum(1 for t in threading.enumerate() if t in threads) |
| 62 | if left != count: |
| 63 | left = count |
| 64 | sys.stderr.write('\r %4d / %d ' % (len(records) - left, len(records))) |
| 65 | sys.stderr.write('\n') |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | main() |