1
2
3 """
4 put a key
5
6 This is the guts of the command-line front-end app fcpput
7 """
8
9
10
11 import sys, os, getopt, traceback, mimetypes
12
13 import node
14
15
16
17 progname = sys.argv[0]
18
19
20
21 -def usage(msg=None, ret=1):
22 """
23 Prints usage message then exits
24 """
25 if msg:
26 sys.stderr.write(msg+"\n")
27 sys.stderr.write("Usage: %s [options] key_uri [filename]\n" % progname)
28 sys.stderr.write("Type '%s -h' for help\n" % progname)
29 sys.exit(ret)
30
31
32
34 """
35 print help options, then exit
36 """
37 print "%s: a simple command-line freenet key insertion command" % progname
38 print "Usage: %s [options] key_uri [<filename>]" % progname
39 print
40 print "Arguments:"
41 print " <key_uri>"
42 print " A freenet key URI, such as 'freenet:KSK@gpl.txt'"
43 print " Note that the 'freenet:' part may be omitted if you feel lazy"
44 print " <filename>"
45 print " The filename from which to source the key's data. If this filename"
46 print " is '-', or is not given, then the data will be sourced from"
47 print " standard input"
48 print
49 print "Options:"
50 print " -h, -?, --help"
51 print " Print this help message"
52 print " -v, --verbose"
53 print " Print verbose progress messages to stderr, do -v twice for more detail"
54 print " -H, --fcpHost=<hostname>"
55 print " Connect to FCP service at host <hostname>"
56 print " -P, --fcpPort=<portnum>"
57 print " Connect to FCP service at port <portnum>"
58 print " -m, --mimetype=<mimetype>"
59 print " The mimetype under which to insert the key. If not given, then"
60 print " an attempt will be made to guess it from the filename. If no"
61 print " filename is given, or if this attempt fails, the mimetype"
62 print " 'text/plain' will be used as a fallback"
63 print " -c, --compress"
64 print " Enable compression of inserted data (default is no compression)"
65 print " -d, --disk"
66 print " Try to have the node access file on disk directly , it will try then a fallback is provided"
67 print " nb:give the path relative to node filesystem not from where you're running this program"
68 print " -p, --persistence="
69 print " Set the persistence type, one of 'connection', 'reboot' or 'forever'"
70 print " -g, --global"
71 print " Do it on the FCP global queue"
72 print " -n, --nowait"
73 print " Don't wait for completion, exit immediately"
74 print " -r, --priority"
75 print " Set the priority (0 highest, 6 lowest, default 3)"
76 print " -t, --timeout="
77 print " Set the timeout, in seconds, for completion. Default one year"
78 print " -V, --version"
79 print " Print version number and exit"
80 print
81 print "Environment:"
82 print " Instead of specifying -H and/or -P, you can define the environment"
83 print " variables FCP_HOST and/or FCP_PORT respectively"
84
85
86
87
89 """
90 Front end for fcpput utility
91 """
92
93 verbosity = node.ERROR
94 verbose = False
95 fcpHost = node.defaultFCPHost
96 fcpPort = node.defaultFCPPort
97 mimetype = None
98 nowait = False
99
100 opts = {
101 "Verbosity" : 0,
102 "persistence" : "connection",
103 "async" : False,
104 "priority" : 3,
105 "MaxRetries" : -1,
106 }
107
108
109 try:
110 cmdopts, args = getopt.getopt(
111 sys.argv[1:],
112 "?hvH:P:m:gcdp:nr:t:V",
113 ["help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global","compress","disk",
114 "persistence=", "nowait",
115 "priority=", "timeout=", "version",
116 ]
117 )
118 except getopt.GetoptError:
119
120 usage()
121 sys.exit(2)
122 output = None
123 verbose = False
124
125
126 makeDDARequest=False
127 opts['nocompress'] = True
128
129 for o, a in cmdopts:
130 if o in ("-V", "--version"):
131 print "This is %s, version %s" % (progname, node.fcpVersion)
132 sys.exit(0)
133
134 elif o in ("-?", "-h", "--help"):
135 help()
136 sys.exit(0)
137
138 elif o in ("-v", "--verbosity"):
139 if verbosity < node.DETAIL:
140 verbosity = node.DETAIL
141 else:
142 verbosity += 1
143 opts['Verbosity'] = 1023
144 verbose = True
145
146 elif o in ("-H", "--fcpHost"):
147 fcpHost = a
148
149 elif o in ("-P", "--fcpPort"):
150 try:
151 fcpPort = int(a)
152 except:
153 usage("Invalid fcpPort argument %s" % repr(a))
154
155 elif o in ("-m", "--mimetype"):
156 mimetype = a
157
158 elif o in ("-c", "--compress"):
159 opts['nocompress'] = False
160
161 elif o in ("-d","--disk"):
162 makeDDARequest=True
163
164 elif o in ("-p", "--persistence"):
165 if a not in ("connection", "reboot", "forever"):
166 usage("Persistence must be one of 'connection', 'reboot', 'forever'")
167 opts['persistence'] = a
168
169 elif o in ("-g", "--global"):
170 opts['Global'] = "true"
171
172 elif o in ("-n", "--nowait"):
173 opts['async'] = True
174 nowait = True
175
176 elif o in ("-r", "--priority"):
177 try:
178 pri = int(a)
179 if pri < 0 or pri > 6:
180 raise hell
181 except:
182 usage("Invalid priority '%s'" % pri)
183 opts['priority'] = int(a)
184
185 elif o in ("-t", "--timeout"):
186 try:
187 timeout = node.parseTime(a)
188 except:
189 usage("Invalid timeout '%s'" % a)
190 opts['timeout'] = timeout
191
192
193 nargs = len(args)
194 if nargs < 1 or nargs > 2:
195 usage("Invalid number of arguments")
196
197 uri = args[0]
198 if not uri.startswith("freenet:"):
199 uri = "freenet:" + uri
200
201
202 if nargs == 1 or args[1] == '-':
203 infile = None
204 else:
205 infile = args[1]
206
207
208 if infile and not mimetype:
209 filename = os.path.basename(infile)
210 if filename:
211 mimetype = mimetypes.guess_type(filename)[0]
212
213 if mimetype:
214
215
216
217
218 opts['mimetype'] = mimetype
219
220
221 try:
222 n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity,
223 logfile=sys.stderr)
224 except:
225 if verbose:
226 traceback.print_exc(file=sys.stderr)
227 usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort))
228
229
230 TestDDARequest=False
231
232 if makeDDARequest:
233 if infile is not None:
234 ddareq=dict()
235 ddafile = os.path.abspath(infile)
236
237 ddareq["Directory"]= os.path.dirname(ddafile)
238 ddareq["WantReadDirectory"]="True"
239 ddareq["WantWriteDirectory"]="false"
240 print "Absolute filepath used for node direct disk access :",ddareq["Directory"]
241 print "File to insert :",os.path.basename( ddafile )
242 TestDDARequest=n.testDDA(**ddareq)
243 print "Result of dda request :",TestDDARequest
244
245 if TestDDARequest:
246 opts["file"]=ddafile
247 uri = n.put(uri,**opts)
248 else:
249 sys.stderr.write("%s: disk access failed to insert file %s fallback to direct\n" % (progname,ddafile) )
250 else:
251 sys.stderr.write("%s: disk access need a disk filename\n" % progname )
252
253
254 if not TestDDARequest:
255
256 if not infile:
257 data = sys.stdin.read()
258 else:
259 try:
260 data = file(infile, "rb").read()
261 except:
262 n.shutdown()
263 usage("Failed to read input from file %s" % repr(infile))
264
265 try:
266
267 uri = n.put(uri, data=data, **opts)
268 except:
269 if verbose:
270 traceback.print_exc(file=sys.stderr)
271 n.shutdown()
272 sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri)))
273 sys.exit(1)
274
275 if nowait:
276
277 uri.waitTillReqSent()
278 else:
279
280 sys.stdout.write(uri)
281 sys.stdout.flush()
282
283 n.shutdown()
284
285
286 sys.exit(0)
287
288
289
290
291
292
293