2007-07-11 10:26:19 -07:00
|
|
|
|
2015-01-30 00:50:18 +00:00
|
|
|
import urllib
|
2010-02-10 18:43:18 -08:00
|
|
|
from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
|
|
|
|
UnknownAliasError
|
2010-06-06 18:02:15 -07:00
|
|
|
from allmydata.scripts.common_http import do_http, format_http_error
|
2007-07-11 10:26:19 -07:00
|
|
|
|
2008-08-01 11:46:24 -07:00
|
|
|
def get(options):
|
|
|
|
nodeurl = options['node-url']
|
|
|
|
aliases = options.aliases
|
|
|
|
from_file = options.from_file
|
|
|
|
to_file = options.to_file
|
|
|
|
stdout = options.stdout
|
|
|
|
stderr = options.stderr
|
|
|
|
|
2007-08-17 12:54:47 -07:00
|
|
|
if nodeurl[-1] != "/":
|
|
|
|
nodeurl += "/"
|
2010-02-10 18:43:18 -08:00
|
|
|
try:
|
|
|
|
rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
|
|
|
|
except UnknownAliasError, e:
|
2010-06-06 18:02:15 -07:00
|
|
|
e.display(stderr)
|
2010-02-10 18:43:18 -08:00
|
|
|
return 1
|
2008-05-19 19:28:50 -07:00
|
|
|
url = nodeurl + "uri/%s" % urllib.quote(rootcap)
|
|
|
|
if path:
|
|
|
|
url += "/" + escape_path(path)
|
2007-07-11 10:26:19 -07:00
|
|
|
|
2008-05-19 19:28:50 -07:00
|
|
|
resp = do_http("GET", url)
|
|
|
|
if resp.status in (200, 201,):
|
2009-12-27 17:04:04 -05:00
|
|
|
if to_file:
|
2015-01-30 00:50:18 +00:00
|
|
|
outf = open(to_file, "wb")
|
2009-12-27 17:04:04 -05:00
|
|
|
else:
|
|
|
|
outf = stdout
|
2008-05-19 19:28:50 -07:00
|
|
|
while True:
|
|
|
|
data = resp.read(4096)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
outf.write(data)
|
2009-12-27 17:04:04 -05:00
|
|
|
if to_file:
|
|
|
|
outf.close()
|
2008-05-19 19:28:50 -07:00
|
|
|
rc = 0
|
2007-07-11 10:26:19 -07:00
|
|
|
else:
|
2010-06-06 18:02:15 -07:00
|
|
|
print >>stderr, format_http_error("Error during GET", resp)
|
2008-05-19 19:28:50 -07:00
|
|
|
rc = 1
|
|
|
|
|
|
|
|
return rc
|