diff options
Diffstat (limited to 'rp/utils/hashdir')
-rwxr-xr-x | rp/utils/hashdir | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/rp/utils/hashdir b/rp/utils/hashdir index d3fe393c..c2c100b8 100755 --- a/rp/utils/hashdir +++ b/rp/utils/hashdir @@ -26,42 +26,40 @@ distributed as part of the repository system. import os import sys -import argparse -import rpki.POW +import rpki.config + +from rpki.rcynicdb.iterator import authenticated_objects def check_dir(s): - if os.path.isdir(s): - return os.path.abspath(s) - else: - raise argparse.ArgumentTypeError("%r is not a directory" % s) + if os.path.isdir(s): + return os.path.abspath(s) + else: + raise argparse.ArgumentTypeError("{!r} is not a directory".format(s)) -parser = argparse.ArgumentParser(description = __doc__) -parser.add_argument("-v", "--verbose", action = "store_true", help = "whistle while you work") -parser.add_argument("rcynic_dir", type = check_dir, help = "rcynic authenticated output directory") -parser.add_argument("output_dir", help = "name of output directory to create") -args = parser.parse_args() +cfg = rpki.config.argparser(doc = __doc__) +cfg.argparser.add_argument("-v", "--verbose", action = "store_true", help = "whistle while you work") +cfg.argparser.add_argument("rcynic_dir", nargs = "?", type = check_dir, help = "rcynic authenticated output directory") +cfg.argparser.add_argument("output_dir", help = "name of output directory to create") +args = cfg.argparser.parse_args() if not os.path.isdir(args.output_dir): - os.makedirs(args.output_dir) + os.makedirs(args.output_dir) -for root, dirs, files in os.walk(args.rcynic_dir): - for ifn in files: - ifn = os.path.join(root, ifn) - if ifn.endswith(".cer"): - obj = rpki.POW.X509.derReadFile(ifn) - fmt = "%08x.%%d" % obj.getSubjectHash() - elif ifn.endswith(".crl"): - obj = rpki.POW.CRL.derReadFile(ifn) - fmt = "%08x.r%%d" % obj.getIssuerHash() - else: - continue +def store(uri, obj, fmt): for i in xrange(1000000): - ofn = os.path.join(args.output_dir, fmt % i) - if not os.path.exists(ofn): - with open(ofn, "w") as f: - f.write(obj.pemWrite()) - if args.verbose: - print ofn, "<=", ifn - break + fn = os.path.join(args.output_dir, fmt.format(i)) + if os.path.exists(fn): + continue + with open(fn, "w") as f: + f.write(obj.pemWrite()) + if args.verbose: + print fn, "<=", uri + return else: - sys.exit("No path name available for %s (%s)" % (ifn, ofn)) + sys.exit("No path name available for {} ({})".format(uri, fn)) + +for uri, cer in authenticated_objects(uri_suffix = ".cer"): + store(uri, cer, "{:08x}.{{:d}}".format(cer.getSubjectHash())) + +for uri, crl in authenticated_objects(uri_suffix = ".crl"): + store(uri, crl, "{:08x}.r{{:d}}".format(crl.getIssuerHash())) |