This file is indexed.

/usr/lib/python2.7/dist-packages/svnmailer/config.py is in svnmailer 1.0.9-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
# pylint: disable-msg = W0201
#
# Copyright 2004-2006 André Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Configfile parsing
"""
__author__    = "André Malo"
__docformat__ = "epytext en"
__all__       = [
    'ConfigFileSettings',
    'Error',
    'ConfigNotFoundError',
    'ConfigMissingError',
    'ConfigInvalidError',
    'ConfigMappingSectionNotFoundError',
    'ConfigMappingSpecInvalidError',
    'ConfigSectionNotFoundError',
    'ConfigOptionUnknownError',
]

# global imports
import ConfigParser, sys, os
from svnmailer import settings, util


# Exceptions
class Error(Exception):
    """ Base exception for this module """
    pass

class ConfigNotFoundError(Error):
    """ Config file not found """
    pass

class ConfigMissingError(ConfigNotFoundError):
    """ Config not specified and not found on default locations """
    pass

class ConfigInvalidError(Error):
    """ Config file has errors """
    pass

class ConfigMappingSectionNotFoundError(ConfigInvalidError):
    """ Config mapping section was not found """
    pass

class ConfigMappingSpecInvalidError(ConfigInvalidError):
    """ Config mapping spec was not recognized """
    pass

class ConfigSectionNotFoundError(ConfigInvalidError):
    """ Specified config section was not found """
    pass

class ConfigOptionUnknownError(ConfigInvalidError):
    """ An unknown option was parsed """
    pass


class ConfigFileSettings(settings.Settings):
    """ Provide settings from config

        @cvar MAPSECTION: The mapping section name; if C{None},
            mapping is effectively disabled
        @type MAPSECTION: C{str}

        @ivar _config: The config object
        @type _config: C{ConfigParser.ConfigParser}
    """
    __implements__ = [settings.Settings]

    MAPSECTION = "maps"

    def init(self, *args, **kwargs):
        """ Implements the C{init} method of L{settings.Settings}

            @exception ConfigInvalidError: invalid config options
            @exception ConfigMissingError: see L{_loadConfig}
            @exception ConfigNotFoundError: see L{_loadConfig}
            @exception ConfigSectionNotFoundError: see L{_passConfig}
            @exception ConfigOptionUnkownError: see L{_passConfig}
            @exception ConfigMappingSpecInvalidError: see L{_applyMaps}
            @exception ConfigMappingSectionNotFoundError: see L{_getPlainMap}
        """
        try:
            self._init(*args, **kwargs)
        except (ValueError, TypeError, UnicodeError, ConfigParser.Error), exc:
            raise ConfigInvalidError, str(exc), sys.exc_info()[2]


    def _init(self, options):
        """ Actual implementation of C{self.init()}

            @param options: runtime options
            @type options: C{optparse.OptionParser}

            @exception ConfigMissingError: see L{_loadConfig}
            @exception ConfigNotFoundError: see L{_loadConfig}
            @exception ConfigSectionNotFoundError: see L{_passConfig}
            @exception ConfigOptionUnkownError: see L{_passConfig}
            @exception ConfigMappingSpecInvalidError: see L{_applyMaps}
            @exception ConfigMappingSectionNotFoundError: see L{_getPlainMap}
        """
        self._initRuntime(options)
        self._loadConfig()  # needs runtime
        self._initGeneral() # needs _config
        self._initGroups()  # needs _config and general


    def _initGroups(self):
        """ Initializes the Group config """
        defaults = self._getGroupDefaults()
        ddict = self._getDefaultGroupDict(defaults)

        for group in self._config.sections():
            ddict["_name"] = group
            container = self.getGroupContainer(**ddict)
            self._passConfig(container, group)
            self.groups.append(container)

        if not self.groups:
            self.groups.append(self.getGroupContainer(**defaults._dict_))


    def _getDefaultGroupDict(self, container):
        """ Returns the default group dict

            @param container: The default container
            @type container: C{svnmailer.settings.GroupSettingsContainer}

            @return: The default dict
            @rtype: C{dict}
        """
        ddict = dict(container._dict_)
        ddict.update({
            "_def_for_repos": container.for_repos,
            "_def_for_paths": container.for_paths,
        })

        return ddict


    def _getGroupDefaults(self):
        """ Returns the default groups container

            @return: The defaults (groupcontainer without maps)
            @rtype: C{svnmailer.settings.GroupSettingsContainer}
        """
        defaults = self.getDefaultGroupContainer(
            _name = "defaults",
            diff_command = self.general.diff_command,
            cia_rpc_server = self.general.cia_rpc_server,
        )
        try:
            self._passConfig(defaults, "defaults")
        except ConfigSectionNotFoundError:
            # [defaults] is optional
            pass
        else:
            self._config.remove_section('defaults')

        return defaults


    def _initGeneral(self):
        """ Initializes the general config

            @exception ConfigSectionNotFoundError: [general] not found
        """
        self.general = self.getGeneralContainer()
        self._passConfig(self.general, 'general')
        self._config.remove_section('general')


    def _initRuntime(self, options):
        """ Initializes the runtime from options

            @param options: runtime options
            @type options: C{optparse.OptionParser}
        """
        # This is needed for every container
        self._fcharset_ = options.path_encoding

        self.runtime = self.getRuntimeContainer(
            revision      = options.revision,
            repository    = options.repository,
            path_encoding = options.path_encoding,
            debug         = options.debug,
            config        = options.config,
            mode          = options.mode,
            author        = options.author,
            propname      = options.propname,
            action        = options.action,
        )


    def _passConfig(self, container, section):
        """ Passes the options to the specified container

            @param container: The container object
            @type container: C{svnmailer.util.Struct}

            @param section: The config section name
            @type section: C{str}

            @exception ConfigSectionNotFoundError: The specified section was
                not found in the config file
            @exception ConfigOptionUnkownError: There was an unknown
                config option in the config file.
        """
        try:
            for option in self._config.options(section):
                # options starting with _ are for internal usage
                if option[:1] in ('_', '-'):
                    raise ConfigOptionUnknownError(
                        "Unknown option '%s' in section [%s]" %
                        (option, section)
                    )

                try:
                    container._set_(
                        option.replace('-', '_'),
                        self._config.get(section, option, raw = True)
                    )
                except AttributeError:
                    raise ConfigOptionUnknownError(
                        "Unknown option '%s' in section [%s]" %
                        (option, section)
                    )
        except ConfigParser.NoSectionError, exc:
            raise ConfigSectionNotFoundError(str(exc))


    def _loadConfig(self):
        """ Parse config file

            @return: parsed config
            @rtype: C{ConfigParser.ConfigParser}

            @exception ConfigNotFoundError: some configfile could not
                be opened
            @exception ConfigMissingError: see L{_findConfig}
            @exception ConfigMappingSpecInvalidError: see L{_applyMaps}
            @exception ConfigMappingSectionNotFoundError: see L{_getPlainMap}
        """
        config_fp = self._findConfig()
        self._config = self._createConfigParser()
        try:
            self._config.readfp(config_fp, config_fp.name)
            config_fp.close()
        except IOError, exc:
            raise ConfigNotFoundError("%s: %s" % (config_fp.name, str(exc)))

        if self._config.has_section("general"):
            self._applyCharset()
            self._applyIncludes(config_fp.name)

        self._applyMaps()


    def _createConfigParser(self):
        """ Returns a ConfigParser instance

            @return: The ConfigParser instance
            @rtype: C{ConfigParser.ConfigParser}
        """
        return ConfigParser.ConfigParser()


    def _findConfig(self, _file = file):
        """ Finds and opens the main config file

            @param _file: The function to open the file
            @type _file: C{callable}

            @return: The open descriptor
            @rtype: file like object

            @exception ConfigMissingError: config neither specified nor
                on default locations found. Default locations are (tried
                in that order):
                     - <repos>/conf/mailer.conf
                     - <scriptdir>/mailer.conf
                     - /etc/svn-mailer.conf
            @exception ConfigNotFoundError: specified configfile could not
                be opened
        """
        import errno

        config_file = self.runtime.config
        if config_file:
            try:
                return config_file == '-' and sys.stdin or _file(config_file)
            except IOError, exc:
                raise ConfigNotFoundError("%s: %s" % (config_file, str(exc)))

        for config_file in self._getDefaultConfigFiles():
            try:
                return _file(config_file)
            except IOError, exc:
                # try next one only if not found
                if exc[0] != errno.ENOENT:
                    raise ConfigNotFoundError("%s: %s" % (
                        config_file, str(exc)
                    ))

        raise ConfigMissingError("No config file found")


    def _applyMaps(self):
        """ Resolves all map definitions

            @TODO: raise an error on unknown options

            @exception ConfigMappingSpecInvalidError: The mapping spec was
                invalid
            @exception ConfigMappingSectionNotFoundError: see L{_getPlainMap}
        """
        section = self.MAPSECTION
        if section is None or not self._config.has_section(section):
            return

        self._maps_ = {}
        remove_sections = [section]
        for option in self._config.options(section):
            if option[:1] in ('_', '-'):
                raise ConfigOptionUnknownError(
                    "Unknown option '%s' in section [%s]" %
                    (option, section)
                )

            value = self._config.get(section, option, raw = True)
            if value[:1] == '[' and value[-1:] == ']':
                this_section = value[1:-1]
                self._maps_[option.replace('-', '_')] = \
                    self._getPlainMap(this_section)
                remove_sections.append(this_section)
            else:
                raise ConfigMappingSpecInvalidError(
                    "Invalid mapping specification %r = %r" % (option, value)
                )

        for name in dict.fromkeys(remove_sections).keys():
            self._config.remove_section(name)


    def _getPlainMap(self, section):
        """ Returns a plain map for a particular section

            @param section: The mapping section
            @type section: C{str}

            @return: The mapping function
            @rtype: C{callable}

            @exception ConfigMappingSectionNotFoundError: The specified
                section was not found
        """
        try:
            mdict = dict([
                (option, self._config.get(section, option, raw = True))
                for option in self._config.options(section)
            ])
        except ConfigParser.NoSectionError, exc:
            raise ConfigMappingSectionNotFoundError(str(exc))

        def mapfunc(value):
            """ Mapping function """
            return mdict.get(value, value)

        return mapfunc


    def _applyIncludes(self, origfile, _file = file):
        """ Applies the includes found in [general]

            @param origfile: original filename
            @type origfile: C{str}

            @param _file: The function to open the file
            @type _file: C{callable}

            @exception ConfigNotFoundError: Error reading an included file
        """
        opt = "include_config"
        try:
            try:
                includes = self._config.get("general", opt, raw = True).strip()
            except ConfigParser.NoOptionError:
                opt = "include-config"
                includes = self._config.get("general", opt, raw = True).strip()
        except ConfigParser.NoOptionError:
            # don't even ignore
            pass
        else:
            self._config.remove_option("general", opt)
            if not len(includes):
                return

            origpath = os.path.dirname(os.path.abspath(origfile))
            includes = [
                util.filename.toLocale(
                    config_file, self._charset_, self.runtime.path_encoding
                )
                for config_file in util.splitCommand(includes) if config_file
            ]

            for config_file in includes:
                try:
                    config_fp = _file(os.path.join(origpath, config_file))
                    self._config.readfp(config_fp, config_fp.name)
                    config_fp.close()
                except IOError, exc:
                    raise ConfigNotFoundError("%s: %s" % (
                        config_file, str(exc)
                    ))
 

    def _applyCharset(self):
        """ Applies the charset found in [general] """
        opt = "config_charset"
        try:
            try:
                charset = self._config.get("general", opt, raw = True).strip()
            except ConfigParser.NoOptionError:
                opt = "config-charset"
                charset = self._config.get("general", opt, raw = True).strip()
        except ConfigParser.NoOptionError:
            # don't even ignore
            pass
        else:
            self._config.remove_option("general", opt)
            if charset:
                self._charset_ = charset


    def _getDefaultConfigFiles(self, _os = os, _sys = sys):
        """ Returns the default config files

            @return: The list
            @rtype: C{list}
        """
        argv0 = util.filename.fromLocale(
            _sys.argv[0], self.runtime.path_encoding
        )
        if isinstance(argv0, unicode):
            candidates = [util.filename.toLocale(
                    name, locale_enc = self.runtime.path_encoding
                ) for name in [
                    _os.path.join(
                        self.runtime.repository, u'conf', u'mailer.conf'
                    ),
                    _os.path.join(_os.path.dirname(argv0), u'mailer.conf'),
                    u'/etc/svn-mailer.conf',
                ]
            ]
        else:
            # --path-encoding=none
            candidates = [
                _os.path.join(self.runtime.repository, 'conf', 'mailer.conf'),
                _os.path.join(_os.path.dirname(argv0), 'mailer.conf'),
                _os.path.join(_os.path.sep, "etc", "svn-mailer.conf"),
            ]

        return candidates