1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import re |
---|
5 | import os |
---|
6 | |
---|
7 | from optparse import OptionParser |
---|
8 | |
---|
9 | # Options |
---|
10 | class 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 | |
---|
22 | class 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 |
---|
33 | parser = Parser() |
---|
34 | opts, args = parser.parse_args() |
---|
35 | |
---|
36 | if len(args) == 1: |
---|
37 | combo = args[0] |
---|
38 | else: |
---|
39 | parser.print_help() |
---|
40 | sys.exit('\nMust have one file argument') |
---|
41 | |
---|
42 | if 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 | |
---|
47 | try: |
---|
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 |
---|
75 | except EnvironmentError, e: |
---|
76 | sys.exit("%s: %s" % (e.strerror, e.filename or '?!')) |
---|
77 | |
---|
78 | # This is probably unnecessary. Close all the diversion files. |
---|
79 | for d in divs: d.f.close() |
---|