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