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