imcplugin: Add automated tests
[nativeclient.git] / site_scons / sync_tgz.py
blob8af6a96010ef4efce08b2e29e9b18f339c17b0a2
1 #!/usr/bin/python2.4
2 # Copyright 2009, Google Inc.
3 # All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Keep a local directory in sync with a website tar file.
33 This module downloads a tgz, and expands it as needed.
34 It supports username and password with basic authentication.
35 """
37 import os
38 import shutil
39 import tarfile
40 import http_download
43 def SyncTgz(url, target, username=None, password=None, verbose=True):
44 """Download a file from a remote server.
46 Args:
47 url: A URL to download from.
48 target: Directory to extract to and prefix to use for tgz file.
49 username: Optional username for download.
50 password: Optional password for download (ignored if no username).
51 verbose: Flag indicating if status shut be printed.
52 """
53 shutil.rmtree(target, True)
54 tgz_filename = target + '.tgz'
56 if verbose:
57 print 'Downloading %s to %s...' % (url, tgz_filename)
58 http_download.HttpDownload(url, tgz_filename,
59 username=username, password=password)
61 if verbose:
62 print 'Extracting from %s...' % tgz_filename
63 tgz = tarfile.open(tgz_filename, 'r')
64 for m in tgz:
65 if verbose:
66 print m.name
67 tgz.extract(m, target)
68 tgz.close()
69 os.remove(tgz_filename)
71 if verbose:
72 print 'Update complete.'