def py_nipub(): """ @brief Command line program to perform a NetInf 'publish' operation using http @brief convergence layer. Uses NIproc global instance of NI operations class Run: > nipub.py --help to see usage and options. Exit code is 0 for success, 1 if HTTP returned something except 200, and negative for local errors. """ # Options parsing and verification stuff usage = "%%prog %s\n %%prog %s\n%s\n %%prog %s\n %%prog %s\n%s\n%s" % \ ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}", "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}", " -- publish file via NI URI over HTTP", "[-q] [-e] [-j|-v|-w|-p] -u <HTTP URI of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}", "[-q] [-e] [-j|-v|-w|-p] [-u <HTTP URI of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}", " -- publish web content via NI URI over HTTP", "Send response as HTML document (-w), plain text (-p), or JSON (-v or -j)\n" "Unless -q is specified, the response is sent to standard output.\n" "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n" "If none of -j, -v, -w or -p are specified, a raw JSON response will be requested.") parser = OptionParser(usage) parser.add_option("-f", "--file", dest="file_name", type="string", help="Pathname for local file to be published.") parser.add_option("-u", "--uri", dest="http_name", type="string", help="HTTP URL for content to be published.") parser.add_option("-d", "--digest", dest="hash_alg", type="string", help="Digest algorithm to be used to hash content " "and create NI URI. Defaults to sha-256.") parser.add_option("-n", "--name", dest="ni_name", type="string", help="Complete ni name. If specified with a file or " "HTTP URL, the digest generated from the content " "will be checked against th digest in the name.") parser.add_option("-e", "--ext", dest="ext", type="string", help="A JSON encoded object to be sent as the 'ext' " "parameter for the Publish message.") parser.add_option("-l", "--loc", dest="locs", action="append", type="string", help="An FQDN where NI might be retrieved. Maybe be " "zero to two if -n is present and has a non-empty netloc. " "Otherwise must be one or two. HTTP is sent to first " "loc if no authority in -n.") parser.add_option("-q", "--quiet", default=False, action="store_true", dest="quiet", help="Suppress textual output") parser.add_option("-j", "--json", default=False, action="store_true", dest="json_raw", help="Request response as JSON string and output raw JSON " "string returned on stdout.") parser.add_option("-v", "--view", default=False, action="store_true", dest="json_pretty", help="Request response as JSON string and pretty print " "JSON string returned on stdout.") parser.add_option("-w", "--web", default=False, action="store_true", dest="html", help="Request response as HTML document and output HTML " "returned on stdout.") parser.add_option("-p", "--plain", default=False, action="store_true", dest="plain", help="Request response as plain text document and output text " "returned on stdout.") (options, args) = parser.parse_args() # Check command line options: # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l. # Either -d or -n must be specified. # If -d is specified, there must be either a -f or a -u but not both at once. # If -n is specified, one of -f or -u may be specified. No leftover arguments allowed. # Specifying more than one of -w, -p, -j and -v is inappropriate. if len(args) != 0: parser.error("Unrecognized arguments %s supplied." % str(args)) sys.exit(-1) if ((options.locs is not None) and (len(options.locs) > 2)): parser.error("Initial version only supports two locators (-l/--loc).") sys.exit(-1) if ((options.ni_name == None) and (options.locs == None)): parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.") sys.exit(-1) if ((options.hash_alg != None) and (options.ni_name != None)): parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).") sys.exit(-1) if ((options.hash_alg == None) and (options.ni_name == None)): parser.error("Must specify either digest algorithm to be used (-d) or complete ni name with algorithm and digest (-n).") sys.exit(-1) if ((((options.ni_name == None) and (options.file_name == None) and (options.http_name == None))) or ((options.file_name != None) and (options.http_name != None))): parser.error("Exactly one of -f/--file and -u/--uri must be specified with -d and optionally with -n.") sys.exit(-1) fc = 0 for flag in [options.json_raw, options.json_pretty, options.html, options.plain]: if flag: fc += 1 if fc > 1: parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.") sys.exit(-1) file_name = None # **** -u is not implemented yet if options.http_name != None: target = options.http_name print "Web name as source(-u/--uri option) not yet implemented. Exiting" sys.exit(-2) if options.file_name != None: target = options.file_name file_name = options.file_name full_put = True else: target = None full_put = False debug("full_put: %s" %full_put) verbose = not options.quiet # If we have a full ni name (-n option) given.. if options.ni_name is not None: # Check the validity of the ni name try: ni_name = NIname(options.ni_name) except Exception, e: if verbose: print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name) sys.exit(-3) rv = ni_name.validate_ni_url() if rv != ni_errs.niSUCCESS: if verbose: print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name) sys.exit(-3) # Extract the scheme and hash algorithm from the name scheme = ni_name.get_scheme() hash_alg = ni_name.get_alg_name() # If the ni name has a netloc in it then that is where to send; if not must have a loc nl = ni_name.get_netloc() if ((nl == "") and (options.locs == None)): print("Error: name (-n/--name) mist have a netloc if no locator options given,") sys.exit(-4) if nl != "": destination = nl authority = nl else: destination = options.locs[0] authority = ""
def py_nipubalt(): """ @brief Command line program to perform a NetInf 'publish' operation using http @brief convergence layer. Uses NIproc global instance of NI operations class Run: > nipubalt.py --help to see usage and options. Exit code is 0 for success, 1 if HTTP returned something except 200, and negative for local errors. """ # Options parsing and verification stuff usage = "%%prog %s\n %%prog %s\n%s\n%s" % \ ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}", "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}", " -- publish file via NI URI over HTTP and/or DTN", "At least one locator must be given either as part of the -n option or via a -l option.\n" "Locators given with -l options can optionally be prefixed with the HTTP scheme (http://) or \n" "the DTN scheme (dtn://). If a -l option is given, this is used to determine the initial\n" "publication destination and the convergence layer used will be HTPP unless the -l option\n" "explicitly gives the DTN scheme prefix. If there are no -l options but the -n option has\n" "a netloc compnent (FQDN or IP address with optional port) the this will be used with the\n" "HTTP convergence layer\n" "The response will be sent as HTML document (-w), plain text (-p), or JSON (-v or -j)\n" "Unless -q is specified, the response is sent to standard output.\n" "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n" "If none of -j, -v, -w or -p are specified, a raw JSON response will be requested.") parser = OptionParser(usage) parser.add_option("-f", "--file", dest="file_name", type="string", help="Pathname for local file to be published.") parser.add_option("-d", "--digest", dest="hash_alg", type="string", help="Digest algorithm to be used to hash content " "and create NI URI. Defaults to sha-256.") parser.add_option("-n", "--name", dest="ni_name", type="string", help="Complete ni name. If specified with a file or " "HTTP URL, the digest generated from the content " "will be checked against th digest in the name.") parser.add_option("-e", "--ext", dest="ext", type="string", help="A JSON encoded object to be sent as the 'ext' " "parameter for the Publish message.") parser.add_option("-l", "--loc", dest="locs", action="append", type="string", help="A locator where NI might be retrieved. Maybe be " "zero to two if -n is present and has a non-empty netloc. " "Otherwise must be one or two. HTTP or DTN is sent to first " "loc if present. Otherwise sent to netloc (authority) in -n." "NOTE: this precedence differs from earlier versions of nipub.") parser.add_option("-q", "--quiet", default=False, action="store_true", dest="quiet", help="Suppress textual output") parser.add_option("-j", "--json", default=False, action="store_true", dest="json_raw", help="Request response as JSON string and output raw JSON " "string returned on stdout.") parser.add_option("-v", "--view", default=False, action="store_true", dest="json_pretty", help="Request response as JSON string and pretty print " "JSON string returned on stdout.") parser.add_option("-w", "--web", default=False, action="store_true", dest="html", help="Request response as HTML document and output HTML " "returned on stdout.") parser.add_option("-p", "--plain", default=False, action="store_true", dest="plain", help="Request response as plain text document and output text " "returned on stdout.") (options, args) = parser.parse_args() # Check command line options: # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l. # If -n option is specified then there must not be a -d. # If -d is specified, there must be a -f. # If -n is specified, -f may be specified - otherwise only metadata is published. No leftover arguments allowed. # Specifying more than one of -w, -p, -j and -v is inappropriate. if len(args) != 0: parser.error("Unrecognized arguments %s supplied." % str(args)) sys.exit(-1) if ((options.locs is not None) and (len(options.locs) > 2)): parser.error("Initial version only supports two locators (-l/--loc).") sys.exit(-1) if ((options.ni_name == None) and (options.locs == None)): parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.") sys.exit(-1) if ((options.hash_alg != None) and (options.ni_name != None)): parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).") sys.exit(-1) fc = 0 for flag in [options.json_raw, options.json_pretty, options.html, options.plain]: if flag: fc += 1 if fc > 1: parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.") sys.exit(-1) file_name = None if options.file_name != None: file_name = os.path.abspath(options.file_name) # Check the file is readable if not os.access(file_name, os.R_OK): if verbose: print("File to be published %s is not readable" % file_name) sys.exit(1) full_put = True else: full_put = False debug("full_put: %s" %full_put) verbose = not options.quiet if ((options.locs is not None) and (len(options.locs) > 2)): if verbose: print "Warning: only first two -l/--loc locators will be published" # If we have a full ni name (-n option) given.. if options.ni_name is not None: # Check the validity of the ni name try: ni_name = NIname(options.ni_name) except Exception, e: if verbose: print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name) sys.exit(-3) rv = ni_name.validate_ni_url() if rv != ni_errs.niSUCCESS: if verbose: print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name) sys.exit(-3) # Extract the scheme and hash algorithm from the name scheme = ni_name.get_scheme() hash_alg = ni_name.get_alg_name() # If there is a -l option, that is where the request is sent. nl = ni_name.get_netloc() if ((options.locs is None) and (nl == "")) : print("Error: name (-n/--name) must have a netloc if no locator options given,") sys.exit(-4) # NOTE: The following logic ie reversed from earlier versions so that # can force use of DTN convergence layer with a -l option. if nl == "": # Already checked this exists destination = options.locs[0] else: destination = nl authority = nl