blob: c18ebfa428b9bad109585469e01a11f5a9ead220 [file] [log] [blame]
Hal Canary181ec2f2018-01-24 13:42:38 -05001#! /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
7import hashlib
Hal Canary5f73c212018-02-05 11:36:25 -05008import multiprocessing
Hal Canary181ec2f2018-01-24 13:42:38 -05009import os
10import shutil
11import sys
Hal Canaryce243ba2018-01-30 16:08:17 -050012import tempfile
Hal Canary181ec2f2018-01-24 13:42:38 -050013import urllib2
14
15def 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
26def 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 Canaryce243ba2018-01-30 16:08:17 -050040def tmp(prefix):
41 fd, path = tempfile.mkstemp(prefix=prefix)
42 os.close(fd)
43 return path
44
Hal Canary181ec2f2018-01-24 13:42:38 -050045def main():
46 os.chdir(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
47 'platform_tools', 'android', 'apps', 'skqp', 'src',
48 'main', 'assets'))
Hal Canaryce243ba2018-01-30 16:08:17 -050049 file_list_file = tmp('files_')
Hal Canary181ec2f2018-01-24 13:42:38 -050050 with open('files.checksum', 'r') as f:
51 md5 = f.read().strip()
52 assert(len(md5) == 32)
Hal Canaryce243ba2018-01-30 16:08:17 -050053 download(md5, file_list_file)
54 with open(file_list_file, 'r') as f:
Hal Canary181ec2f2018-01-24 13:42:38 -050055 records = []
56 for line in f:
57 md5, path = line.strip().split(';', 1)
58 records.append((md5, path))
Hal Canary5f73c212018-02-05 11:36:25 -050059 sys.stderr.write('Downloading %d files.\n' % len(records))
60 pool = multiprocessing.Pool(processes=multiprocessing.cpu_count() * 2)
61 for record in records:
62 pool.apply_async(download, record, callback=lambda x: sys.stderr.write('.'))
63 pool.close()
64 pool.join()
Hal Canary181ec2f2018-01-24 13:42:38 -050065 sys.stderr.write('\n')
66
67if __name__ == '__main__':
68 main()