cli: implement 'get'

This commit is contained in:
Brian Warner 2007-07-11 10:26:19 -07:00
parent 32166c360a
commit 9238c61224
3 changed files with 66 additions and 33 deletions

View File

@ -1,4 +1,5 @@
import os.path, sys
from twisted.python import usage
class VDriveOptions(usage.Options):
@ -19,10 +20,17 @@ class GetOptions(VDriveOptions):
self['vdrive_filename'] = vdrive_filename
self['local_filename'] = local_filename
def getSynopsis(self):
return "%s get VDRIVE_FILE LOCAL_FILE" % (os.path.basename(sys.argv[0]),)
longdesc = """Retrieve a file from the virtual drive and write it to the
local disk. If LOCAL_FILE is omitted or '-', the contents of the file
will be written to stdout."""
subCommands = [
["ls", None, ListOptions, "List a directory"],
["get", None, GetOptions, "Retrieve a file from the virtual drive"],
["get", None, GetOptions, "Retrieve a file from the virtual drive."],
]
def list(config, stdout, stderr):
@ -34,9 +42,21 @@ def list(config, stdout, stderr):
def get(config, stdout, stderr):
from allmydata.scripts import tahoe_get
vdrive_filename = config['vdrive_filename']
local_filename = config['local_filename']
rc = tahoe_get.get(config['server'],
config['vdrive'],
config['vdrive_filename'])
vdrive_filename,
local_filename)
if rc == 0:
if local_filename is None or local_filename == "-":
# be quiet, since the file being written to stdout should be
# proof enough that it worked, unless the user is unlucky
# enough to have picked an empty file
pass
else:
print >>stderr, "%s retrieved and written to %s" % \
(vdrive_filename, local_filename)
return rc
dispatch = {

View File

@ -1,31 +0,0 @@
#!/usr/bin/env python
import optparse, sys, urllib
def GET(url, outf):
f = urllib.urlopen(url)
outf.write(f.read())
parser = optparse.OptionParser()
parser.add_option("-d", "--vdrive", dest="vdrive", default="global")
parser.add_option("-s", "--server", dest="server", default="http://tahoebs1.allmydata.com:8011")
(options, args) = parser.parse_args()
vfname = args[0]
if len(args) == 1 or args[1] == "-":
targfname = None
else:
targfname = args[1]
base = options.server
base += "/vdrive/"
base += options.vdrive
base += "/"
url = base + vfname
if targfname is None:
GET(url, sys.stdout)
else:
GET(url, open(targfname, "wb"))

View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
import sys, urllib
def get(server, vdrive, vdrive_file, local_file):
if server[-1] != "/":
server += "/"
url = server + "vdrive/" + vdrive + "/"
if vdrive_file:
url += vdrive_file
if local_file is None or local_file == "-":
outf = sys.stdout
else:
outf = open(local_file, "wb")
inf = urllib.urlopen(url)
while True:
data = inf.read(4096)
if not data:
break
outf.write(data)
outf.close()
return 0
def main():
import optparse
parser = optparse.OptionParser()
parser.add_option("-d", "--vdrive", dest="vdrive", default="global")
parser.add_option("-s", "--server", dest="server", default="http://tahoebs1.allmydata.com:8011")
(options, args) = parser.parse_args()
vdrive_file = args[0]
local_file = None
if len(args) > 1:
local_file = args[1]
get(options.server, options.vdrive, vdrive_file, local_file)
if __name__ == '__main__':
main()