download_model 2.2 KB

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