]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_get.py
af577228198664d2b65c499e4d754c8bf15cf1f6
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_get.py
1 #!/usr/bin/env python
2
3 import urllib
4
5 def get(nodeurl, root_uri, vdrive_fname, local_file, stdout, stderr):
6     if nodeurl[-1] != "/":
7         nodeurl += "/"
8     url = nodeurl + "uri/%s/" % urllib.quote(root_uri)
9     if vdrive_fname:
10         url += urllib.quote(vdrive_fname)
11
12     if local_file is None or local_file == "-":
13         outf = stdout
14         close_outf = False
15     else:
16         outf = open(local_file, "wb")
17         close_outf = True
18     inf = urllib.urlopen(url)
19     while True:
20         data = inf.read(4096)
21         if not data:
22             break
23         outf.write(data)
24     if close_outf:
25         outf.close()
26
27     return 0
28
29
30 def main():
31     import optparse, re
32     parser = optparse.OptionParser()
33     parser.add_option("-u", "--nodeurl", dest="nodeurl")
34     parser.add_option("-r", "--root-uri", dest="rooturi")
35
36     (options, args) = parser.parse_args()
37
38     NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
39     if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
40         raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
41
42     if not options.rooturi:
43         raise ValueError("must provide --root-uri")
44
45     vdrive_fname = args[0]
46     local_file = None
47     if len(args) > 1:
48         local_file = args[1]
49
50     get(options.nodeurl, options.rooturi, vdrive_fname, local_file)
51
52 if __name__ == '__main__':
53     main()