1 # Copyright (C) 2010, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(__file__
))))
7 from zeroinstall
import _
9 # NB: duplicated in download.py
12 RESULT_NOT_MODIFIED
= 2
14 def _download_as_child(url
, if_modified_since
):
15 from httplib
import HTTPException
16 from urllib2
import urlopen
, Request
, HTTPError
, URLError
18 #print "Child downloading", url
19 if url
.startswith('http:') or url
.startswith('https:') or url
.startswith('ftp:'):
21 if url
.startswith('http:') and if_modified_since
:
22 req
.add_header('If-Modified-Since', if_modified_since
)
25 raise Exception(_('Unsupported URL protocol in: %s') % url
)
29 except AttributeError:
30 sock
= src
.fp
.fp
._sock
# Python 2.5 on FreeBSD
37 except (HTTPError
, URLError
, HTTPException
) as ex
:
38 if isinstance(ex
, HTTPError
) and ex
.code
== 304: # Not modified
39 sys
.exit(RESULT_NOT_MODIFIED
)
40 print >>sys
.stderr
, "Error downloading '" + url
+ "': " + (str(ex
) or str(ex
.__class
__.__name
__))
41 sys
.exit(RESULT_FAILED
)
43 if __name__
== '__main__':
44 assert (len(sys
.argv
) == 2) or (len(sys
.argv
) == 3), "Usage: download URL [If-Modified-Since-Date], not %s" % sys
.argv
45 if len(sys
.argv
) >= 3:
46 if_modified_since_date
= sys
.argv
[2]
48 if_modified_since_date
= None
49 _download_as_child(sys
.argv
[1], if_modified_since_date
)