tahoe-lafs/src/allmydata/scripts/tahoe_get.py

54 lines
1.4 KiB
Python
Raw Normal View History

2007-07-11 17:26:19 +00:00
#!/usr/bin/env python
2007-10-12 02:47:40 +00:00
import urllib
2007-07-11 17:26:19 +00:00
2007-10-12 02:20:41 +00:00
def get(nodeurl, root_uri, vdrive_fname, local_file, stdout, stderr):
if nodeurl[-1] != "/":
nodeurl += "/"
2007-10-11 08:34:44 +00:00
url = nodeurl + "uri/%s/" % urllib.quote(root_uri.replace("/","!"))
if vdrive_fname:
url += urllib.quote(vdrive_fname)
2007-07-11 17:26:19 +00:00
if local_file is None or local_file == "-":
2007-10-12 02:20:41 +00:00
outf = stdout
close_outf = False
2007-07-11 17:26:19 +00:00
else:
outf = open(local_file, "wb")
2007-10-12 02:20:41 +00:00
close_outf = True
2007-07-11 17:26:19 +00:00
inf = urllib.urlopen(url)
while True:
data = inf.read(4096)
if not data:
break
outf.write(data)
2007-10-12 02:20:41 +00:00
if close_outf:
outf.close()
2007-07-11 17:26:19 +00:00
return 0
def main():
import optparse, re
2007-07-11 17:26:19 +00:00
parser = optparse.OptionParser()
parser.add_option("-u", "--nodeurl", dest="nodeurl")
parser.add_option("-r", "--root-uri", dest="rooturi")
2007-07-11 17:26:19 +00:00
(options, args) = parser.parse_args()
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
if not options.rooturi:
raise ValueError("must provide --root-uri")
vdrive_fname = args[0]
2007-07-11 17:26:19 +00:00
local_file = None
if len(args) > 1:
local_file = args[1]
get(options.nodeurl, options.rooturi, vdrive_fname, local_file)
2007-07-11 17:26:19 +00:00
if __name__ == '__main__':
main()