[go: up one dir, main page]

File: rpm.py

package info (click to toggle)
kiwi 9.24.56-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,844 kB
  • sloc: python: 58,147; sh: 3,261; xml: 2,919; makefile: 304; ansic: 132
file content (132 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2019 SUSE Linux GmbH.  All rights reserved.
#
# This file is part of kiwi.
#
# kiwi 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 3 of the License, or
# (at your option) any later version.
#
# kiwi 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 kiwi.  If not, see <http://www.gnu.org/licenses/>
#
import os
import re

# project
from kiwi.command import Command
from kiwi.defaults import Defaults
from kiwi.path import Path


class Rpm:
    """
    **Helper methods to handle the rpm database configuration**
    """
    def __init__(self, root_dir=None, macro_file=None):
        self.root_dir = root_dir
        self.config = []
        self.custom_config = []
        self.macro_path = os.sep.join(
            [
                self.root_dir or '', Defaults.get_custom_rpm_macros_path()
            ]
        )
        self.macro_file = os.sep.join(
            [
                self.macro_path,
                macro_file if macro_file else
                Defaults.get_custom_rpm_bootstrap_macro_name()
            ]
        )

    def expand_query(self, key):
        """
        Query database configuration and expand it
        """
        if self.root_dir:
            rpmdb_call = Command.run(
                ['chroot', self.root_dir, 'rpm', '-E', key]
            )
        else:
            rpmdb_call = Command.run(
                ['rpm', '-E', key]
            )
        return rpmdb_call.output.strip()

    def get_query(self, key):
        """
        Query database configuration

        Extract the first match for the given key. The method
        expects the format of entries to match the regular
        expression: '.*key[\t ]+value'

        :param string key: query key name

        :returns: match or None if there isn't any match

        :rtype: string | None
        """
        if not self.config:
            self.config = self._read_macro_configuration()
        item_list = list(
            filter(lambda item: key in item, self.config)
        )
        for item in item_list:
            match = re.match('.*{0}[\t ]+(.*)'.format(key), item)
            if match:
                return match.group(1)

    def set_config_value(self, key, value):
        """
        Set custom database configuration key/value pair

        :param string key: key name
        :param string value: key value name
        """
        self.custom_config.append('%{0}\t{1}'.format(key, value))

    def write_config(self):
        """
        Write custom database configuration

        Write bootstrap macro file to the custom rpm macros path
        """
        if self.custom_config:
            Path.create(self.macro_path)
            with open(self.macro_file, 'w') as macro:
                for entry in self.custom_config:
                    macro.write('{0}{1}'.format(entry, os.linesep))

    def wipe_config(self):
        """
        Delete custom database configuration
        """
        Path.wipe(self.macro_file)

    def _read_macro_configuration(self):
        """
        Read rpm database macro and configuration setup

        Please note we intentionally don't use 'rpm -E' here because
        it tries to expand the complete macro and if that is not
        possible it returns with the given unexpanded macro. There
        is no way to detect if the macro really existed or not.
        In addition for the use case of this class the macro should
        be read as it was configured and not its expanded form.
        """
        if self.root_dir:
            rpmdb_call = Command.run(
                ['chroot', self.root_dir, 'rpmdb', '--showrc']
            )
        else:
            rpmdb_call = Command.run(
                ['rpmdb', '--showrc']
            )
        return rpmdb_call.output.split(os.linesep)