Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / site_scons / http_download.py
blobabc64e65b704cae0a09efb5aa61ec6510f653fde
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 """Download a file from a URL to a file on disk.
33 This module supports username and password with basic authentication.
34 """
36 import base64
37 import os
38 import urllib2
41 def _CreateDirectory(path):
42 """Create a directory tree, ignore if it's already there."""
43 try:
44 os.makedirs(path)
45 return True
46 except os.error:
47 return False
50 def HttpDownload(url, target, username=None, password=None):
51 """Download a file from a remote server.
53 Args:
54 url: A URL to download from.
55 target: Filename to write download to.
56 username: Optional username for download.
57 password: Optional password for download (ignored if no username).
58 """
60 headers = [('Accept', '*/*')]
61 if username:
62 if password:
63 auth_code = base64.b64encode(username + ':' + password)
64 else:
65 auth_code = base64.b64encode(username)
66 headers.append(('Authorization', 'Basic ' + auth_code))
67 opener = urllib2.build_opener()
68 opener.addheaders = headers
69 urllib2.install_opener(opener)
70 src = urllib2.urlopen(url)
71 data = src.read()
72 src.close()
74 _CreateDirectory(os.path.split(target)[0])
75 fh = open(target, 'wb')
76 fh.write(data)
77 fh.close()