summaryrefslogtreecommitdiffstats
path: root/base/common/python/pki/__init__.py
blob: 404aa92d422b7b8257792257eeacbde7a6bb3bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
# Authors:
#     Endi S. Dewata <edewata@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Red Hat, Inc.
# All rights reserved.
#

import os
import re


CONF_DIR          = '/etc/pki'
SHARE_DIR         = '/usr/share/pki'
BASE_DIR          = '/var/lib'
LOG_DIR           = '/var/log/pki'

PACKAGE_VERSION   = SHARE_DIR + '/VERSION'


def read_text(message,
    options=None, default=None, delimiter=':',
    allowEmpty=True, caseSensitive=True):

    if default:
        message = message + ' [' + default + ']'
    message = message + delimiter + ' '

    done = False
    while not done:
        value = raw_input(message)
        value = value.strip()

        if len(value) == 0:  # empty value
            if allowEmpty:
                value = default
                done = True
                break

        else:  # non-empty value
            if options is not None:
                for v in options:
                    if caseSensitive:
                        if v == value:
                            done = True
                            break
                    else:
                        if v.lower() == value.lower():
                            done = True
                            break
            else:
                done = True
                break

    return value


def implementation_version():

    with open(PACKAGE_VERSION, 'r') as f:
        for line in f:
            line = line.strip('\n')

            # parse <key>: <value>
            match = re.match('^\s*(\S*)\s*:\s*(.*)\s*$', line)

            if not match:
                continue

            key = match.group(1)
            value = match.group(2)

            if key.lower() != 'implementation-version':
                continue

            return value

    raise Exception('Missing implementation version.')


class PKIException(Exception):

    def __init__(self, message, exception=None):

        Exception.__init__(self, message)

        self.exception = exception


class PropertyFile(object):

    def __init__(self, filename, delimiter='='):

        self.filename = filename
        self.delimiter = delimiter

        self.lines = []

    def read(self):

        self.lines = []

        if not os.path.exists(self.filename):
            return

        # read all lines and preserve the original order
        with open(self.filename, 'r') as f:
            for line in f:
                line = line.strip('\n')
                self.lines.append(line)

    def write(self):

        # write all lines in the original order
        with open(self.filename, 'w') as f:
            for line in self.lines:
                f.write(line + '\n')

    def show(self):

        for line in self.lines:
            print line

    def insert_line(self, index, line):

        self.lines.insert(index, line)

    def remove_line(self, index):

        self.lines.pop(index)

    def index(self, name):

        for i, line in enumerate(self.lines):

            # parse <key> <delimiter> <value>
            match = re.match('^\s*(\S*)\s*%s\s*(.*)\s*$' % self.delimiter, line)

            if not match:
                continue

            key = match.group(1)

            if key.lower() == name.lower():
                return i

        return -1

    def get(self, name):

        result = None

        for line in self.lines:

            # parse <key> <delimiter> <value>
            match = re.match('^\s*(\S*)\s*%s\s*(.*)\s*$' % self.delimiter, line)

            if not match:
                continue

            key = match.group(1)
            value = match.group(2)

            if key.lower() == name.lower():
                return value

        return result

    def set(self, name, value, index=None):

        for i, line in enumerate(self.lines):

            # parse <key> <delimiter> <value>
            match = re.match('^\s*(\S*)\s*%s\s*(.*)\s*$' % self.delimiter, line)

            if not match:
                continue

            key = match.group(1)

            if key.lower() == name.lower():
                self.lines[i] = key + self.delimiter + value
                return

        if index is None:
            self.lines.append(name + self.delimiter + value)

        else:
            self.insert_line(index, name + self.delimiter + value)

    def remove(self, name):

        for i, line in enumerate(self.lines):

            # parse <key> <delimiter> <value>
            match = re.match('^\s*(\S*)\s*%s\s*(.*)\s*$' % self.delimiter, line)

            if not match:
                continue

            key = match.group(1)
            value = match.group(2)

            if key.lower() == name.lower():
                self.lines.pop(i)
                return value

        return None