Changeset 353db8c for fedd/creddy_split.py
- Timestamp:
- Nov 23, 2010 5:00:48 PM (13 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master
- Children:
- 6e63513
- Parents:
- 3ff5e2a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/creddy_split.py
r3ff5e2a r353db8c 6 6 7 7 from optparse import OptionParser 8 from federation.util import abac_split_cert, abac_pem_type 8 9 9 10 # Options … … 20 21 'default: [%default]')) 21 22 22 class diversion:23 '''24 Wraps up the reqular expression to start and end a diversion, as well as25 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 23 # Option validation 33 24 parser = Parser() … … 40 31 sys.exit('\nMust have one file argument') 41 32 42 if not opts.force:43 for fn in (opts.cert, opts.key):44 if o s.access(fn, os.F_OK):45 33 for fn in (opts.cert, opts.key): 34 if os.access(fn, os.F_OK): 35 if opts.force: os.unlink(fn) 36 else: sys.exit('%s exists. --force to overwite it' % fn) 46 37 47 38 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 39 type = abac_pem_type(combo) 40 if type == 'both': 41 abac_split_cert(combo, opts.key, opts.cert) 42 else: 43 sys.exit('Cannot split %s as it is a %s' % (combo, type or 'dunno')); 75 44 except EnvironmentError, e: 76 45 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()
Note: See TracChangeset
for help on using the changeset viewer.