kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
andrew@webrtc.org | 6241bee | 2012-02-23 21:32:37 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 10 | """Downloads WebRTC resources files from a remote host.""" |
| 11 | |
| 12 | from optparse import OptionParser |
kjellander@webrtc.org | 3379b0c | 2011-11-23 15:24:44 +0000 | [diff] [blame] | 13 | from urlparse import urljoin |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 14 | import os |
| 15 | import shutil |
| 16 | import sys |
| 17 | import tarfile |
| 18 | import tempfile |
| 19 | import urllib2 |
| 20 | |
henrike@webrtc.org | 2a7fd53 | 2013-06-27 18:36:28 +0000 | [diff] [blame] | 21 | DESIRED_VERSION = 16 |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 22 | REMOTE_URL_BASE = 'http://commondatastorage.googleapis.com/webrtc-resources' |
| 23 | VERSION_FILENAME = 'webrtc-resources-version' |
| 24 | FILENAME_PREFIX = 'webrtc-resources-' |
| 25 | EXTENSION = '.tgz' |
| 26 | |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 27 | |
| 28 | def main(): |
| 29 | """ |
| 30 | Downloads WebRTC resources files from a remote host. |
| 31 | |
| 32 | This script will download WebRTC resource files used for testing, like audio |
| 33 | and video files. It will check the current version in the DEPS file and |
| 34 | compare it with the one downloaded (kept in a text file in the download dir). |
kjellander@webrtc.org | 401045a | 2012-01-05 08:05:07 +0000 | [diff] [blame] | 35 | If the DEPS version is different than the one downloaded, the correct version |
| 36 | will be downloaded. |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 37 | """ |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 38 | |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 39 | # Make it possible to skip download using an environment variable: |
| 40 | if os.getenv('WEBRTC_SKIP_RESOURCES_DOWNLOAD'): |
| 41 | print 'Skipping resources download since WEBRTC_SKIP_RESOURCES_DOWNLOAD set' |
| 42 | return |
| 43 | |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame^] | 44 | project_root_dir = os.path.normpath(sys.path[0] + '/../../') |
| 45 | downloads_dir = os.path.join(project_root_dir, 'resources') |
| 46 | current_version_file = os.path.join(downloads_dir, VERSION_FILENAME) |
| 47 | |
| 48 | # Ensure the downloads dir is created. |
| 49 | if not os.path.isdir(downloads_dir): |
| 50 | os.mkdir(downloads_dir) |
| 51 | |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 52 | # Define and parse arguments. |
| 53 | parser = OptionParser() |
andrew@webrtc.org | 6241bee | 2012-02-23 21:32:37 +0000 | [diff] [blame] | 54 | parser.add_option('-f', '--force', action='store_true', dest='force', |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 55 | help='forces download and removal of existing resources.') |
| 56 | parser.add_option('-b', '--base_url', dest='base_url', |
| 57 | help= 'Overrides the default Base URL (%s) and uses the ' |
| 58 | 'supplied URL instead.' % REMOTE_URL_BASE) |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame^] | 59 | (options, unused_args) = parser.parse_args() |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 60 | |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 61 | # Download archive if forced or DEPS version is different than our current. |
| 62 | current_version = _get_current_version(current_version_file) |
henrike@webrtc.org | 2a7fd53 | 2013-06-27 18:36:28 +0000 | [diff] [blame] | 63 | if DESIRED_VERSION != current_version or options.force: |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 64 | base_url = options.base_url or REMOTE_URL_BASE |
henrike@webrtc.org | 2a7fd53 | 2013-06-27 18:36:28 +0000 | [diff] [blame] | 65 | _perform_download(base_url, DESIRED_VERSION, downloads_dir) |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 66 | else: |
| 67 | print 'Already have correct version: %s' % current_version |
| 68 | |
| 69 | |
| 70 | def _get_current_version(current_version_file): |
| 71 | """Returns the version already downloaded (if any). |
| 72 | |
| 73 | Args: |
| 74 | current_version_file: The filename of the text file containing the |
| 75 | currently downloaded version (if any) on local disk. |
| 76 | Returns: |
| 77 | The version number, or 0 if no downloaded version exists. |
| 78 | """ |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 79 | current_version = 0 |
| 80 | if os.path.isfile(current_version_file): |
| 81 | f = open(current_version_file) |
| 82 | current_version = int(f.read()) |
| 83 | f.close() |
| 84 | print 'Found downloaded resources: version: %s' % current_version |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 85 | return current_version |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 86 | |
andrew@webrtc.org | 6241bee | 2012-02-23 21:32:37 +0000 | [diff] [blame] | 87 | |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 88 | def _perform_download(base_url, desired_version, downloads_dir): |
| 89 | """Performs the download and extracts the downloaded resources. |
| 90 | |
| 91 | Args: |
| 92 | base_url: URL that holds the resource downloads. |
| 93 | desired_version: Desired version, which decides the filename. |
| 94 | """ |
| 95 | temp_dir = tempfile.mkdtemp(prefix='webrtc-resources-') |
| 96 | try: |
| 97 | archive_name = '%s%s%s' % (FILENAME_PREFIX, desired_version, EXTENSION) |
| 98 | # urljoin requires base URL to end with slash to construct a proper URL |
| 99 | # to our file: |
| 100 | if not base_url[-1:] == '/': |
| 101 | base_url += '/' |
| 102 | remote_archive_url = urljoin(base_url, archive_name) |
andrew@webrtc.org | 6241bee | 2012-02-23 21:32:37 +0000 | [diff] [blame] | 103 | # Download into the temporary directory with display of progress, inspired |
kjellander@webrtc.org | 7256bdc | 2012-03-09 11:15:07 +0000 | [diff] [blame] | 104 | # by the Stack Overflow post at http://goo.gl/JIrbo |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 105 | temp_filename = os.path.join(temp_dir, archive_name) |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 106 | print 'Downloading: %s' % remote_archive_url |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 107 | |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 108 | response = urllib2.urlopen(remote_archive_url) |
| 109 | temp_file = open(temp_filename, 'wb') |
kjellander@webrtc.org | 7256bdc | 2012-03-09 11:15:07 +0000 | [diff] [blame] | 110 | meta = response.info() |
| 111 | file_size_kb = int(meta.getheaders('Content-Length')[0]) / 1024 |
| 112 | print 'Progress: %s : %s kB' % (archive_name, file_size_kb) |
| 113 | |
| 114 | file_size_dl_kb = 0 |
| 115 | block_size = 65536 |
| 116 | while True: |
| 117 | file_buffer = response.read(block_size) |
| 118 | if not file_buffer: |
| 119 | break |
| 120 | file_size_dl_kb += len(file_buffer) / 1024 |
| 121 | temp_file.write(file_buffer) |
| 122 | status = r'%10d kB [%3.2f%%]' % (file_size_dl_kb, |
| 123 | file_size_dl_kb * 100. / file_size_kb) |
| 124 | status += chr(8) * (len(status) + 1) |
| 125 | print status, |
| 126 | print |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 127 | temp_file.close() |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 128 | |
| 129 | # Clean up the existing resources dir. |
| 130 | print 'Removing old resources in %s' % downloads_dir |
| 131 | shutil.rmtree(downloads_dir) |
| 132 | os.mkdir(downloads_dir) |
| 133 | |
kjellander@webrtc.org | 401045a | 2012-01-05 08:05:07 +0000 | [diff] [blame] | 134 | # Extract the archive. |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 135 | archive = tarfile.open(temp_filename, 'r:gz') |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 136 | archive.extractall(downloads_dir) |
| 137 | archive.close() |
| 138 | print 'Extracted resource files into %s' % downloads_dir |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 139 | |
| 140 | # Write the downloaded version to a text file in the resources dir to avoid |
| 141 | # re-download of the same version in the future. |
kjellander@webrtc.org | 7256bdc | 2012-03-09 11:15:07 +0000 | [diff] [blame] | 142 | new_version_filename = os.path.join(downloads_dir, VERSION_FILENAME) |
| 143 | version_file = open(new_version_filename, 'w') |
| 144 | version_file.write('%d' % desired_version) |
| 145 | version_file.close() |
kjellander@webrtc.org | 9457422 | 2012-03-02 13:44:44 +0000 | [diff] [blame] | 146 | |
| 147 | finally: |
kjellander@webrtc.org | 401045a | 2012-01-05 08:05:07 +0000 | [diff] [blame] | 148 | # Clean up the temp dir. |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 149 | shutil.rmtree(temp_dir) |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 150 | |
kjellander@webrtc.org | 0c80c7f | 2011-11-23 14:11:16 +0000 | [diff] [blame] | 151 | if __name__ == '__main__': |
| 152 | main() |