1
2
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
16
17 import sys, os, getopt, traceback, mimetypes
18
19 import node
20
21
22
23 progname = sys.argv[0]
24
25
26
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
38
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
71
73 """
74 Front end for fcpget utility
75 """
76
77 verbosity = node.ERROR
78 verbose = False
79 fcpHost = node.defaultFCPHost
80 fcpPort = node.defaultFCPPort
81
82 opts = {
83 "Verbosity" : 0,
84 }
85
86
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
96 usage()
97 sys.exit(2)
98 output = None
99 verbose = False
100
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
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
134 if len(args) != 2:
135 usage("Invalid number of arguments")
136 uriSrc = args[0].strip()
137 uriDest = args[1].strip()
138
139
140 uriPub = n.redirect(uriSrc, uriDest)
141
142 n.shutdown()
143
144
145 sys.stdout.write(uriPub)
146 sys.stdout.flush()
147
148
149 sys.exit(0)
150
151
152
153
154
155
156