Move read_sources_file to utils

Signed-off-by: Niels Thykier <niels@thykier.net>
email-direct-upload-sponsor
Niels Thykier 8 years ago
parent 0f45b195a6
commit c4be8436db

@ -207,6 +207,7 @@ from britney2.utils import (old_libraries_format, undo_changes,
old_libraries, is_nuninst_asgood_generous, old_libraries, is_nuninst_asgood_generous,
clone_nuninst, check_installability, clone_nuninst, check_installability,
create_provides_map, read_release_file, create_provides_map, read_release_file,
read_sources_file,
) )
__author__ = 'Fabio Tranchitella and the Debian Release Team' __author__ = 'Fabio Tranchitella and the Debian Release Team'
@ -767,46 +768,9 @@ class Britney(object):
self._inst_tester = builder.build() self._inst_tester = builder.build()
# Data reading/writing methods # Data reading/writing methods
# ---------------------------- # ----------------------------
def _read_sources_file(self, filename, sources=None, intern=sys.intern):
if sources is None:
sources = {}
self.log("Loading source packages from %s" % filename)
Packages = apt_pkg.TagFile(filename)
get_field = Packages.section.get
step = Packages.step
while step():
if get_field('Extra-Source-Only', 'no') == 'yes':
# Ignore sources only referenced by Built-Using
continue
pkg = get_field('Package')
ver = get_field('Version')
# There may be multiple versions of the source package
# (in unstable) if some architectures have out-of-date
# binaries. We only ever consider the source with the
# largest version for migration.
if pkg in sources and apt_pkg.version_compare(sources[pkg][0], ver) > 0:
continue
maint = get_field('Maintainer')
if maint:
maint = intern(maint.strip())
section = get_field('Section')
if section:
section = intern(section.strip())
sources[intern(pkg)] = SourcePackage(intern(ver),
section,
[],
maint,
False,
)
return sources
def read_sources(self, basedir): def read_sources(self, basedir):
"""Read the list of source packages from the specified directory """Read the list of source packages from the specified directory
@ -824,10 +788,12 @@ class Britney(object):
for component in self.options.components: for component in self.options.components:
filename = os.path.join(basedir, component, "source", "Sources") filename = os.path.join(basedir, component, "source", "Sources")
filename = possibly_compressed(filename) filename = possibly_compressed(filename)
self._read_sources_file(filename, sources) self.log("Loading source packages from %s" % filename)
read_sources_file(filename, sources)
else: else:
filename = os.path.join(basedir, "Sources") filename = os.path.join(basedir, "Sources")
sources = self._read_sources_file(filename) self.log("Loading source packages from %s" % filename)
sources = read_sources_file(filename)
return sources return sources

@ -24,6 +24,7 @@
import apt_pkg import apt_pkg
import errno import errno
import os import os
import sys
import time import time
from collections import defaultdict from collections import defaultdict
from datetime import datetime from datetime import datetime
@ -32,6 +33,7 @@ from itertools import filterfalse
import yaml import yaml
from britney2 import SourcePackage
from britney2.consts import (VERSION, PROVIDES, DEPENDS, CONFLICTS, from britney2.consts import (VERSION, PROVIDES, DEPENDS, CONFLICTS,
ARCHITECTURE, SECTION, ARCHITECTURE, SECTION,
SOURCE, MAINTAINER, MULTIARCH, SOURCE, MAINTAINER, MULTIARCH,
@ -677,3 +679,51 @@ def read_release_file(suite_dir):
if next(tag_file, None) is not None: # pragma: no cover if next(tag_file, None) is not None: # pragma: no cover
raise TypeError("%s has more than one paragraph" % release_file) raise TypeError("%s has more than one paragraph" % release_file)
return result return result
def read_sources_file(filename, sources=None, intern=sys.intern):
"""Parse a single Sources file into a hash
Parse a single Sources file into a dict mapping a source package
name to a SourcePackage object. If there are multiple source
packages with the same version, then highest versioned source
package (that is not marked as "Extra-Source-Only") is the
version kept in the dict.
:param filename: Path to the Sources file. Can be compressed by any algorithm supported by apt_pkg.TagFile
:param sources: Optional dict to add the packages to. If given, this is also the value returned.
:param intern: Internal optimisation / implementation detail to avoid python's "LOAD_GLOBAL" instruction in a loop
:return a dict mapping a name to a source package
"""
if sources is None:
sources = {}
tag_file = apt_pkg.TagFile(filename)
get_field = tag_file.section.get
step = tag_file.step
while step():
if get_field('Extra-Source-Only', 'no') == 'yes':
# Ignore sources only referenced by Built-Using
continue
pkg = get_field('Package')
ver = get_field('Version')
# There may be multiple versions of the source package
# (in unstable) if some architectures have out-of-date
# binaries. We only ever consider the source with the
# largest version for migration.
if pkg in sources and apt_pkg.version_compare(sources[pkg][0], ver) > 0:
continue
maint = get_field('Maintainer')
if maint:
maint = intern(maint.strip())
section = get_field('Section')
if section:
section = intern(section.strip())
sources[intern(pkg)] = SourcePackage(intern(ver),
section,
[],
maint,
False,
)
return sources

Loading…
Cancel
Save