2011-11-23 15:11:16 +01:00
|
|
|
#!/usr/bin/env python
|
2012-02-23 22:32:37 +01:00
|
|
|
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-11-23 15:11:16 +01:00
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license
|
|
|
|
# that can be found in the LICENSE file in the root of the source
|
|
|
|
# tree. An additional intellectual property rights grant can be found
|
|
|
|
# in the file PATENTS. All contributing project authors may
|
|
|
|
# be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
|
|
|
__author__ = 'kjellander@webrtc.org (Henrik Kjellander)'
|
|
|
|
|
|
|
|
"""Downloads WebRTC resources files from a remote host."""
|
|
|
|
|
|
|
|
from optparse import OptionParser
|
2011-11-23 16:24:44 +01:00
|
|
|
from urlparse import urljoin
|
2011-11-23 15:11:16 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
import urllib2
|
|
|
|
|
2012-03-02 14:44:44 +01:00
|
|
|
DEPS_KEY = 'webrtc_resources_revision'
|
|
|
|
REMOTE_URL_BASE = 'http://commondatastorage.googleapis.com/webrtc-resources'
|
|
|
|
VERSION_FILENAME = 'webrtc-resources-version'
|
|
|
|
FILENAME_PREFIX = 'webrtc-resources-'
|
|
|
|
EXTENSION = '.tgz'
|
|
|
|
|
2011-11-23 15:11:16 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Downloads WebRTC resources files from a remote host.
|
|
|
|
|
|
|
|
This script will download WebRTC resource files used for testing, like audio
|
|
|
|
and video files. It will check the current version in the DEPS file and
|
|
|
|
compare it with the one downloaded (kept in a text file in the download dir).
|
2012-01-05 09:05:07 +01:00
|
|
|
If the DEPS version is different than the one downloaded, the correct version
|
|
|
|
will be downloaded.
|
2011-11-23 15:11:16 +01:00
|
|
|
"""
|
2012-03-02 14:44:44 +01:00
|
|
|
|
|
|
|
# Make it possible to skip download using an environment variable:
|
|
|
|
if os.getenv('WEBRTC_SKIP_RESOURCES_DOWNLOAD'):
|
|
|
|
print 'Skipping resources download since WEBRTC_SKIP_RESOURCES_DOWNLOAD set'
|
|
|
|
return
|
|
|
|
|
2012-02-23 22:32:37 +01:00
|
|
|
project_root_dir = os.path.normpath(sys.path[0] + '/../../')
|
2011-11-23 15:11:16 +01:00
|
|
|
deps_file = os.path.join(project_root_dir, 'DEPS')
|
|
|
|
downloads_dir = os.path.join(project_root_dir, 'resources')
|
2012-03-02 14:44:44 +01:00
|
|
|
current_version_file = os.path.join(downloads_dir, VERSION_FILENAME)
|
2012-02-23 22:32:37 +01:00
|
|
|
|
2011-11-23 15:11:16 +01:00
|
|
|
# Ensure the downloads dir is created.
|
|
|
|
if not os.path.isdir(downloads_dir):
|
|
|
|
os.mkdir(downloads_dir)
|
|
|
|
|
|
|
|
# Define and parse arguments.
|
|
|
|
parser = OptionParser()
|
2012-02-23 22:32:37 +01:00
|
|
|
parser.add_option('-f', '--force', action='store_true', dest='force',
|
2012-03-02 14:44:44 +01:00
|
|
|
help='forces download and removal of existing resources.')
|
|
|
|
parser.add_option('-b', '--base_url', dest='base_url',
|
|
|
|
help= 'Overrides the default Base URL (%s) and uses the '
|
|
|
|
'supplied URL instead.' % REMOTE_URL_BASE)
|
2011-11-23 15:11:16 +01:00
|
|
|
(options, unused_args) = parser.parse_args()
|
|
|
|
|
2012-03-02 14:44:44 +01:00
|
|
|
# Download archive if forced or DEPS version is different than our current.
|
|
|
|
current_version = _get_current_version(current_version_file)
|
|
|
|
desired_version = _get_desired_version(deps_file)
|
|
|
|
if desired_version != current_version or options.force:
|
|
|
|
base_url = options.base_url or REMOTE_URL_BASE
|
|
|
|
_perform_download(base_url, desired_version, downloads_dir)
|
|
|
|
else:
|
|
|
|
print 'Already have correct version: %s' % current_version
|
|
|
|
|
|
|
|
|
|
|
|
def _get_current_version(current_version_file):
|
|
|
|
"""Returns the version already downloaded (if any).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
current_version_file: The filename of the text file containing the
|
|
|
|
currently downloaded version (if any) on local disk.
|
|
|
|
Returns:
|
|
|
|
The version number, or 0 if no downloaded version exists.
|
|
|
|
"""
|
2011-11-23 15:11:16 +01:00
|
|
|
current_version = 0
|
|
|
|
if os.path.isfile(current_version_file):
|
|
|
|
f = open(current_version_file)
|
|
|
|
current_version = int(f.read())
|
|
|
|
f.close()
|
|
|
|
print 'Found downloaded resources: version: %s' % current_version
|
2012-03-02 14:44:44 +01:00
|
|
|
return current_version
|
2011-11-23 15:11:16 +01:00
|
|
|
|
2012-02-23 22:32:37 +01:00
|
|
|
|
2012-03-02 14:44:44 +01:00
|
|
|
def _get_desired_version(deps_file):
|
|
|
|
"""Evaluates the project's DEPS and returns the desired resources version.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
deps_file: Full path to the DEPS file of the project.
|
|
|
|
Returns:
|
|
|
|
The desired resources version.
|
|
|
|
"""
|
|
|
|
# Evaluate the DEPS file as Python code to extract the variables defined.
|
|
|
|
locals_dict = {'Var': lambda name: locals_dict['vars'][name],
|
|
|
|
'File': lambda name: name,
|
|
|
|
'From': lambda deps, definition: deps}
|
|
|
|
execfile(deps_file, {}, locals_dict)
|
|
|
|
deps_vars = locals_dict['vars']
|
|
|
|
|
|
|
|
desired_version = int(deps_vars[DEPS_KEY])
|
|
|
|
print 'Version in DEPS file: %d' % desired_version
|
|
|
|
return desired_version
|
|
|
|
|
|
|
|
|
|
|
|
def _perform_download(base_url, desired_version, downloads_dir):
|
|
|
|
"""Performs the download and extracts the downloaded resources.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
base_url: URL that holds the resource downloads.
|
|
|
|
desired_version: Desired version, which decides the filename.
|
|
|
|
"""
|
|
|
|
temp_dir = tempfile.mkdtemp(prefix='webrtc-resources-')
|
|
|
|
try:
|
|
|
|
archive_name = '%s%s%s' % (FILENAME_PREFIX, desired_version, EXTENSION)
|
|
|
|
# urljoin requires base URL to end with slash to construct a proper URL
|
|
|
|
# to our file:
|
|
|
|
if not base_url[-1:] == '/':
|
|
|
|
base_url += '/'
|
|
|
|
remote_archive_url = urljoin(base_url, archive_name)
|
2012-02-23 22:32:37 +01:00
|
|
|
# Download into the temporary directory with display of progress, inspired
|
2012-03-09 12:15:07 +01:00
|
|
|
# by the Stack Overflow post at http://goo.gl/JIrbo
|
2012-03-02 14:44:44 +01:00
|
|
|
temp_filename = os.path.join(temp_dir, archive_name)
|
2011-11-23 15:11:16 +01:00
|
|
|
print 'Downloading: %s' % remote_archive_url
|
2012-03-02 14:44:44 +01:00
|
|
|
|
|
|
|
response = urllib2.urlopen(remote_archive_url)
|
|
|
|
temp_file = open(temp_filename, 'wb')
|
2012-03-09 12:15:07 +01:00
|
|
|
meta = response.info()
|
|
|
|
file_size_kb = int(meta.getheaders('Content-Length')[0]) / 1024
|
|
|
|
print 'Progress: %s : %s kB' % (archive_name, file_size_kb)
|
|
|
|
|
|
|
|
file_size_dl_kb = 0
|
|
|
|
block_size = 65536
|
|
|
|
while True:
|
|
|
|
file_buffer = response.read(block_size)
|
|
|
|
if not file_buffer:
|
|
|
|
break
|
|
|
|
file_size_dl_kb += len(file_buffer) / 1024
|
|
|
|
temp_file.write(file_buffer)
|
|
|
|
status = r'%10d kB [%3.2f%%]' % (file_size_dl_kb,
|
|
|
|
file_size_dl_kb * 100. / file_size_kb)
|
|
|
|
status += chr(8) * (len(status) + 1)
|
|
|
|
print status,
|
|
|
|
print
|
2012-03-02 14:44:44 +01:00
|
|
|
temp_file.close()
|
2011-11-23 15:11:16 +01:00
|
|
|
|
|
|
|
# Clean up the existing resources dir.
|
|
|
|
print 'Removing old resources in %s' % downloads_dir
|
|
|
|
shutil.rmtree(downloads_dir)
|
|
|
|
os.mkdir(downloads_dir)
|
|
|
|
|
2012-03-02 14:44:44 +01:00
|
|
|
# Extract the archive.
|
|
|
|
archive = tarfile.open(temp_filename, 'r:gz')
|
|
|
|
archive.extractall(downloads_dir)
|
|
|
|
archive.close()
|
|
|
|
print 'Extracted resource files into %s' % downloads_dir
|
|
|
|
|
2012-01-05 09:05:07 +01:00
|
|
|
# Write the downloaded version to a text file in the resources dir to avoid
|
|
|
|
# re-download of the same version in the future.
|
2012-03-09 12:15:07 +01:00
|
|
|
new_version_filename = os.path.join(downloads_dir, VERSION_FILENAME)
|
|
|
|
version_file = open(new_version_filename, 'w')
|
|
|
|
version_file.write('%d' % desired_version)
|
|
|
|
version_file.close()
|
2012-02-23 22:32:37 +01:00
|
|
|
|
2012-03-02 14:44:44 +01:00
|
|
|
finally:
|
2012-01-05 09:05:07 +01:00
|
|
|
# Clean up the temp dir.
|
2011-11-23 15:11:16 +01:00
|
|
|
shutil.rmtree(temp_dir)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|