File: support.py

package info (click to toggle)
dnf-plugins-core 4.10.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,752 kB
  • sloc: python: 7,441; sh: 23; makefile: 15; xml: 7
file content (294 lines) | stat: -rw-r--r-- 9,229 bytes parent folder | download | duplicates (3)
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
# Copyright (C) 2014-2018  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#

from __future__ import absolute_import
from __future__ import unicode_literals

import dnf
import dnf.cli.option_parser
import logging
import sys
import unittest

PY3 = False
if sys.version_info.major >= 3:
    PY3 = True

if PY3:
    from unittest import mock
else:
    from . import mock


def command_configure(cmd, args):
    parser = dnf.cli.option_parser.OptionParser()
    args = [cmd._basecmd] + args
    parser.parse_main_args(args)
    parser.parse_command_args(cmd, args)
    return cmd.configure()


def command_run(cmd, args):
    command_configure(cmd, args)
    return cmd.run()


class BaseStub(object):
    def __init__(self):
        self.sack = dnf.sack.Sack()
        self.repos = dnf.repodict.RepoDict()
        self.conf = FakeConf()
        self.output = dnf.cli.output.Output(self, self.conf)
        self.output.progress = dnf.callback.NullDownloadProgress()

    def add_remote_rpms(self, path_list, progress=None):
        self.sack.create_cmdline_repo()
        pkgs = []
        for path in path_list:
            pkgs.append(self.sack.add_cmdline_package(path))
        return pkgs

    def _add_repo_to_sack(self, repo):
        raise dnf.exceptions.RepoError()

    def reset(self, sack=True, repos=True):
        self.sack = dnf.sack.Sack()
        self.repos = dnf.repodict.RepoDict()
        self.conf = FakeConf()

    def fill_sack(self, load_system_repo=False, load_available_repos=False):
        return


class BaseCliStub(object):
    """A class mocking `dnf.cli.cli.BaseCli`."""

    def __init__(self, available_pkgs=(), available_groups=()):
        """Initialize the base."""
        self._available_pkgs = set(available_pkgs)
        self._available_groups = set(available_groups)
        self.installed_groups = set()
        self.installed_pkgs = set()
        self.repos = dnf.repodict.RepoDict()
        self.conf = dnf.conf.BaseConfig()

    def read_all_repos(self):
        """Read repositories information."""
        self.repos.add(dnf.repo.Repo(name='main'))

    def read_comps(self):
        """Read groups information."""
        if not self._available_groups:
            raise dnf.exceptions.CompsError('no group available')


class CliStub(object):
    """A class mocking `dnf.cli.Cli`."""

    def __init__(self, base):
        """Initialize the CLI."""
        self.base = base
        self.cli_commands = {}
        self.demands = DemandsStub()
        self.logger = logging.getLogger()
        self.register_command(dnf.cli.commands.HelpCommand)

    def register_command(self, command):
        """Register given *command*."""
        self.cli_commands.update({alias: command for alias in command.aliases})

    def redirect_logger(self, stdout=None, stderr=None):
        return

    def redirect_repo_progress(self, fo=sys.stderr):
        return


class DemandsStub(object):
    pass


class FakeConf(dnf.conf.Conf):
    def __init__(self, **kwargs):
        super(FakeConf, self).__init__()
        self.substitutions['releasever'] = 'Fedora69'
        for optname, val in [
                ('assumeyes', None),
                ('best', False),
                ('cachedir', dnf.const.TMPDIR),
                ('clean_requirements_on_remove', False),
                ('color', 'never'),
                ('color_update_installed', 'normal'),
                ('color_update_remote', 'normal'),
                ('color_list_available_downgrade', 'dim'),
                ('color_list_available_install', 'normal'),
                ('color_list_available_reinstall', 'bold'),
                ('color_list_available_upgrade', 'bold'),
                ('color_list_installed_extra', 'bold'),
                ('color_list_installed_newer', 'bold'),
                ('color_list_installed_older', 'bold'),
                ('color_list_installed_reinstall', 'normal'),
                ('color_update_local', 'bold'),
                ('debug_solver', False),
                ('debuglevel', 2),
                ('defaultyes', False),
                ('disable_excludes', []),
                ('diskspacecheck', True),
                ('exclude', []),
                ('includepkgs', []),
                ('install_weak_deps', True),
                ('history_record', False),
                ('installonly_limit', 0),
                ('installonlypkgs', ['kernel']),
                ('installroot', '/'),
                ('ip_resolve', None),
                ('multilib_policy', 'best'),
                ('obsoletes', True),
                ('persistdir', '/should-not-exist-bad-test/persist'),
                ('protected_packages', ["dnf"]),
                ('plugins', False),
                ('showdupesfromrepos', False),
                ('tsflags', []),
                ('strict', True),
                ] + list(kwargs.items()):
            self._set_value(optname, val, dnf.conf.PRIO_DEFAULT)

    @property
    def releasever(self):
        return self.substitutions['releasever']


