Ticket #1873: 0001-Add-a-show-caps-command-to-the-CLI.patch

File 0001-Add-a-show-caps-command-to-the-CLI.patch, 4.3 KB (added by igor, at 2012-11-21T18:42:10Z)
  • src/allmydata/scripts/cli.py

    From 1dfacdc8a6ac3b0c6bc9ac77c5b948360b7e1331 Mon Sep 17 00:00:00 2001
    From: Igor Almeida <igor.contato@gmail.com>
    Date: Wed, 21 Nov 2012 15:35:23 -0300
    Subject: [PATCH] Add a show-caps command to the CLI
    
    This command lists the available capabilities a path you would give to
    'tahoe ls' possesses.
    ---
     src/allmydata/scripts/cli.py            |   16 ++++++++
     src/allmydata/scripts/tahoe_showcaps.py |   62 +++++++++++++++++++++++++++++++
     2 files changed, 78 insertions(+), 0 deletions(-)
     create mode 100644 src/allmydata/scripts/tahoe_showcaps.py
    
    diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py
    index 6758fe7..d9d7d59 100644
    a b class DeepCheckOptions(VDriveOptions): 
    481481    (which must be a directory), like 'tahoe check' but for multiple files.
    482482    Optionally repair any problems found."""
    483483
     484class ShowCapsOptions(VDriveOptions):
     485    optFlags = []
     486
     487    def parseArgs(self, where=""):
     488        self.where = argv_to_unicode(where)
     489
     490    longdesc = """Given an alias, show its capability and the ones that can be
     491    derived from it."""
     492
    484493subCommands = [
    485494    ["mkdir", None, MakeDirectoryOptions, "Create a new directory."],
    486495    ["add-alias", None, AddAliasOptions, "Add a new alias cap."],
    subCommands = [ 
    500509    ["stats", None, StatsOptions, "Print statistics about all files/directories in a subtree."],
    501510    ["check", None, CheckOptions, "Check a single file or directory."],
    502511    ["deep-check", None, DeepCheckOptions, "Check all files/directories reachable from a starting point."],
     512    ["show-caps", None, ShowCapsOptions, "Show all caps an alias can give."],
    503513    ]
    504514
    505515def mkdir(options):
    def deepcheck(options): 
    599609    rc = tahoe_check.deepcheck(options)
    600610    return rc
    601611
     612def showcaps(options):
     613    from allmydata.scripts import tahoe_showcaps
     614    rc = tahoe_showcaps.show(options)
     615    return rc
     616
    602617dispatch = {
    603618    "mkdir": mkdir,
    604619    "add-alias": add_alias,
    dispatch = { 
    618633    "stats": stats,
    619634    "check": check,
    620635    "deep-check": deepcheck,
     636    "show-caps": showcaps,
    621637    }
  • new file src/allmydata/scripts/tahoe_showcaps.py

    diff --git a/src/allmydata/scripts/tahoe_showcaps.py b/src/allmydata/scripts/tahoe_showcaps.py
    new file mode 100644
    index 0000000..3809203
    - +  
     1
     2import urllib
     3import simplejson
     4from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
     5                                     UnknownAliasError
     6from allmydata.scripts.common_http import do_http, format_http_error
     7from allmydata.util.encodingutil import quote_output, is_printable_ascii
     8
     9def show(options):
     10    nodeurl = options['node-url']
     11    options['json'] = True
     12    aliases = options.aliases
     13    where = options.where
     14    stdout = options.stdout
     15    stderr = options.stderr
     16
     17    if not nodeurl.endswith("/"):
     18        nodeurl += "/"
     19    if where.endswith("/"):
     20        where = where[:-1]
     21    try:
     22        rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS)
     23    except UnknownAliasError, e:
     24        e.display(stderr)
     25        return 1
     26    url = nodeurl + "uri/%s" % urllib.quote(rootcap)
     27    if path:
     28        # move where.endswith check here?
     29        url += "/" + escape_path(path)
     30    assert not url.endswith("/")
     31    url += "?t=json"
     32    resp = do_http("GET", url)
     33    if resp.status == 404:
     34        print >>stderr, "No such file or directory"
     35        return 2
     36    if resp.status != 200:
     37        print >>stderr, format_http_error("Error during GET", resp)
     38        if resp.status == 0:
     39            return 3
     40        else:
     41            return resp.status
     42
     43    data = resp.read()
     44
     45    # The webapi server should always output printable ASCII.
     46    # XXX is this necessary?
     47    if not is_printable_ascii(data):
     48        print >>stderr, "The JSON response contained unprintable characters:\n%s" % quote_output(data)
     49        return 1
     50
     51    try:
     52        parsed = simplejson.loads(data)
     53    except Exception, e:
     54        print >>stderr, "error: %s" % quote_output(e.args[0], quotemarks=False)
     55        print >>stderr, "Could not parse JSON response:\n%s" % quote_output(data)
     56        return 1
     57
     58    for key in ("rw_uri", "ro_uri", "verify_uri"):
     59        if key in parsed[1]:
     60            print >>stdout,'{}: {}'.format(key, parsed[1][key])
     61
     62    return 0