Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """This file contains helpers for representing, manipulating, and writing | |
| 7 OpenSSL configuration files [1] | |
| 8 | |
| 9 Configuration files are simply a collection of name=value "properties", which | |
| 10 are grouped into "sections". | |
| 11 | |
| 12 [1] https://www.openssl.org/docs/manmaster/apps/config.html | |
| 13 """ | |
| 14 | |
| 15 class Property(object): | |
| 16 """Represents a key/value pair in OpenSSL .cnf files. | |
| 17 | |
| 18 Names and values are not quoted in any way, so callers need to pass the text | |
| 19 exactly as it should be written to the file (leading and trailing whitespace | |
| 20 doesn't matter). | |
| 21 | |
| 22 For instance: | |
| 23 baseConstraints = critical, CA:false | |
| 24 | |
| 25 Could be represented by a Property where: | |
| 26 name = 'baseConstraints' | |
| 27 value = 'critical, CA:false' | |
| 28 """ | |
| 29 def __init__(self, name, value): | |
| 30 self.name = name | |
| 31 self.value = value | |
| 32 | |
| 33 | |
| 34 def WriteTo(self, out): | |
| 35 """Outputs this property to .cnf file.""" | |
| 36 out.write('%s = %s\n' % (self.name, self.value)) | |
| 37 | |
| 38 | |
| 39 class Section(object): | |
| 40 """Represents a section in OpenSSL. For instance: | |
| 41 [CA_root] | |
| 42 preserve = true | |
| 43 | |
| 44 Could be represented by a Section where: | |
| 45 name = 'CA_root' | |
| 46 properties = [Property('preserve', 'true')] | |
| 47 """ | |
| 48 def __init__(self, name): | |
| 49 self.name = name | |
| 50 self.properties = [] | |
| 51 | |
| 52 | |
| 53 def EnsurePropertyNameNotDuplicated(self, name): | |
| 54 """Raises an exception of there is more than 1 property named |name|.""" | |
| 55 count = 0 | |
| 56 for prop in self.properties: | |
| 57 if prop.name == name: | |
| 58 count += 1 | |
|
mattm
2015/11/02 19:40:39
If you want to get into python tricks:
count = sum
eroman
2015/11/02 20:51:43
Thanks, I decided not to use that (since I find it
| |
| 59 if count > 1: | |
| 60 raise Exception('Duplicate property: %s' % (name)) | |
| 61 | |
| 62 | |
| 63 def SetProperty(self, name, value): | |
| 64 """Replaces, adds, or removes a Property from the Section: | |
| 65 | |
| 66 - If |value| is None, then this is equivalent to calling | |
| 67 RemoveProperty(name) | |
| 68 - If there is an existing property matching |name| then its value is | |
| 69 replaced with |value| | |
| 70 - If there are no properties matching |name| then a new one is added at | |
| 71 the end of the section | |
| 72 | |
| 73 It is expected that there is AT MOST 1 property with the given name. If | |
| 74 that is not the case then this function will raise an error.""" | |
| 75 | |
| 76 if value is None: | |
| 77 self.RemoveProperty(name) | |
| 78 return | |
| 79 | |
| 80 self.EnsurePropertyNameNotDuplicated(name) | |
| 81 | |
| 82 for prop in self.properties: | |
| 83 if prop.name == name: | |
| 84 prop.value = value | |
| 85 return | |
| 86 | |
| 87 self.AddProperty(name, value) | |
| 88 | |
| 89 | |
| 90 def AddProperty(self, name, value): | |
| 91 """Adds a property (allows duplicates)""" | |
| 92 self.properties.append(Property(name, value)) | |
| 93 | |
| 94 | |
| 95 def RemoveProperty(self, name): | |
| 96 """Removes the property with the indicated name, if it exists. | |
| 97 | |
| 98 It is expected that there is AT MOST 1 property with the given name. If | |
| 99 that is not the case then this function will raise an error.""" | |
| 100 self.EnsurePropertyNameNotDuplicated(name) | |
| 101 | |
| 102 for i in range(len(self.properties)): | |
| 103 if self.properties[i].name == name: | |
| 104 self.properties.pop(i) | |
| 105 return | |
| 106 | |
| 107 | |
| 108 def WriteTo(self, out): | |
| 109 """Outputs the section in the format used by .cnf files""" | |
| 110 out.write('[%s]\n' % (self.name)) | |
| 111 for prop in self.properties: | |
| 112 prop.WriteTo(out) | |
| 113 out.write('\n') | |
| 114 | |
| 115 | |
| 116 class Config(object): | |
| 117 """Represents a .cnf (configuration) file in OpenSSL""" | |
| 118 def __init__(self): | |
| 119 self.sections = [] | |
| 120 | |
| 121 | |
| 122 def GetSection(self, name): | |
| 123 """Gets or creates a section with the given name.""" | |
| 124 for section in self.sections: | |
| 125 if section.name == name: | |
| 126 return section | |
| 127 new_section = Section(name) | |
| 128 self.sections.append(new_section) | |
| 129 return new_section | |
| 130 | |
| 131 | |
| 132 def WriteToFile(self, path): | |
| 133 """Outputs the Config to a .cnf files.""" | |
| 134 with open(path, 'w') as out: | |
| 135 for section in self.sections: | |
| 136 section.WriteTo(out) | |
| OLD | NEW |