File: deb-check-file-conflicts

package info (click to toggle)
devscripts 2.25.15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,528 kB
  • sloc: perl: 26,530; sh: 11,698; python: 4,428; makefile: 363
file content (178 lines) | stat: -rwxr-xr-x 6,008 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name

#  Copyright © 2016 Maximiliano Curia <maxy@gnuservers.com.ar>

#  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, see <http://www.gnu.org/licenses/>.

"""
Check the produced binary packages and checks if there are conflicting files
against packages that are not declared with breaks and replaces.

Note that the results depend on what apt repositories are enabled. If you want
prevent upgrade issues across Debian releases, you need to have both unstable
and the previous release repositories enabled in the apt sources configuration.
"""

import argparse
import collections
import logging
import os
import subprocess
import sys

from debian import deb822, debfile, debian_support
from junit_xml import TestCase, TestSuite


def get_pkg_file(lines):
    found = collections.defaultdict(set)
    for line in lines.split("\n"):
        if not line:
            continue
        package, filename = line.split(": ", 1)
        found[package].add(filename)
    return found


def get_relations(field_value):
    relations = collections.defaultdict(lambda: collections.defaultdict(str))
    if not field_value:
        return relations
    parsed = deb822.PkgRelation.parse_relations(field_value)
    for or_part in parsed:
        for part in or_part:
            rel_name = part["name"]
            if "version" in part:
                if "version" in relations[rel_name]:
                    if (
                        debian_support.version_compare(
                            part["version"][1], relations[rel_name]["version"]
                        )
                        > 0
                    ):
                        relations[rel_name]["version"] = part["version"][1]
            else:
                relations[rel_name] = collections.defaultdict(str)
    return relations


def process_options():
    kw = {"format": "[%(levelname)s] %(message)s"}
    arg_parser = argparse.ArgumentParser(description=__doc__)
    arg_parser.add_argument("--debug", action="store_true")
    arg_parser.add_argument(
        "--changes-file", default=os.environ.get("CHANGES_FILE", "")
    )
    arg_parser.add_argument(
        "-o",
        "--output",
        help="Output file",
        default=f"{os.environ.get('EXPORT_DIR', '.')}/missing_breaks_replaces.xml",
    )
    args = arg_parser.parse_args()

    if args.debug:
        kw["level"] = logging.DEBUG

    logging.basicConfig(**kw)

    return args


def get_package_relations(deb_control):
    """Extract and parse package relation fields"""
    deb_replaces = deb_control.get("Replaces", "")
    deb_breaks = deb_control.get("Breaks", "")
    deb_conflicts = deb_control.get("Conflicts", "")

    return {
        "breaks": get_relations(deb_breaks),
        "conflicts": get_relations(deb_conflicts),
        "replaces": get_relations(deb_replaces),
    }


def process_entry(dirname, entry):
    """Process a single changes files entry"""
    logging.debug(entry["name"])
    deb_filename = os.path.join(dirname, entry["name"])

    deb_control = debfile.DebFile(deb_filename).debcontrol()
    name = deb_control["Package"]
    logging.info("Processing: %s %s", name, deb_control["Version"])

    relations = get_package_relations(deb_control)

    # apt-file now returns 1 if the files are not found, specially bothering
    # with the dbgsym packages (and the packages not yet uploaded)
    proc = subprocess.run(
        ["apt-file", "-D", "search", deb_filename],
        universal_newlines=True,
        stdout=subprocess.PIPE,
        check=False,
    )
    if proc.returncode and proc.stdout:
        proc.check_returncode()
    interesting = get_pkg_file(proc.stdout)
    result = []
    for pkg_name in interesting:
        if pkg_name == name or pkg_name in relations["conflicts"]:
            # TODO check versions
            continue
        if pkg_name in relations["breaks"] and pkg_name in relations["replaces"]:
            # TODO check versions
            continue
        msg = f"{name} conflicts with {pkg_name} files: {interesting[pkg_name]}"
        logging.error("Missing Breaks/Replaces found")
        logging.error(msg)
        result.append(msg)

    return name, result, proc.stdout


def generate_test_cases(results):
    test_cases = []
    for name, (result, output) in results.items():
        test_case = TestCase(name, stdout=output)
        if result:
            test_case.add_error_info("\n".join(result))

        test_cases.append(test_case)

    return test_cases


def main():
    """Check changes files for missing Breaks/Replaces using apt-file"""
    args = process_options()
    dirname = os.path.dirname(args.changes_file)
    results = {}
    with open(args.changes_file, encoding="utf-8") as changes_file:
        changes = deb822.Changes(changes_file)
        for entry in changes["Files"]:
            if not entry["name"].endswith(".deb"):
                continue
            name, result, output = process_entry(dirname, entry)
            results[name] = (result, output)
    test_cases = generate_test_cases(results)
    test_suite = TestSuite("check_for_missing_breaks_replaces", test_cases)
    with open(args.output, "w", encoding="utf-8") as output_file:
        output_file.write(TestSuite.to_xml_string([test_suite]))
    return 1 if any(test_case.is_error() for test_case in test_cases) else 0


if __name__ == "__main__":
    sys.exit(main())