source: fedd/creddy_split.py @ 835cf55

axis_examplecompt_changesinfo-ops
Last change on this file since 835cf55 was 87c0fc1, checked in by Ted Faber <faber@…>, 14 years ago

A couple more abac scripts

  • Property mode set to 100755
File size: 2.2 KB
Line 
1#!/usr/local/bin/python
2
3import sys
4import re
5import os
6
7from optparse import OptionParser
8
9# Options
10class Parser(OptionParser):
11    def __init__(self):
12        OptionParser.__init__(self, usage="%prog [options] file.pem")
13        self.add_option('--cert', dest='cert', default='./cert.pem', 
14                help='File to extract certificate into, default: [%default]')
15        self.add_option('--key', dest='key', default='./key.pem', 
16                help='File to extract key into, default: [%default]')
17        self.add_option('--force', action='store_true', dest='force',
18                default=False,
19                help=('Overwite existing certificate and key files. ' + \
20                        'default: [%default]'))
21
22class diversion:
23    '''
24    Wraps up the reqular expression to start and end a diversion, as well as
25    the open file that gets the lines.
26    '''
27    def __init__(self, start, end, fn):
28        self.start = re.compile(start)
29        self.end = re.compile(end)
30        self.f = open(fn, 'w')
31
32# Option validation
33parser = Parser()
34opts, args = parser.parse_args()
35
36if len(args) == 1:
37    combo = args[0]
38else:
39    parser.print_help()
40    sys.exit('\nMust have one file argument')
41
42if not opts.force:
43    for fn in (opts.cert, opts.key):
44        if os.access(fn, os.F_OK):
45            sys.exit('%s exists.  --force to overwite it' % fn)
46
47try:
48    # Initialize the diversions
49    divs = [diversion(s, e, fn) for s, e,fn in (
50        ('\s*-----BEGIN RSA PRIVATE KEY-----$', 
51            '\s*-----END RSA PRIVATE KEY-----$',
52            opts.key),
53        ('\s*-----BEGIN CERTIFICATE-----$', 
54            '\s*-----END CERTIFICATE-----$',
55            opts.cert))]
56
57    # walk through the file, beginning a diversion when a start regexp matches
58    # until the end regexp matches.  While in the two regexps, print each line
59    # to the ipen diversion file (including the two matches).
60    active = None
61    f = open(combo, 'r')
62    for l in f:
63        if active:
64            if active.end.match(l):
65                print >>active.f, l,
66                active = None
67        else:
68            for d in divs:
69                if d.start.match(l):
70                    active = d
71                    break
72        if active: print >>active.f, l,
73
74# This clause catches all file opening problems, including the diversion opens
75except EnvironmentError, e:
76    sys.exit("%s: %s" % (e.strerror, e.filename or '?!'))
77
78# This is probably unnecessary.  Close all the diversion files.
79for d in divs: d.f.close()
Note: See TracBrowser for help on using the repository browser.