kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
| 10 | __author__ = 'kjellander@webrtc.org (Henrik Kjellander)' |
| 11 | |
| 12 | """Downloads WebRTC resources files from a remote host.""" |
| 13 | |
| 14 | from optparse import OptionParser |
| 15 | import os |
| 16 | import shutil |
| 17 | import sys |
| 18 | import tarfile |
| 19 | import tempfile |
| 20 | import urllib2 |
| 21 | |
| 22 | |
| 23 | def main(): |
| 24 | """ |
| 25 | Downloads WebRTC resources files from a remote host. |
| 26 | |
| 27 | This script will download WebRTC resource files used for testing, like audio |
| 28 | and video files. It will check the current version in the DEPS file and |
| 29 | compare it with the one downloaded (kept in a text file in the download dir). |
| 30 | If there's a new version of the resources available it will be downloaded. |
| 31 | """ |
| 32 | # Constants |
| 33 | deps_key = 'webrtc_resources_revision' |
| 34 | remote_url_base = 'http://commondatastorage.googleapis.com/webrtc-resources' |
| 35 | version_filename = 'webrtc-resources-version' |
| 36 | filename_prefix = 'webrtc-resources-' |
| 37 | extension = '.tgz' |
| 38 | |
| 39 | # Variables used by the script |
| 40 | project_root_dir = os.path.normpath(sys.path[0] + '/../../') |
| 41 | deps_file = os.path.join(project_root_dir, 'DEPS') |
| 42 | downloads_dir = os.path.join(project_root_dir, 'resources') |
| 43 | current_version_file = os.path.join(downloads_dir, version_filename) |
| 44 | |
| 45 | # Ensure the downloads dir is created. |
| 46 | if not os.path.isdir(downloads_dir): |
| 47 | os.mkdir(downloads_dir) |
| 48 | |
| 49 | # Define and parse arguments. |
| 50 | parser = OptionParser() |
| 51 | parser.add_option('-f', '--force', action='store_true', dest='force', |
| 52 | help='forces download and removes all existing resources.') |
| 53 | (options, unused_args) = parser.parse_args() |
| 54 | |
| 55 | # Check if we have an existing version already downloaded. |
| 56 | current_version = 0 |
| 57 | if os.path.isfile(current_version_file): |
| 58 | f = open(current_version_file) |
| 59 | current_version = int(f.read()) |
| 60 | f.close() |
| 61 | print 'Found downloaded resources: version: %s' % current_version |
| 62 | |
| 63 | # Check the DEPS file for the latest version number. |
| 64 | deps_vars = EvalDepsFile(deps_file)['vars'] |
| 65 | latest_version = int(deps_vars[deps_key]) |
| 66 | print 'Version in DEPS file: %d' % latest_version |
| 67 | |
| 68 | # Download archive if forced or latest version is newer than our current. |
| 69 | if latest_version > current_version or options.force: |
| 70 | temp_dir = tempfile.mkdtemp(prefix='webrtc-resources-') |
| 71 | archive_name = '%s%s%s' % (filename_prefix, latest_version, extension) |
| 72 | remote_archive_url = os.path.join(remote_url_base, archive_name) |
| 73 | # Download into the temporary directory with display of progress, inspired |
| 74 | # by the Stack Overflow post at http://goo.gl/JIrbo |
| 75 | temp_file = os.path.join(temp_dir, archive_name) |
| 76 | print 'Downloading: %s' % remote_archive_url |
| 77 | u = urllib2.urlopen(remote_archive_url) |
| 78 | f = open(temp_file, 'wb') |
| 79 | meta = u.info() |
| 80 | file_size = int(meta.getheaders('Content-Length')[0]) |
| 81 | print 'Progress: %s bytes: %s' % (archive_name, file_size) |
| 82 | |
| 83 | file_size_dl = 0 |
| 84 | block_size = 65536 |
| 85 | while True: |
| 86 | file_buffer = u.read(block_size) |
| 87 | if not file_buffer: |
| 88 | break |
| 89 | file_size_dl += len(file_buffer) |
| 90 | f.write(file_buffer) |
| 91 | status = r'%10d [%3.2f%%]' % (file_size_dl, |
| 92 | file_size_dl * 100. / file_size) |
| 93 | status += chr(8) * (len(status) + 1) |
| 94 | print status, |
| 95 | print |
| 96 | f.close() |
| 97 | |
| 98 | # Clean up the existing resources dir. |
| 99 | print 'Removing old resources in %s' % downloads_dir |
| 100 | shutil.rmtree(downloads_dir) |
| 101 | os.mkdir(downloads_dir) |
| 102 | |
| 103 | # Write the latest version to a text file in the resources dir to avoid |
| 104 | # re-download of the same version in the future: |
| 105 | new_version_file = os.path.join(downloads_dir, version_filename) |
| 106 | f = open(new_version_file, 'w') |
| 107 | f.write('%d' % latest_version) |
| 108 | f.close() |
| 109 | |
| 110 | # Extract the archive |
| 111 | archive = tarfile.open(temp_file, 'r:gz') |
| 112 | archive.extractall(downloads_dir) |
| 113 | archive.close() |
| 114 | print 'Extracted resource files into %s' % downloads_dir |
| 115 | # Clean up the temp dir |
| 116 | shutil.rmtree(temp_dir) |
| 117 | else: |
| 118 | print 'Already have latest (or newer) version: %s' % current_version |
| 119 | |
| 120 | |
| 121 | def EvalDepsFile(path): |
| 122 | scope = {'Var': lambda name: scope['vars'][name], |
| 123 | 'File': lambda name: name} |
| 124 | execfile(path, {}, scope) |
| 125 | return scope |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | main() |