diff options
Diffstat (limited to 'rp/utils/hashdir')
-rwxr-xr-x | rp/utils/hashdir | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/rp/utils/hashdir b/rp/utils/hashdir index c7c18350..21619ce4 100755 --- a/rp/utils/hashdir +++ b/rp/utils/hashdir @@ -27,41 +27,39 @@ distributed as part of the repository system. import os import sys import argparse -import rpki.POW + +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) + 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("rcynic_dir", nargs = "?", type = check_dir, help = "rcynic authenticated output directory") parser.add_argument("output_dir", help = "name of output directory to create") args = parser.parse_args() if not os.path.isdir(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: +def store(uri, obj, fmt): + for i in xrange(1000000): + fn = os.path.join(args.output_dir, fmt.format(i)) + if os.path.exists(fn): continue - 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 - else: - sys.exit("No path name available for %s (%s)" % (ifn, ofn)) + 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 {} ({})".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())) |