File: ALCConfig.py

package info (click to toggle)
apt-listchanges 3.24
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,352 kB
  • sloc: python: 1,710; xml: 650; makefile: 138; sh: 71; perl: 61
file content (284 lines) | stat: -rw-r--r-- 10,305 bytes parent folder | download
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
# vim:set fileencoding=utf-8 et ts=4 sts=4 sw=4:
#
#   apt-listchanges - Show changelog entries between the installed versions
#                     of a set of packages and the versions contained in
#                     corresponding .deb files
#
#   Copyright (C) 2000-2006  Matt Zimmerman  <mdz@debian.org>
#   Copyright (C) 2006       Pierre Habouzit <madcoder@debian.org>
#
#   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; either version 2 of the License, or
#   (at your option) any later version.
#
#   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 St, Fifth Floor, Boston, MA 02110-1301, USA.
#

import configparser
import getopt
import sys, os

import ALCLog
from ALChacks import _

class ALCConfig:
    def __init__(self):
        ### Command line only options
        self.apt_mode = False   # "--apt" option
        self.dump_seen = False
        self.profile = None

        ### Options that can be set in the config file
        self.frontend = 'pager'
        self.email_address = None
        self.email_format = 'text'
        self.verbose = False
        self.show_all = False
        self.confirm = False
        self.headers = False
        self.debug = False
        self.save_seen = None
        self.which = 'both'
        self.since = None
        self.latest = None
        self.reverse = False
        self.no_network = False
        self.select_frontend = False
        self.ignore_apt_assume = False
        self.ignore_debian_frontend = False
        self.titled = True
        self.log = '/var/log/apt/listchanges.log'
        self.filter = None

        ### Internal options for managing the config file options
        # - options that can be also used for command line parameters
        #   (after replacing '_' with '-', and adding trailing '=' if needed)
        self._bool_opts = [
            'confirm',
            'debug',
            'show_all',
            'headers',
            'verbose',
            'reverse',
            'dump_seen',
            'select_frontend',
            'no_network',
            'ignore_apt_assume',
            'ignore_debian_frontend',
            'titled',
        ]
        self._value_opts = [
            'frontend',
            'email_address',
            'email_format',
            'latest',
            'log',
            'filter',
            'save_seen',
            'since',
            'which',
        ]
        # - options that can be given only in the config file
        self._cfgfile_only_opts = [
            'browser',
            'pager',
            'xterm',
        ]

        ### Other options
        self.quiet = 0
        self.frontend_from_env = False
        self._allowed_email_formats = ('text', 'html')
        self._allowed_which = ('both', 'news', 'changelogs')

    def read(self, file):
        self.parser = configparser.ConfigParser()
        self.parser.read(file)

    def expose(self):
        if self.parser.has_section(self.profile):
            for option in self.parser.options(self.profile):
                value = None
                if self.parser.has_option(self.profile, option):
                    if option in self._bool_opts:
                        value = self.parser.getboolean(self.profile, option)
                    elif option in self._value_opts  or option in self._cfgfile_only_opts:
                        value = self.parser.get(self.profile, option)
                    else:
                        ALCLog.warning(_("Unknown configuration file option: %s") % option)
                        continue
                setattr(self, option, value)

    def get(self, option, defvalue=None):
        return getattr(self, option, defvalue)

    def usage(self, exitcode):
        if exitcode == 0:
            fh = sys.stdout
        else:
            fh = sys.stderr

        fh.write(_("Usage: apt-listchanges [options] {--apt | filename.deb ...}\n"))
        sys.exit(exitcode)

    def _check_allowed(self, arg, opt, allowed):
        if arg in allowed:
            return arg
        ALCLog.error((_('Unknown argument %(arg)s for option %(opt)s.  Allowed are: %(allowed)s.') %
                          {'arg': arg, 'opt': opt, 'allowed': ', '.join(allowed)}))
        sys.exit(1)

    def _check_debs(self, debs):
        if self.apt_mode or self.dump_seen:
            return
        if not debs:
            self.usage(1)
        for deb in debs:
            ext = os.path.splitext(deb)[1]
            if ext != ".deb":
                ALCLog.error(_("%(deb)s does not have '.deb' extension") % {'deb': deb})
                sys.exit(1)
            if not os.path.isfile(deb):
                ALCLog.error(_('%(deb)s does not exist or is not a file') % {'deb': deb})
                sys.exit(1)
            if not os.access(deb, os.R_OK):
                ALCLog.error(_('%(deb)s is not readable') % {'deb' : deb})
                sys.exit(1)

    def getopt(self, argv):
        try:
            (optlist, args) = getopt.getopt(argv[1:], 'vf:s:cah', [
                # command line only
                "apt", "profile=", "help",
                # deprecated options for backward compatibility
                "all", "save_seen=" ]
                # boolean options
                + [ x.replace('_', '-') for x in  self._bool_opts ]
                # with value options
                + [ x.replace('_', '-')+'=' for x in self._value_opts ]
            )
        except getopt.GetoptError as err:
            ALCLog.error(str(err))
            sys.exit(1)

        # Determine mode and profile before processing other options
        for opt, arg in optlist:
            if opt == '--profile':
                self.profile = arg
            elif opt == '--apt':
                self.apt_mode = True

        # Provide a default profile if none has been specified
        if self.profile is None:
            if self.apt_mode:
                self.profile = 'apt'
            else:
                self.profile = 'cmdline'

        # Expose defaults from config file
        self.expose()

        # Environment variables override config file
        if 'APT_LISTCHANGES_FRONTEND' in os.environ:
            self.frontend = os.getenv('APT_LISTCHANGES_FRONTEND')
            self.frontend_from_env = True
            # Prefer APT_LISTCHANGES_FRONTEND over DEBIAN_FRONTEND
            self.ignore_debian_frontend = True

        (since, latest, show_all) = (None, None, False)

        # Command-line options override environment and config file
        for opt, arg in optlist:
            if opt == '--help':
                self.usage(0)
            elif opt in ('-v', '--verbose'):
                self.verbose = True
            elif opt in ('-f', '--frontend'):
                self.frontend = arg
            elif opt == '--email-address':
                self.email_address = arg
            elif opt == '--email-format':
                self.email_format = self._check_allowed(arg, opt, self._allowed_email_formats)
            elif opt in ('-c', '--confirm'):
                self.confirm = True
            elif opt in ('--since'):
                since = arg
            elif opt in ('--latest'):
                latest = arg
            elif opt in ('-a', '--show-all', '--all'):
                show_all = True
            elif opt in ('-h', '--headers'):
                self.headers = True
            elif opt in ('--save-seen', '--save_seen'):
                self.save_seen = arg
            elif opt == '--dump-seen':
                self.dump_seen = True
            elif opt == '--which':
                self.which = self._check_allowed(arg, opt, self._allowed_which)
            elif opt == '--debug':
                self.debug = True
            elif opt == '--reverse':
                self.reverse = True
            elif opt in ('-n', '--no-network'):
                self.no_network = True
            elif opt == '--select-frontend':
                self.select_frontend = True
            elif opt == '--ignore-apt-assume':
                self.ignore_apt_assume = True
            elif opt == '--ignore-debian-frontend':
                self.ignore_debian_frontend = True
            elif opt == '--titled':
                self.titled = True
            elif opt == '--untitled':
                self.titled = False
            elif opt in ('--log'):
                self.log = arg
            elif opt in ('--filter'):
                self.filter = arg

        if self.email_address == 'none':
            self.email_address = None
        if self.save_seen == 'none':
            self.save_seen = None

        if ((self.since is not None and self.show_all)
           or (since is not None and show_all)):
                ALCLog.error(_('--since=<version> and --show-all are mutually exclusive'))
                sys.exit(1)
        elif since is not None or show_all:
            self.since = since
            self.show_all = show_all

        if self.since is not None:
            if len(args) != 1:
                ALCLog.error(_('--since=<version> expects a path to exactly one .deb archive'))
                sys.exit(1)
            self.save_seen = None

        if ((self.latest is not None and self.show_all) or
             (latest is not None and show_all)):
                ALCLog.error(_('--latest=<N> and --show-all are mutually exclusive'))
                sys.exit(1)
        elif latest is not None or show_all:
            self.latest = latest
            self.show_all = show_all

        if (self.apt_mode and not self.ignore_debian_frontend and
              os.getenv('DEBIAN_FRONTEND', '') == 'noninteractive'):
            # Force non-interactive usage
            self.quiet = 1
            self.confirm = False

        self._check_debs(args)

        return args

__all__ = [ 'ALCConfig' ]