2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
43 def SyncTgz(url
, target
, username
=None, password
=None, verbose
=True):
44 """Download a file from a remote server.
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.
53 shutil
.rmtree(target
, True)
54 tgz_filename
= target
+ '.tgz'
57 print 'Downloading %s to %s...' % (url
, tgz_filename
)
58 http_download
.HttpDownload(url
, tgz_filename
,
59 username
=username
, password
=password
)
62 print 'Extracting from %s...' % tgz_filename
63 tgz
= tarfile
.open(tgz_filename
, 'r')
67 tgz
.extract(m
, target
)
69 os
.remove(tgz_filename
)
72 print 'Update complete.'