blob: a1ac085c231fe1376433ff15c8eb3b3d1c74fba4 [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
henrike@webrtc.org2a7fd532013-06-27 18:36:28 +000021DESIRED_VERSION = 16
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'
26
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000027
28def 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.org401045a2012-01-05 08:05:07 +000035 If the DEPS version is different than the one downloaded, the correct version
36 will be downloaded.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000037 """
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000038
kjellander@webrtc.org94574222012-03-02 13:44:44 +000039 # 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.orgdb7d82f2013-07-05 08:49:09 +000044 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.org0c80c7f2011-11-23 14:11:16 +000052 # Define and parse arguments.
53 parser = OptionParser()
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000054 parser.add_option('-f', '--force', action='store_true', dest='force',
kjellander@webrtc.org94574222012-03-02 13:44:44 +000055 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.orgdb7d82f2013-07-05 08:49:09 +000059 (options, unused_args) = parser.parse_args()
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000060
kjellander@webrtc.org94574222012-03-02 13:44:44 +000061 # Download archive if forced or DEPS version is different than our current.
62 current_version = _get_current_version(current_version_file)
henrike@webrtc.org2a7fd532013-06-27 18:36:28 +000063 if DESIRED_VERSION != current_version or options.force:
kjellander@webrtc.org94574222012-03-02 13:44:44 +000064 base_url = options.base_url or REMOTE_URL_BASE
henrike@webrtc.org2a7fd532013-06-27 18:36:28 +000065 _perform_download(base_url, DESIRED_VERSION, downloads_dir)
kjellander@webrtc.org94574222012-03-02 13:44:44 +000066 else:
67 print 'Already have correct version: %s' % current_version
68
69
70def _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.org0c80c7f2011-11-23 14:11:16 +000079 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.org94574222012-03-02 13:44:44 +000085 return current_version
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000086
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000087
kjellander@webrtc.org94574222012-03-02 13:44:44 +000088def _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.org6241bee2012-02-23 21:32:37 +0000103 # Download into the temporary directory with display of progress, inspired
kjellander@webrtc.org7256bdc2012-03-09 11:15:07 +0000104 # by the Stack Overflow post at http://goo.gl/JIrbo
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000105 temp_filename = os.path.join(temp_dir, archive_name)
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000106 print 'Downloading: %s' % remote_archive_url
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000107
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000108 response = urllib2.urlopen(remote_archive_url)
109 temp_file = open(temp_filename, 'wb')
kjellander@webrtc.org7256bdc2012-03-09 11:15:07 +0000110 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.org94574222012-03-02 13:44:44 +0000127 temp_file.close()
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000128
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.org401045a2012-01-05 08:05:07 +0000134 # Extract the archive.
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000135 archive = tarfile.open(temp_filename, 'r:gz')
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000136 archive.extractall(downloads_dir)
137 archive.close()
138 print 'Extracted resource files into %s' % downloads_dir
kjellander@webrtc.org94574222012-03-02 13:44:44 +0000139
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.org7256bdc2012-03-09 11:15:07 +0000142 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.org94574222012-03-02 13:44:44 +0000146
147 finally:
kjellander@webrtc.org401045a2012-01-05 08:05:07 +0000148 # Clean up the temp dir.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000149 shutil.rmtree(temp_dir)
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000150
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000151if __name__ == '__main__':
152 main()