#!/usr/bin/env python import sys import re import os from federation.util import abac_split_cert, abac_pem_type, file_expanding_opts # Options class Parser(file_expanding_opts): def __init__(self): file_expanding_opts.__init__(self, usage="%prog [options] file.pem") self.add_option('--cert', dest='cert', default='./cert.pem', action='callback', callback=self.expand_file, type='str', help='File to extract certificate into, default: [%default]') self.add_option('--key', dest='key', default='./key.pem', action='callback', callback=self.expand_file, type='str', help='File to extract key into, default: [%default]') self.add_option('--force', action='store_true', dest='force', default=False, help=('Overwite existing certificate and key files. ' + \ 'default: [%default]')) # Option validation parser = Parser() opts, args = parser.parse_args() if len(args) == 1: combo = args[0] else: parser.print_help() sys.exit('\nMust have one file argument') for fn in (opts.cert, opts.key): if os.access(fn, os.F_OK): if opts.force: os.unlink(fn) else: sys.exit('%s exists. --force to overwite it' % fn) try: type = abac_pem_type(combo) if type == 'both': abac_split_cert(combo, opts.key, opts.cert) else: sys.exit('Cannot split %s as it is a %s' % (combo, type or 'dunno')); except EnvironmentError, e: sys.exit("%s: %s" % (e.strerror, e.filename or '?!'))