Package fcp :: Module redirect
[hide private]
[frames] | no frames]

Source Code for Module fcp.redirect

  1  #@+leo-ver=4 
  2  #@+node:@file redirect.py 
  3  """ 
  4  Insert a redirect from a uri to an existing URI 
  5   
  6  This is the guts of the command-line front-end app fcpredirect 
  7   
  8  Example usage: 
  9   
 10  $ fcpredirect KSK@darknet USK@PFeLTa1si2Ml5sDeUy7eDhPso6TPdmw-2gWfQ4Jg02w,3ocfrqgUMVWA2PeorZx40TW0c-FiIOL-TWKQHoDbVdE,AQABAAE/Index/35/ 
 11   
 12  Inserts key 'KSK@darknet', as a redirect to the 'darknet index' freesite 
 13  """ 
 14   
 15  #@+others 
 16  #@+node:imports 
 17  import sys, os, getopt, traceback, mimetypes 
 18   
 19  import node 
 20   
 21  #@-node:imports 
 22  #@+node:globals 
 23  progname = sys.argv[0] 
 24   
 25  #@-node:globals 
 26  #@+node:usage 
27 -def usage(msg=None, ret=1):
28 """ 29 Prints usage message then exits 30 """ 31 if msg: 32 sys.stderr.write(msg+"\n") 33 sys.stderr.write("Usage: %s [options] src-uri target-uri\n" % progname) 34 sys.stderr.write("Type '%s -h' for help\n" % progname) 35 sys.exit(ret)
36 37 #@-node:usage 38 #@+node:help
39 -def help():
40 """ 41 print help options, then exit 42 """ 43 print "%s: inserts a key, as a redirect to another key" % progname 44 print 45 print "Usage: %s [options] src-uri target-uri" % progname 46 print 47 print "Options:" 48 print " -h, -?, --help" 49 print " Print this help message" 50 print " -v, --verbose" 51 print " Print verbose progress messages to stderr" 52 print " -H, --fcpHost=<hostname>" 53 print " Connect to FCP service at host <hostname>" 54 print " -P, --fcpPort=<portnum>" 55 print " Connect to FCP service at port <portnum>" 56 print " -V, --version" 57 print " Print version number and exit" 58 print 59 print "Example:" 60 print " %s KSK@foo KSK@bar" % progname 61 print " Inserts key KSK@foo, which when retrieved will redirect to KSK@bar" 62 print " Prints resulting URI (in this case KSK@foo) to stdout" 63 print 64 print "Environment:" 65 print " Instead of specifying -H and/or -P, you can define the environment" 66 print " variables FCP_HOST and/or FCP_PORT respectively" 67 68 sys.exit(0)
69 70 #@-node:help 71 #@+node:main
72 -def main():
73 """ 74 Front end for fcpget utility 75 """ 76 # default job options 77 verbosity = node.ERROR 78 verbose = False 79 fcpHost = node.defaultFCPHost 80 fcpPort = node.defaultFCPPort 81 82 opts = { 83 "Verbosity" : 0, 84 } 85 86 # process command line switches 87 try: 88 cmdopts, args = getopt.getopt( 89 sys.argv[1:], 90 "?hvH:P:V", 91 ["help", "verbose", "fcpHost=", "fcpPort=", "version", 92 ] 93 ) 94 except getopt.GetoptError: 95 # print help information and exit: 96 usage() 97 sys.exit(2) 98 output = None 99 verbose = False 100 #print cmdopts 101 for o, a in cmdopts: 102 103 if o in ("-?", "-h", "--help"): 104 help() 105 106 if o in ("-V", "--version"): 107 print "This is %s, version %s" % (progname, node.fcpVersion) 108 sys.exit(0) 109 110 if o in ("-v", "--verbosity"): 111 verbosity = node.DETAIL 112 opts['Verbosity'] = 1023 113 verbose = True 114 115 if o in ("-H", "--fcpHost"): 116 fcpHost = a 117 118 if o in ("-P", "--fcpPort"): 119 try: 120 fcpPort = int(a) 121 except: 122 usage("Invalid fcpPort argument %s" % repr(a)) 123 124 # try to create the node 125 try: 126 n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, 127 logfile=sys.stderr) 128 except: 129 if verbose: 130 traceback.print_exc(file=sys.stderr) 131 usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) 132 133 # determine the uris 134 if len(args) != 2: 135 usage("Invalid number of arguments") 136 uriSrc = args[0].strip() 137 uriDest = args[1].strip() 138 139 # do the invert 140 uriPub = n.redirect(uriSrc, uriDest) 141 142 n.shutdown() 143 144 # successful, return the uri 145 sys.stdout.write(uriPub) 146 sys.stdout.flush() 147 148 # all done 149 sys.exit(0)
150 151 #@-node:main 152 #@-others 153 154 #@-node:@file redirect.py 155 #@-leo 156