def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None nowait = False opts = { "Verbosity" : 0, "persistence" : "connection", "async" : False, "priority" : 3, "MaxRetries" : -1, } # process command line switches try: cmdopts, args = getopt.getopt( sys.argv[1:], "?hvH:P:m:gcdp:nr:t:V", ["help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global","compress","disk", "persistence=", "nowait", "priority=", "timeout=", "version", ] ) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest=False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d","--disk"): makeDDARequest=True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage("Persistence must be one of 'connection', 'reboot', 'forever'") opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-n", "--nowait"): opts['async'] = True nowait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri # determine where to get input if nargs == 1 or args[1] == '-': infile = None else: infile = args[1] # figure out a mimetype if none present if infile and not mimetype: filename = os.path.basename(infile) if filename: mimetype = mimetypes.guess_type(filename)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) TestDDARequest=False if makeDDARequest: if infile is not None: ddareq=dict() ddafile = os.path.abspath(infile) ddareq["Directory"]= os.path.dirname(ddafile) ddareq["WantReadDirectory"]="True" ddareq["WantWriteDirectory"]="false" print "Absolute filepath used for node direct disk access :",ddareq["Directory"] print "File to insert :",os.path.basename( ddafile ) TestDDARequest=n.testDDA(**ddareq) print "Result of dda request :",TestDDARequest if TestDDARequest: opts["file"]=ddafile uri = n.put(uri,**opts) else: sys.stderr.write("%s: disk access failed to insert file %s fallback to direct\n" % (progname,ddafile) ) else: sys.stderr.write("%s: disk access need a disk filename\n" % progname ) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) uri = n.put(uri, data=data, **opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if nowait: # got back a job ticket, wait till it has been sent uri.waitTillReqSent() else: # successful, return the uri sys.stdout.write(uri) sys.stdout.flush() n.shutdown() # all done sys.exit(0)
def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None wait = False opts = { "Verbosity" : 0, "persistence" : "forever", "async" : True, "priority" : 3, "Global": "true", "MaxRetries" : -1, } # process command line switches try: cmdopts, args = getopt.getopt( sys.argv[1:], "?hvH:P:m:gcdp:wr:et:V", ["help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global","compress","disk", "persistence=", "wait", "priority=", "realtime", "timeout=", "version", ] ) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest=False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d","--disk"): makeDDARequest=True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage("Persistence must be one of 'connection', 'reboot', 'forever'") opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-w", "--wait"): opts['async'] = False wait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-e", "--realtime"): opts['realtime'] = True elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") keytypes = ["USK", "KSK", "SSK", "CHK"] if nargs == 2: infile = args[1] uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri if not uri[len("freenet:"):len("freenet:")+3] in keytypes: print uri, uri[len("freenet:"):len("freenet:")+4] usage("The first argument must be a key. Example: CHK@/<filename>") else: # if no infile is given, automatically upload to a CHK key. infile = args[0] uri = "freenet:CHK@/" + node.toUrlsafe(infile) # if we got an infile, but the key does not have the filename, use that filename for the uri. if infile and uri[-2:] == "@/" and uri[:3] in keytypes: uri += node.toUrlsafe(infile) # figure out a mimetype if none present if infile and not mimetype: base, ext = os.path.splitext(infile) if ext: mimetype = mimetypes.guess_type(ext)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) # FIXME: Throw out all the TestDDARequest stuff. It is not needed for putting a single file. TestDDARequest=False #: The key of the uploaded file. freenet_uri = None if makeDDARequest: if infile is not None: ddareq=dict() ddafile = os.path.abspath(infile) ddareq["Directory"]= os.path.dirname(ddafile) # FIXME: This does not work. The only reason why testDDA # works is because there is an alternate way of specifying # a content hash, and that way works. ddareq["WantReadDirectory"]="True" ddareq["WantWriteDirectory"]="false" print "Absolute filepath used for node direct disk access :",ddareq["Directory"] print "File to insert :",os.path.basename( ddafile ) TestDDARequest=n.testDDA(**ddareq) if TestDDARequest: opts["file"]=ddafile putres = n.put(uri,**opts) else: sys.stderr.write("%s: disk access failed to insert file %s fallback to direct\n" % (progname,ddafile) ) else: sys.stderr.write("%s: disk access needs a disk filename\n" % progname ) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) # give it the file anyway: Put is more intelligent than this script. opts["data"] = data if infile: opts["file"] = infile n.listenGlobal() putres = n.put(uri, **opts) if not wait: opts["chkonly"] = True opts["async"] = False # force the node to be fast opts["priority"] = 0 opts["realtime"] = True opts["persistence"] = "connection" opts["Global"] = False freenet_uri = n.put(uri,**opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if not wait: # got back a job ticket, wait till it has been sent putres.waitTillReqSent() else: # successful, return the uri sys.stdout.write(putres) sys.stdout.flush() n.shutdown() # output the key of the file if not wait: print freenet_uri # all done sys.exit(0)
def main(argv=sys.argv[1:]): """ Front end for fcpget utility """ # default job options #verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort Global = False verbosity = node.ERROR opts = { "Verbosity" : 0, "persistence" : "connection", "priority" : 3, } # process command line switches try: cmdopts, args = getopt.getopt( argv, "?hvH:P:gp:r:t:V", ["help", "verbose", "fcpHost=", "fcpPort=", "global", "persistence=", "priority=", "timeout=", "version", ] ) except getopt.GetoptError: #traceback.print_exc() # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts for o, a in cmdopts: if o in ("-?", "-h", "--help"): help() if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) if o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True if o in ("-H", "--fcpHost"): fcpHost = a if o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) if o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage("Persistence must be one of 'connection', 'reboot', 'forever'") opts['persistence'] = a if o in ("-g", "--global"): opts['Global'] = "true" if o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) if o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout print "timeout=%s" % timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri # determine where to put output if nargs == 1: outfile = None else: outfile = args[1] # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, Global=Global, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) # try to retrieve the key try: # print "opts=%s" % opts mimetype, data, msg = n.get(uri, **opts) n.shutdown() except: if verbose: traceback.print_exc(file=sys.stderr) sys.stderr.write("%s: Failed to retrieve key %s\n" % (progname, repr(uri))) n.shutdown() sys.exit(1) # try to dispose of the data if outfile: # figure out an extension, if none given base, ext = os.path.splitext(outfile) if not ext: ext = mimetypes.guess_extension(mimetype) if not ext: ext = "" outfile = base + ext # try to save to file try: f = file(outfile, "wb") f.write(data) f.close() if verbose: sys.stderr.write("Saved key to file %s\n" % outfile) except: # save failed if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to write data to output file %s" % repr(outfile)) else: # no output file given, dump to stdout sys.stdout.write(data) sys.stdout.flush() # all done try: n.shutdown() except: pass sys.exit(0)
def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None wait = False opts = { "Verbosity": 0, "persistence": "forever", "async": True, "priority": 3, "Global": "true", "MaxRetries": -1, } # process command line switches try: cmdopts, args = getopt.getopt(sys.argv[1:], "?hvH:P:m:gcdp:wr:et:V", [ "help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global", "compress", "disk", "persistence=", "wait", "priority=", "realtime", "timeout=", "version", ]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest = False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d", "--disk"): makeDDARequest = True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage( "Persistence must be one of 'connection', 'reboot', 'forever'" ) opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-w", "--wait"): opts['async'] = False wait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-e", "--realtime"): opts['realtime'] = True elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") keytypes = ["USK", "KSK", "SSK", "CHK"] if nargs == 2: infile = args[1] uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri if not uri[len("freenet:"):len("freenet:") + 3] in keytypes: print uri, uri[len("freenet:"):len("freenet:") + 4] usage("The first argument must be a key. Example: CHK@/<filename>") else: # if no infile is given, automatically upload to a CHK key. infile = args[0] uri = "freenet:CHK@/" + node.toUrlsafe(infile) # if we got an infile, but the key does not have the filename, use that filename for the uri. if infile and uri[-2:] == "@/" and uri[:3] in keytypes: uri += node.toUrlsafe(infile) # figure out a mimetype if none present if infile and not mimetype: base, ext = os.path.splitext(infile) if ext: mimetype = mimetypes.guess_type(ext)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) # FIXME: Throw out all the TestDDARequest stuff. It is not needed for putting a single file. TestDDARequest = False #: The key of the uploaded file. freenet_uri = None if makeDDARequest: if infile is not None: ddareq = dict() ddafile = os.path.abspath(infile) ddareq["Directory"] = os.path.dirname(ddafile) # FIXME: This does not work. The only reason why testDDA # works is because there is an alternate way of specifying # a content hash, and that way works. ddareq["WantReadDirectory"] = "True" ddareq["WantWriteDirectory"] = "false" print "Absolute filepath used for node direct disk access :", ddareq[ "Directory"] print "File to insert :", os.path.basename(ddafile) TestDDARequest = n.testDDA(**ddareq) if TestDDARequest: opts["file"] = ddafile putres = n.put(uri, **opts) else: sys.stderr.write( "%s: disk access failed to insert file %s fallback to direct\n" % (progname, ddafile)) else: sys.stderr.write("%s: disk access needs a disk filename\n" % progname) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) # give it the file anyway: Put is more intelligent than this script. opts["data"] = data if infile: opts["file"] = infile n.listenGlobal() putres = n.put(uri, **opts) if not wait: opts["chkonly"] = True opts["async"] = False # force the node to be fast opts["priority"] = 0 opts["realtime"] = True opts["persistence"] = "connection" opts["Global"] = False freenet_uri = n.put(uri, **opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if not wait: # got back a job ticket, wait till it has been sent putres.waitTillReqSent() else: # successful, return the uri sys.stdout.write(putres) sys.stdout.flush() n.shutdown() # output the key of the file if not wait: print freenet_uri # all done sys.exit(0)
def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None nowait = False opts = { "Verbosity" : 0, "persistence" : "connection", "async" : False, "priority" : 3, "MaxRetries" : -1, } # process command line switches try: cmdopts, args = getopt.getopt( sys.argv[1:], "?hvH:P:m:gcdp:nr:t:V", ["help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global","compress","disk", "persistence=", "nowait", "priority=", "timeout=", "version", ] ) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest=False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d","--disk"): makeDDARequest=True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage("Persistence must be one of 'connection', 'reboot', 'forever'") opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-n", "--nowait"): opts['async'] = True nowait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri # determine where to get input if nargs == 1 or args[1] == '-': infile = None else: infile = args[1] # figure out a mimetype if none present if infile and not mimetype: filename = os.path.basename(infile) if filename: mimetype = mimetypes.guess_type(filename)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) if makeDDARequest: if infile is not None: ddareq=dict() ddafile = os.path.abspath(infile) ddareq["Directory"]= os.path.dirname(ddafile) ddareq["WantReadDirectory"]="True" ddareq["WantWriteDirectory"]="False" print "Absolute filepath used for node direct disk access :",ddareq["Directory"] print "File to insert :",os.path.basename( ddafile ) TestDDARequest=n.testDDA(**ddareq) print "Result of dda request :",TestDDARequest if TestDDARequest: opts["file"]=ddafile uri = n.put(uri,**opts) else: sys.stderr.write("%s: disk access failed to insert file %s fallback to direct\n" % (progname,ddafile) ) else: sys.stderr.write("%s: disk access need a disk filename\n" % progname ) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) uri = n.put(uri, data=data, **opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if nowait: # got back a job ticket, wait till it has been sent uri.waitTillReqSent() else: # successful, return the uri sys.stdout.write(uri) sys.stdout.flush() n.shutdown() # all done sys.exit(0)