/usr/lib/python3/dist-packages/debdrylib/tree.py is in debdry 0.2.2-1.
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | #!/usr/bin/python3
# coding: utf8
import os
import io
import re
import shutil
import apt_pkg
from collections import OrderedDict
import tempfile
import difflib
import logging
log = logging.getLogger(__name__)
class Node:
pass
class FilesystemNode(Node):
def __init__(self, relname, absname):
self.relname = relname
self.absname = absname
def dump(self, out, level=0):
print("{}{}".format(" " * level, self.relname), file=out)
class DirectoryBase(FilesystemNode):
def __init__(self, relname, absname):
super().__init__(relname, absname)
self.files = {}
def dump(self, out, level=0):
super().dump(out, level)
for n in self.files.values():
n.dump(out, level + 1)
class DebianDirectory(DirectoryBase):
"""
Represent a debian/ directory
"""
def __init__(self, srcdir, absname=None):
if absname is None: absname = os.path.join(srcdir, "debian")
super().__init__(os.path.basename(absname), absname)
self.srcdir = srcdir
@classmethod
def scan(cls, srcdir, absname=None):
"""
Init the DebianDirectory by scanning the filesystem
"""
self = cls(srcdir, absname)
for relname in os.listdir(self.absname):
absname = os.path.join(self.absname, relname)
if relname == "control":
log.info("%s: control file", absname)
self.files[relname] = Control.scan(relname, absname)
elif relname == "rules":
log.info("%s: rules file", absname)
self.files[relname] = Rules.scan(relname, absname)
elif os.path.isdir(absname):
log.info("%s: subdirectory", absname)
self.files[relname] = Directory.scan(relname, absname)
else:
log.info("%s: plain file", absname)
self.files[relname] = File.scan(relname, absname)
return self
@classmethod
def combine(cls, srcdir, absname, base, extra):
"""
Init the DebianDirectory by overlaying extra on top of base
"""
os.makedirs(absname, exist_ok=True)
self = cls(srcdir, absname)
for k in base.files.keys() - extra.files.keys():
self.files[k] = base.files[k].copy_to(k, os.path.join(self.absname, k))
for k in extra.files.keys() - base.files.keys():
self.files[k] = extra.files[k].copy_to(k, os.path.join(self.absname, k))
for k in base.files.keys() & extra.files.keys():
self.files[k] = base.files[k].combine(k, os.path.join(self.absname, k), extra.files[k])
return self
@classmethod
def diff(cls, srcdir, absname, base, extra):
"""
Init the DebianDirectory with the diff to go from base to extra
"""
os.makedirs(absname, exist_ok=True)
self = cls(srcdir, absname)
for k in base.files.keys() - extra.files.keys():
log.warn("{}: exists in {} but not in {}: ignoring file".format(
self.relname, base.absname, extra.absname))
for k in extra.files.keys() - base.files.keys():
self.files[k] = extra.files[k].copy_to(k, os.path.join(self.absname, k))
for k in base.files.keys() & extra.files.keys():
self.files[k] = base.files[k].diff(k, os.path.join(self.absname, k), extra.files[k])
return self
class Directory(DirectoryBase):
"""
Represent a subdirectory of debian/, any level down
"""
@classmethod
def scan(cls, relname, absname):
self = cls(relname, absname)
for relname in os.listdir(self.absname):
absname = os.path.join(self.absname, relname)
if os.path.isdir(absname):
log.info("%s: subdirectory", absname)
self.files[relname] = Directory(relname, absname)
else:
log.info("%s: plain file", absname)
self.files[relname] = File(relname, absname)
return self
def copy_to(self, relname, absname):
log.debug("%s: generating subdir", absname)
os.makedirs(absname, exist_ok=True)
for name, node in self.files.items():
node.copy_to(name, os.path.join(absname, name))
return self.__class__(relname, absname)
def combine(self, relname, absname, other):
os.makedirs(absname, exist_ok=True)
res = self.__class__(relname, absname)
for k in self.files.keys() - other.files.keys():
self.files[k] = self.files[k].copy_to(k, os.path.join(self.absname, k))
for k in other.files.keys() - self.files.keys():
self.files[k] = other.files[k].copy_to(k, os.path.join(self.absname, k))
for k in other.files.keys() & self.files.keys():
self.files[k] = self.files[k].combine(k, os.path.join(self.absname, k), other.files[k])
return self
def diff(self, relname, absname, other):
os.makedirs(absname, exist_ok=True)
res = self.__class__(relname, absname)
for k in self.files.keys() - other.files.keys():
log.warn("{}: exists in {} but not in {}: ignoring file".format(
self.relname, base.absname, extra.absname))
for k in other.files.keys() - self.files.keys():
self.files[k] = other.files[k].copy_to(k, os.path.join(self.absname, k))
for k in other.files.keys() & self.files.keys():
self.files[k] = self.files[k].diff(k, os.path.join(self.absname, k), other.files[k])
return self
class DeletedFile(FilesystemNode):
def dump(self, out, level=0):
print("{}{} (deleted)".format(" " * level, self.relname), file=out)
class File(FilesystemNode):
"""
Represent a file anywhere under debian/
"""
def open(self, mode="rt", encoding="utf8", **kw):
return io.open(self.absname, mode=mode, encoding=encoding, **kw)
def read(self, **kw):
with io.open(self.absname, **kw) as fd:
return fd.read()
def readlines(self, **kw):
with io.open(self.absname, **kw) as fd:
return fd.readlines()
def copy_to(self, relname, absname):
log.info("%s: copying from %s", absname, self.absname)
shutil.copy2(self.absname, absname)
return self.__class__(relname, absname)
def combine(self, relname, absname, other):
shutil.copy2(other.absname, absname)
return self.__class__.scan(relname, absname)
def diff(self, relname, absname, other):
if self.read() != other.read():
shutil.copy2(other.absname, absname)
return self.__class__.scan(relname, absname)
else:
return DeletedFile(relname, absname)
@classmethod
def scan(cls, relname, absname):
return cls(relname, absname)
class Rules(File):
"""
Represent a debian/rules file
"""
@property
def contents(self):
res = getattr(self, "_contents", None)
if res is not None: return res
self._contents = self.read()
return self._contents
@classmethod
def scan(cls, relname, absname):
return cls(relname, absname)
def combine(self, relname, absname, other):
if other.contents.startswith("#!"):
shutil.copy2(other.absname, absname)
else:
with io.open(absname, "wt") as fd:
fd.write(self.contents)
fd.write("\n")
fd.write(other.contents)
shutil.copystat(other.absname, absname)
return self.__class__.scan(relname, absname)
def diff(self, relname, absname, other):
log.warn("%s: diffing debian/rules is not yet supported: generating a diff instead", self.relname)
my_lines = self.readlines()
other_lines = other.readlines()
dstpathname = absname + ".diff"
with io.open(dstpathname, "wt") as out:
for line in difflib.unified_diff(my_lines, other_lines, "debian.auto/rules", "debian/rules"):
out.write(line)
return File(relname + ".diff", dstpathname)
def dump(self, out, level=0):
if self.contents.startswith("#!"):
print("{}{} (full)".format(" " * level, self.relname), file=out)
else:
print("{}{} (partial)".format(" " * level, self.relname), file=out)
class Control(File):
SOURCE_FIELDS = frozenset(apt_pkg.REWRITE_SOURCE_ORDER)
BINARY_FIELDS = frozenset(apt_pkg.REWRITE_PACKAGE_ORDER)
PKGLIST_FIELDS = frozenset(("Depends", "Pre-Depends", "Recommends",
"Suggests", "Breaks", "Conflicts", "Provides",
"Replaces", "Enhances", "Build-Depends"))
def __init__(self, relname, absname):
super().__init__(relname, absname)
# Source: section
self.source = ControlSectionSource()
# Package: sections
self.binaries = OrderedDict()
def _scan_lines(self, data):
for section in apt_pkg.TagFile(data):
if 'Source' in section:
for tag in section.keys():
self.add_source_field(tag, section[tag])
elif 'Package' in section:
name = section["Package"]
for tag in section.keys():
self.add_binary_field(name, tag, section[tag])
else:
# If there is no Source or Package in this stanza, dispatch
# according to field names
for tag in section.keys():
self.add_mixed_field(tag, section[tag])
@classmethod
def scan(cls, relname, absname):
self = cls(relname, absname)
with self.open() as fd:
self._scan_lines(fd)
return self
@classmethod
def scan_string(cls, relname, absname, buf):
self = cls(relname, absname)
with tempfile.TemporaryFile() as fd:
fd.write(buf.encode("utf8"))
fd.seek(0)
self._scan_lines(fd)
return self
def to_string(self):
sections = []
# Add the source section
sections.append(self.source.to_string())
# Add the binary sections
for section in self.binaries.values():
sections.append(section.to_string())
return "\n".join(sections)
def write(self):
# Write out the concatenation of all the stanzas
with io.open(self.absname, "wt") as outfd:
outfd.write(self.to_string())
def combine(self, relname, absname, other):
res = self.__class__(relname, absname)
res.source = self.source.combine(other.source)
# Add the binary sections that are in self but not in other
for name in self.binaries.keys() - other.binaries.keys():
res.binaries[name] = self.binaries[name].copy()
#log.warn("%s: Package: %s exists only in auto: ignored", absname, name)
# Add the binary sections that are in other but not in self
for name in other.binaries.keys() - self.binaries.keys():
res.binaries[name] = other.binaries[name].copy()
# Merge the binary sections that are in both, taking auto as the
# baseline
for name in self.binaries.keys() & other.binaries.keys():
res.binaries[name] = self.binaries[name].combine(other.binaries[name])
# Write out the file
res.write()
return res
def diff(self, relname, absname, other):
res = self.__class__(relname, absname)
res.source = self.source.diff(other.source)
# Add the binary sections that are in self but not in other
for name in self.binaries.keys() - other.binaries.keys():
log.warn("%s: Package: %s exists only in auto: ignored", absname, name)
# Add the binary sections that are in other but not in self
for name in other.binaries.keys() - self.binaries.keys():
res.binaries[name] = other.binaries[name].copy()
# Merge the binary sections that are in both, taking auto as the
# baseline
for name in self.binaries.keys() & other.binaries.keys():
res.binaries[name] = self.binaries[name].diff(other.binaries[name])
# Write out the file
res.write()
return res
def add_source_field(self, tag, value):
if tag.startswith("X-Debdry-"):
tag = tag[9:]
smart = True
else:
smart = False
if tag == "Uploaders":
self.source.add(tag, UploadersControlField(tag, value, smart))
elif tag in self.PKGLIST_FIELDS:
self.source.add(tag, PkglistControlField(tag, value, smart))
else:
if smart:
log.warn("%s: unsupported field X-Debdry-%s found: treating it as %s",
self.absname, tag, tag)
self.source.add(tag, PlainControlField(tag, value))
def add_binary_field(self, pkgname, tag, value):
if tag.startswith("X-Debdry-"):
tag = tag[9:]
smart = True
else:
smart = False
rec = self.binaries.get(pkgname, None)
if rec is None:
self.binaries[pkgname] = rec = ControlSectionBinary()
if tag in self.PKGLIST_FIELDS:
rec.add(tag, PkglistControlField(tag, value, smart))
else:
if smart:
log.warn("%s: unsupported field X-Debdry-%s found: treating it as %s",
self.absname, tag, tag)
rec.add(tag, PlainControlField(tag, value))
def add_mixed_field(self, tag, value):
orig_tag = tag
if tag.startswith("X-Debdry-"):
tag = tag[9:]
if tag in self.SOURCE_FIELDS:
self.add_source_field(orig_tag, value)
elif tag in self.BINARY_FIELDS:
self.add_binary_field(None, orig_tag, value)
else:
log.warn("%s: ignoring unrecognised field '%s'", self.absname, orig_tag)
def dump(self, out, level=0):
super().dump(out, level)
print("{}src:".format(" " * (level + 1)))
self.source.dump(out, level + 1)
for pkgname, section in self.binaries.items():
print("{}bin {}:".format(" " * (level + 1), pkgname))
section.dump(out, level + 1)
class ControlSection:
def __init__(self):
self.fields = OrderedDict()
def add(self, tag, field):
if field is None: return
self.fields[tag] = field
def dump(self, out, level=0):
for tag, field in self.fields.items():
field.dump(out, level + 1)
def copy(self):
res = self.__class__()
for k, v in self.fields.items():
res.fields[k] = v.copy()
return res
def to_string(self):
section = self.get_empty_tagsection()
return apt_pkg.rewrite_section(section, self.REWRITE_ORDER,
[f.to_apt() for f in self.fields.values()])
def combine(self, other):
res = self.__class__()
# All fields in self are copied
for tag in self.fields.keys() - other.fields.keys():
res.add(tag, self.fields[tag].copy())
# Then all fields in other are copied
for tag in other.fields.keys() - self.fields.keys():
res.add(tag, other.fields[tag].copy())
# Combine the fields that are in both
for tag in self.fields.keys() & other.fields.keys():
res.add(tag, self.fields[tag].combine(other.fields[tag]))
return res
def diff(self, other):
res = self.__class__()
# FIXME: we still have no way to delete a field
for tag in self.fields.keys() - other.fields.keys():
log.warn("control field %s exists only in auto: ignored", tag)
# Then all fields in other are copied
for tag in other.fields.keys() - self.fields.keys():
res.add(tag, other.fields[tag].copy())
# Combine the fields that are in both
for tag in self.fields.keys() & other.fields.keys():
if tag in ("Source", "Package"):
res.add(tag, other.fields[tag].copy())
else:
res.add(tag, self.fields[tag].diff(other.fields[tag]))
return res
class ControlSectionSource(ControlSection):
REWRITE_ORDER = apt_pkg.REWRITE_SOURCE_ORDER
def get_empty_tagsection(self):
name = self.fields.get("Source", None)
if name is None:
raise RuntimeError("TODO: package is name is missing for source section")
return apt_pkg.TagSection("Source: {}\n".format(name))
class ControlSectionBinary(ControlSection):
REWRITE_ORDER = apt_pkg.REWRITE_PACKAGE_ORDER
def get_empty_tagsection(self):
name = self.fields.get("Package", None)
if name is None:
raise RuntimeError("TODO: package is name is missing for binary section")
return apt_pkg.TagSection("Package: {}\n".format(name))
class PlainControlField(Node):
def __init__(self, tag, value):
self.tag = tag
self.value = value
def dump(self, out, level=0):
print("{}{}: {}".format(" " * level, self.tag, self.value[:40]))
def copy(self):
return self.__class__(self.tag, self.value)
def to_string(self):
return self.value
def to_apt(self):
return (self.tag, self.value)
def combine(self, other):
return self.__class__(self.tag, other.value)
def diff(self, other):
if self.value == other.value:
return None
return self.__class__(self.tag, other.value)
class IndexedControlField(Node):
def __init__(self, tag, smart):
self.tag = tag
self.smart = smart
self.values = OrderedDict()
def dump(self, out, level=0):
if self.smart:
tag = "X-Debdry-" + self.tag
else:
tag = self.tag
if len(self.values) > 1:
print("{}{}: {} and {} more".format(" " * level,
tag,
", ".join(self.values.values())[:40],
len(self.values) - 1))
else:
print("{}{}: {}".format(" " * level,
tag,
", ".join(self.values.values())[:40]))
def copy(self):
return self.__class__(self.tag, self.to_string(), self.smart)
def to_string(self):
return ", ".join(self.values.values())
def to_apt(self):
if self.smart:
return ("X-Debdry-" + self.tag, self.to_string())
else:
return (self.tag, self.to_string())
def combine(self, other):
if not other.smart:
value = other.to_string()
else:
res = OrderedDict()
for k, v in self.values.items():
res[k] = v
for k, v in other.values.items():
res[k] = v
value = ", ".join(res.values())
return self.__class__(self.tag, value, self.smart)
def diff(self, other):
smart = False
if self.to_string() == other.to_string():
value = None
elif self.values.keys() - other.values.keys():
# If there are values in self and not in others, do a full field
# override
value = other.to_string()
else:
res = OrderedDict()
# Common fields first
for k in self.values.keys() & other.values.keys():
theirs = other.values[k]
# Skip common fields
if self.values[k] == theirs: continue
res[k] = theirs
# Then other-only fields
for k in other.values.keys() - self.values.keys():
res[k] = other.values[k]
if res:
smart = True
value = ", ".join(res.values())
else:
value = None
if value is None: return None
return self.__class__(self.tag, value, smart)
class UploadersControlField(IndexedControlField):
re_split_uploaders = re.compile(r'(?<=>)\s*,\s*')
re_email = re.compile(r'<([^>]+)>')
def __init__(self, tag, value, smart):
super().__init__(tag, smart)
# Store uploaders indexed by email address
for v in self.re_split_uploaders.split(value):
self.values[self._get_email(v)] = v
def _get_email(self, maint):
mo = self.re_email.search(maint)
if not mo:
raise ValueError("Unparsable email: {}".format(repr(maint)))
return mo.group(1)
class PkglistControlField(IndexedControlField):
re_split_deps = re.compile(r"\s*,\s*")
def __init__(self, tag, value, smart):
super().__init__(tag, smart)
# Store package entries indexed by package name
for v in self.re_split_deps.split(value):
if not v: continue
self.values[v.split()[0]] = v
|