source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/tahoe_get.py
file stats: 35 lines, 31 executed: 88.6% covered
   1. 
   2. import urllib
   3. from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
   4. from allmydata.scripts.common_http import do_http
   5. 
   6. def get(options):
   7.     nodeurl = options['node-url']
   8.     aliases = options.aliases
   9.     from_file = options.from_file
  10.     to_file = options.to_file
  11.     stdout = options.stdout
  12.     stderr = options.stderr
  13. 
  14.     if nodeurl[-1] != "/":
  15.         nodeurl += "/"
  16.     rootcap, path = get_alias(aliases, from_file, DEFAULT_ALIAS)
  17.     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
  18.     if path:
  19.         url += "/" + escape_path(path)
  20. 
  21.     if to_file:
  22.         outf = open(to_file, "wb")
  23.         close_outf = True
  24.     else:
  25.         outf = stdout
  26.         close_outf = False
  27. 
  28.     resp = do_http("GET", url)
  29.     if resp.status in (200, 201,):
  30.         while True:
  31.             data = resp.read(4096)
  32.             if not data:
  33.                 break
  34.             outf.write(data)
  35.         rc = 0
  36.     else:
  37.         print >>stderr, "Error, got %s %s" % (resp.status, resp.reason)
  38.         print >>stderr, resp.read()
  39.         rc = 1
  40. 
  41.     if close_outf:
  42.         outf.close()
  43. 
  44.     return rc