command-line: fix all three commands and all two ways to invoke them to require node-url and give a useful usage string if node-url is absent or of the wrong form

This commit is contained in:
Zooko O'Whielacronx 2007-08-17 12:54:47 -07:00
parent 2e6039644f
commit bffd0c97f5
4 changed files with 40 additions and 21 deletions

View File

@ -1,8 +1,10 @@
import os.path, sys
import os.path, re, sys
from twisted.python import usage
from allmydata.scripts.common import BaseOptions
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
class VDriveOptions(BaseOptions, usage.Options):
optParameters = [
["vdrive", "d", "global",
@ -12,6 +14,10 @@ class VDriveOptions(BaseOptions, usage.Options):
"URL of the tahoe node to use, a URL like \"http://127.0.0.1:8888\""],
]
def postOptions(self):
if not isinstance(self['node-url'], basestring) or not NODEURL_RE.match(self['node-url']):
raise usage.UsageError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (self['node-url'],))
class ListOptions(VDriveOptions):
def parseArgs(self, vdrive_filename=""):
self['vdrive_filename'] = vdrive_filename

View File

@ -2,11 +2,13 @@
import sys, urllib
def get(server, vdrive, vdrive_file, local_file):
def get(nodeurl, vdrive, vdrive_file, local_file):
if not isinstance(nodeurl, basestring):
raise ValueError("nodeurl is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (nodeurl,))
if server[-1] != "/":
server += "/"
url = server + "vdrive/" + vdrive + "/"
if nodeurl[-1] != "/":
nodeurl += "/"
url = nodeurl + "vdrive/" + vdrive + "/"
if vdrive_file:
url += vdrive_file
@ -26,19 +28,23 @@ def get(server, vdrive, vdrive_file, local_file):
def main():
import optparse
import optparse, re
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")
parser.add_option("-u", "--nodeurl", dest="nodeurl")
(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,))
vdrive_file = args[0]
local_file = None
if len(args) > 1:
local_file = args[1]
get(options.server, options.vdrive, vdrive_file, local_file)
get(options.nodeurl, options.vdrive, vdrive_file, local_file)
if __name__ == '__main__':
main()

View File

@ -3,11 +3,10 @@
import urllib
import simplejson
def list(server, vdrive, vdrive_file):
if server[-1] != "/":
server += "/"
url = server + "vdrive/" + vdrive + "/"
def list(nodeurl, vdrive, vdrive_file):
if nodeurl[-1] != "/":
nodeurl += "/"
url = nodeurl + "vdrive/" + vdrive + "/"
if vdrive_file:
url += vdrive_file
url += "?t=json"
@ -30,18 +29,22 @@ def list(server, vdrive, vdrive_file):
def main():
import optparse
import optparse, re
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")
parser.add_option("-u", "--node-url", dest="nodeurl")
(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,))
vdrive_file = ""
if args:
vdrive_file = args[0]
list(options.server, options.vdrive, vdrive_file)
list(options.nodeurl, options.vdrive, vdrive_file)
if __name__ == '__main__':
main()

View File

@ -2,7 +2,7 @@
import re, socket, sys
SERVERURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
def put(nodeurl, vdrive, vdrive_fname, local_fname, verbosity):
"""
@ -13,7 +13,7 @@ def put(nodeurl, vdrive, vdrive_fname, local_fname, verbosity):
if not isinstance(nodeurl, basestring):
raise ValueError("nodeurl is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (nodeurl,))
mo = SERVERURL_RE.match(nodeurl)
mo = NODEURL_RE.match(nodeurl)
if not mo:
raise ValueError("nodeurl is required to look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (nodeurl,))
host = mo.group(1)
@ -78,19 +78,23 @@ def put(nodeurl, vdrive, vdrive_fname, local_fname, verbosity):
return 1
def main():
import optparse
import optparse, re
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")
parser.add_option("-u", "--node-url", dest="nodeurl")
(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,))
local_file = args[0]
vdrive_file = None
if len(args) > 1:
vdrive_file = args[1]
return put(options.server, options.vdrive, vdrive_file, local_file)
return put(options.nodeurl, options.vdrive, vdrive_file, local_file)
if __name__ == '__main__':
main()