blob: 1cb88f4fce65b1a15054770e576ead3b064e33ac [file] [log] [blame]
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +00001#!/usr/bin/env python
andrew@webrtc.org6241bee2012-02-23 21:32:37 +00002# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +00003#
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.org0c80c7f2011-11-23 14:11:16 +000010"""Downloads WebRTC resources files from a remote host."""
11
12from optparse import OptionParser
kjellander@webrtc.org3379b0c2011-11-23 15:24:44 +000013from urlparse import urljoin
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000014import os
15import shutil
16import sys
17import tarfile
18import tempfile
19import urllib2
20
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +000021DESIRED_VERSION = 18
kjellander@webrtc.org94574222012-03-02 13:44:44 +000022REMOTE_URL_BASE = 'http://commondatastorage.googleapis.com/webrtc-resources'
23VERSION_FILENAME = 'webrtc-resources-version'
24FILENAME_PREFIX = 'webrtc-resources-'
25EXTENSION = '.tgz'
henrike@webrtc.org34773d92013-07-08 14:55:23 +000026RELATIVE_OUTPUT_PATH = '../../'
kjellander@webrtc.org94574222012-03-02 13:44:44 +000027
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000028
29def 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.org401045a2012-01-05 08:05:07 +000036 If the DEPS version is different than the one downloaded, the correct version
37 will be downloaded.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000038 """
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000039
kjellander@webrtc.org94574222012-03-02 13:44:44 +000040 # 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.org0c80c7f2011-11-23 14:11:16 +000045 # Define and parse arguments.
46 parser = OptionParser()
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000047 parser.add_option('-f', '--force', action='store_true', dest='force',
kjellander@webrtc.org94574222012-03-02 13:44:44 +000048 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.org34773d92013-07-08 14:55:23 +000052 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.org0c80c7f2011-11-23 14:11:16 +000063
kjellander@webrtc.org94574222012-03-02 13:44:44 +000064 # Download archive if forced or DEPS version is different than our current.
65 current_version = _get_current_version(current_version_file)
henrike@webrtc.org2a7fd532013-06-27 18:36:28 +000066 if DESIRED_VERSION != current_version or options.force:
kjellander@webrtc.org94574222012-03-02 13:44:44 +000067 base_url = options.base_url or REMOTE_URL_BASE
henrike@webrtc.org2a7fd532013-06-27 18:36:28 +000068 _perform_download(base_url, DESIRED_VERSION, downloads_dir)
kjellander@webrtc.org94574222012-03-02 13:44:44 +000069 else:
70 print 'Already have correct version: %s' % current_version
71
72
73def _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.org0c80c7f2011-11-23 14:11:16 +000082 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.org94574222012-03-02 13:44:44 +000088 return current_version
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000089
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000090
kjellander@webrtc.org94574222012-03-02 13:44:44 +000091def _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.org6241bee2012-02-23 21:32:37 +0000106 # Download into the temporary directory with display of progress, inspired
kjellander@webrtc.org7256bdc2012-03-09 11:15:07 +0000107 # by the Stack Overflow post at http://goo.gl/JIrbo
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000108 temp_filename = os.path.join(temp_dir, archive_name)
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000109 print 'Downloading: %s' % remote_archive_url
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000110
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000111 response = urllib2.urlopen(remote_archive_url)
112 temp_file = open(temp_filename, 'wb')
kjellander@webrtc.org7256bdc2012-03-09 11:15:07 +0000113 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.org94574222012-03-02 13:44:44 +0000130 temp_file.close()
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000131
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.org401045a2012-01-05 08:05:07 +0000137 # Extract the archive.
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000138 archive = tarfile.open(temp_filename, 'r:gz')
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000139 archive.extractall(downloads_dir)
140 archive.close()
141 print 'Extracted resource files into %s' % downloads_dir
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000142
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.org7256bdc2012-03-09 11:15:07 +0000145 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.org94574222012-03-02 13:44:44 +0000149
150 finally:
kjellander@webrtc.org401045a2012-01-05 08:05:07 +0000151 # Clean up the temp dir.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000152 shutil.rmtree(temp_dir)
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000153
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000154if __name__ == '__main__':
155 main()