bump tbb windows version
[torbrowser/rransom.git] / build-scripts / pyget.py
blob3c0925b9a78c78639c469d3d724a9bcbff943bc3
1 #!/usr/bin/python
3 ###
4 ### Simple HTTP download utility for platforms without wget
5 ###
6 ### Copyright 2008 Steven J. Murdoch <http://www.cl.cam.ac.uk/users/sjm217/>
7 ### See LICENSE for licensing information
8 ###
9 ### $Id$
10 ###
12 import sys
13 import os
14 import urllib
15 import urlparse
16 from optparse import OptionParser
18 ## Destination filename when no sensible default can be guessed
19 DEFAULT_DEST = "index.html"
21 ## Create a URL opener which throws an exception on error
22 class DebugURLopener(urllib.FancyURLopener):
23 def http_error_default(self, url, fp, errcode, errmsg, headers):
24 _ = fp.read()
25 fp.close()
26 raise IOError, ('http error', errcode, errmsg, headers)
28 ## Set this as the default URL opener
29 urllib._urlopener = DebugURLopener()
31 def main():
32 ## Parse command line
33 usage = "Usage: %prog [options] URL\n\nDownload URL to file."
34 parser = OptionParser(usage)
35 parser.set_defaults(verbose=True)
36 parser.add_option("-O", "--output-document", dest="dest",
37 help="write document to DEST")
38 parser.add_option("-q", "--quiet", action="store_false", dest="verbose",
39 help="don't show debugging information")
41 (options, args) = parser.parse_args()
42 if len(args) != 1:
43 parser.error("Missing URL")
45 ## Get URL
46 url = args[0]
48 ## Get destination filename
49 if options.dest:
50 dest = options.dest
51 else:
52 url_components = urlparse.urlsplit(url)
53 dest = os.path.basename(url_components.path).strip()
54 if dest == "":
55 dest = DEFAULT_DEST
57 ## Download URL
58 if options.verbose:
59 print "Downloading %s to %s..."%(url, dest)
61 urllib.urlretrieve(url, dest)
63 if options.verbose:
64 print "Download was successful."
66 if __name__ == "__main__":
67 main()