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