blob: a07e2c1fbf1a45a86cfed2c313352d2e8dfeba77 [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
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).
kjellander@webrtc.org401045a2012-01-05 08:05:07 +000031 If the DEPS version is different than the one downloaded, the correct version
32 will be downloaded.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000033 """
kjellander@webrtc.org401045a2012-01-05 08:05:07 +000034 # Constants.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000035 deps_key = 'webrtc_resources_revision'
kjellander@webrtc.org3379b0c2011-11-23 15:24:44 +000036 remote_url_base = 'http://commondatastorage.googleapis.com/webrtc-resources/'
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000037 version_filename = 'webrtc-resources-version'
38 filename_prefix = 'webrtc-resources-'
39 extension = '.tgz'
40
kjellander@webrtc.org401045a2012-01-05 08:05:07 +000041 # Variables used by the script.
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000042 project_root_dir = os.path.normpath(sys.path[0] + '/../../')
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000043 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.org6241bee2012-02-23 21:32:37 +000046
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000047 # 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.org6241bee2012-02-23 21:32:37 +000053 parser.add_option('-f', '--force', action='store_true', dest='force',
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000054 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.org6241bee2012-02-23 21:32:37 +000069
kjellander@webrtc.org401045a2012-01-05 08:05:07 +000070 # Download archive if forced or DEPS version is different than our current.
71 if latest_version != current_version or options.force:
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000072 temp_dir = tempfile.mkdtemp(prefix='webrtc-resources-')
73 archive_name = '%s%s%s' % (filename_prefix, latest_version, extension)
kjellander@webrtc.org3379b0c2011-11-23 15:24:44 +000074 remote_archive_url = urljoin(remote_url_base, archive_name)
andrew@webrtc.org6241bee2012-02-23 21:32:37 +000075 # Download into the temporary directory with display of progress, inspired
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +000076 # 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.org401045a2012-01-05 08:05:07 +0000105 # 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.org0c80c7f2011-11-23 14:11:16 +0000107 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.org6241bee2012-02-23 21:32:37 +0000111
kjellander@webrtc.org401045a2012-01-05 08:05:07 +0000112 # Extract the archive.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000113 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.org401045a2012-01-05 08:05:07 +0000117 # Clean up the temp dir.
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000118 shutil.rmtree(temp_dir)
119 else:
kjellander@webrtc.org401045a2012-01-05 08:05:07 +0000120 print 'Already have correct version: %s' % current_version
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000121
122
123def EvalDepsFile(path):
andrew@webrtc.org6241bee2012-02-23 21:32:37 +0000124 scope = {'Var': lambda name: scope['vars'][name],
125 'File': lambda name: name,
126 'From': lambda deps, definition: deps}
kjellander@webrtc.org0c80c7f2011-11-23 14:11:16 +0000127 execfile(path, {}, scope)
128 return scope
129
130if __name__ == '__main__':
131 main()