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