class RepoStub(object):
    """A class mocking `dnf.repo.Repo`"""

    enabled = True

    def __init__(self, id_):
        """Initialize the repository."""
        self.id = id_
        self.priority = 99
        self.cost = 1000

    def _valid(self):
        """Return a message if the repository is not valid."""

    def enable(self):
        """Enable the repo"""
        self.enabled = True

    def disable(self):
        """Disable the repo"""
        self.enabled = False

    def _md_expire_cache(self):
        """Mark cache expired"""


class PkgStub:
    def __init__(self, n, e, v, r, a, repo_id, src_name="", location="", repo=None,
                 obsoletes=()):
        """Mocking dnf.package.Package."""
        self.name = n
        self.version = v
        self.release = r
        self.arch = a
        self.epoch = e
        self.repoid = repo_id
        self.reponame = repo_id
        self.src_name = src_name
        self.repo = repo
        self.location = location
        self.obsoletes = obsoletes

    def __str__(self):
        return '%s : %s' % (self.fullname, self.reponame)

    def __lt__(self, other):
        return self.fullname < other.fullname

    @property
    def evr(self):
        if self.epoch != '0':
            return '%s:%s-%s' % (self.epoch, self.version, self.release)
        else:
            return '%s-%s' % (self.version, self.release)

    @property
    def source_debug_name(self):
        """
        returns name of debuginfo package for source package of given package
        e.g. krb5-libs -> krb5-debuginfo
        """
        return "{}-debuginfo".format(self.source_name)

    @property
    def source_name(self):
        """"
        returns name of source package
        e.g. krb5-libs -> krb5
        """
        if self.sourcerpm is not None:
            # trim suffix first
            srcname = dnf.util.rtrim(self.sourcerpm, ".src.rpm")
            # source package filenames may not contain epoch, handle both cases
            srcname = dnf.util.rtrim(srcname, "-{}".format(self.evr))
            srcname = dnf.util.rtrim(srcname, "-{0.version}-{0.release}".format(self))
        else:
            srcname = None
        return srcname

    @property
    def sourcerpm(self):
        name = self.src_name or self.name

        # special cases for debuginfo tests
        if name == "kernel-PAE":
            name = "kernel"
        elif name == "krb5-libs":
            name = "krb5"

        if self.arch != 'src':
            return '%s-%s.src.rpm' % (name, self.evr)
        else:
            return '%s-%s.%s.rpm' % (name, self.evr, self.arch)

    @property
    def fullname(self):
        return '%s-%s.%s' % (self.name, self.evr, self.arch)

    def localPkg(self):
        return '/tmp/dnf/%s-%s.%s.rpm' % (self.name, self.evr, self.arch)

    @property
    def from_cmdline(self):
        return True

    @property
    def debug_name(self):
        """
        returns name of debuginfo package for given package
        e.g. kernel-PAE -> kernel-PAE-debuginfo
        """
        return "{}-debuginfo".format(self.name)


class TestCase(unittest.TestCase):
    def assertEmpty(self, collection):
        return self.assertEqual(len(collection), 0)

    if not PY3:
        assertCountEqual = unittest.TestCase.assertItemsEqual