/usr/lib/python3/dist-packages/diffoscope/comparators/debian.py is in diffoscope 93ubuntu1.
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 | # -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2014-2015 Jérémy Bobbio <lunar@debian.org>
#
# diffoscope 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.
#
# diffoscope 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 diffoscope. If not, see <https://www.gnu.org/licenses/>.
import re
import os.path
import hashlib
import logging
import functools
from debian.deb822 import Dsc
from diffoscope.changes import Changes
from diffoscope.difference import Difference
from .utils.file import File
from .utils.container import Container
logger = logging.getLogger(__name__)
class DebControlMember(File):
def __init__(self, container, member_name):
self._container = container
self._name = member_name
self._path = None
@property
def container(self):
return self._container
@property
def name(self):
return self._name
@property
def path(self):
return os.path.join(
os.path.dirname(self.container.source.path),
self.name,
)
def is_directory(self):
return False
def is_symlink(self):
return False
def is_device(self):
return False
class DebControlContainer(Container):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._version_re = DebControlContainer.get_version_trimming_re(self)
@staticmethod
def get_version_trimming_re(dcc):
version = dcc.source.deb822.get('Version')
# Remove the epoch as it's not in the filename
version = re.sub(r'^\d+:', '', version)
if '-' in version:
upstream, revision = version.rsplit('-', 1)
return re.compile(r'_%s(?:-%s)?' % (
re.escape(upstream),
re.escape(revision),
))
return re.compile(re.escape(version))
def get_adjusted_members(self):
for name in self.get_member_names():
yield self._trim_version_number(name), self.get_member(name)
def get_member_names(self):
field = self.source.deb822.get('Files') or \
self.source.deb822.get('Checksums-Sha256')
# Show results from debugging packages last; they are rather verbose,
# masking other more interesting differences due to truncating the
# output.
return sorted(
(x['name'] for x in field),
key=lambda x: (x.endswith('.deb') and '-dbgsym_' in x, x),
)
def get_member(self, member_name):
return DebControlMember(self, member_name)
def _trim_version_number(self, name):
return self._version_re.sub('', name)
class DebControlFile(File):
CONTAINER_CLASS = DebControlContainer
@property
def deb822(self):
return self._deb822
def compare_details(self, other, source=None):
differences = []
for field in sorted(set(self.deb822.keys()).union(set(other.deb822.keys()))):
if field.startswith('Checksums-') or field == 'Files':
continue
my_value = ""
if field in self.deb822:
my_value = self.deb822.get_as_string(field).lstrip()
other_value = ""
if field in other.deb822:
other_value = other.deb822.get_as_string(field).lstrip()
differences.append(Difference.from_text(
my_value,
other_value,
self.path,
other.path,
source=field,
))
# Compare Files as string
if self.deb822.get('Files'):
differences.append(Difference.from_text(
self.deb822.get_as_string('Files'),
other.deb822.get_as_string('Files'),
self.path,
other.path,
source='Files',
))
else:
differences.append(Difference.from_text(
self.deb822.get_as_string('Checksums-Sha256'),
other.deb822.get_as_string('Checksums-Sha256'),
self.path,
other.path,
source='Checksums-Sha256',
))
return differences
class DotChangesFile(DebControlFile):
DESCRIPTION = "Debian .changes files"
FILE_EXTENSION_SUFFIX = '.changes'
@classmethod
def recognizes(cls, file):
if not super().recognizes(file):
return False
changes = Changes(filename=file.path)
try:
changes.validate(check_signature=False)
except FileNotFoundError:
return False
file._deb822 = changes
return True
def compare(self, other, *args, **kwargs):
differences = super().compare(other, *args, **kwargs)
if differences is None:
return None
files = zip(self.deb822.get('Files'), other.deb822.get('Files'))
files_identical = all(
x == y for x, y in files
if not x['name'].endswith('.buildinfo')
)
if files_identical and \
len(differences.details) == 1 and \
differences.details[0].source1 == 'Files':
logger.warning("Ignoring buildinfo file differences")
return None
return differences
class DotDscFile(DebControlFile):
DESCRIPTION = "Debian source packages (.dsc)"
FILE_EXTENSION_SUFFIX = '.dsc'
@classmethod
def recognizes(cls, file):
if not super().recognizes(file):
return False
with open(file.path, 'rb') as f:
dsc = Dsc(f)
for d in dsc.get('Files'):
md5 = hashlib.md5()
# XXX: this will not work for containers
in_dsc_path = os.path.join(
os.path.dirname(file.path),
d['Name'],
)
if not os.path.exists(in_dsc_path):
return False
with open(in_dsc_path, 'rb') as f:
for buf in iter(functools.partial(f.read, 32768), b''):
md5.update(buf)
if md5.hexdigest() != d['md5sum']:
return False
file._deb822 = dsc
return True
class DotBuildinfoContainer(DebControlContainer):
def get_member_names(self):
result = super(DotBuildinfoContainer, self).get_member_names()
# As a special-case, if the parent container of this .buildinfo is a
# .changes file, ignore members here that are referenced in both. This
# avoids recursing into files twice where a .buildinfo references a
# file that is also listed in that member's parent .changes file:
#
# foo.changes → foo.deb
# foo.changes → foo.buildinfo → foo.deb
#
ignore = set()
if isinstance(self.source.container, DebControlContainer):
ignore.update(self.source.container.get_member_names())
return [x for x in result if x not in ignore]
class DotBuildinfoFile(DebControlFile):
DESCRIPTION = "Debian .buildinfo files"
CONTAINER_CLASS = DotBuildinfoContainer
FILE_EXTENSION_SUFFIX = '.buildinfo'
@classmethod
def recognizes(cls, file):
if not super().recognizes(file):
return False
with open(file.path, 'rb') as f:
# We can parse .buildinfo files just like .dsc
buildinfo = Dsc(f)
if 'Checksums-Sha256' not in buildinfo:
return False
for d in buildinfo.get('Checksums-Sha256'):
sha256 = hashlib.sha256()
# XXX: this will not work for containers
in_buildinfo_path = os.path.join(
os.path.dirname(file.path),
d['Name'],
)
if not os.path.exists(in_buildinfo_path):
return False
with open(in_buildinfo_path, 'rb') as f:
for buf in iter(functools.partial(f.read, 32768), b''):
sha256.update(buf)
if sha256.hexdigest() != d['sha256']:
return False
file._deb822 = buildinfo
return True
|