summaryrefslogtreecommitdiff
path: root/aptsources
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2011-05-26 18:01:26 +0200
committerMichael Vogt <michael.vogt@ubuntu.com>2011-05-26 18:01:26 +0200
commit19e2e0f210da3fb4cb87cfe1ddd4bbc6c61d8cd1 (patch)
tree917dd7e06549e3ac7efffdae3c8e9d02788ac81d /aptsources
parente0b2560ab0fab5716187b7a1f93052373f5cbd23 (diff)
downloadpython-apt-19e2e0f210da3fb4cb87cfe1ddd4bbc6c61d8cd1.tar.gz
merge from debian, omit disable of the 0.7 API
Diffstat (limited to 'aptsources')
-rw-r--r--aptsources/distinfo.py167
-rw-r--r--aptsources/sourceslist.py87
2 files changed, 128 insertions, 126 deletions
diff --git a/aptsources/distinfo.py b/aptsources/distinfo.py
index a69f944a..249985e7 100644
--- a/aptsources/distinfo.py
+++ b/aptsources/distinfo.py
@@ -176,89 +176,90 @@ class DistInfo(object):
map_mirror_sets = {}
dist_fname = "%s/%s.info" % (base_dir, dist)
- dist_file = open(dist_fname)
- if not dist_file:
- return
- template = None
- component = None
- for line in dist_file:
- tokens = line.split(':', 1)
- if len(tokens) < 2:
- continue
- field = tokens[0].strip()
- value = tokens[1].strip()
- if field == 'ChangelogURI':
- self.changelogs_uri = _(value)
- elif field == 'MetaReleaseURI':
- self.metarelease_uri = value
- elif field == 'Suite':
- self.finish_template(template, component)
- component=None
- template = Template()
- template.name = value
- template.distribution = dist
- template.match_name = "^%s$" % value
- elif field == 'MatchName':
- template.match_name = value
- elif field == 'ParentSuite':
- template.child = True
- for nanny in self.templates:
- # look for parent and add back ref to it
- if nanny.name == value:
- template.parents.append(nanny)
- nanny.children.append(template)
- elif field == 'Available':
- template.available = apt_pkg.string_to_bool(value)
- elif field == 'Official':
- template.official = apt_pkg.string_to_bool(value)
- elif field == 'RepositoryType':
- template.type = value
- elif field == 'BaseURI' and not template.base_uri:
- template.base_uri = value
- elif field == 'BaseURI-%s' % self.arch:
- template.base_uri = value
- elif field == 'MatchURI' and not template.match_uri:
- template.match_uri = value
- elif field == 'MatchURI-%s' % self.arch:
- template.match_uri = value
- elif (field == 'MirrorsFile' or
- field == 'MirrorsFile-%s' % self.arch):
- # Make the path absolute.
- value = os.path.isabs(value) and value or \
- os.path.abspath(os.path.join(base_dir, value))
- if value not in map_mirror_sets:
- mirror_set = {}
- try:
- mirror_data = filter(match_mirror_line.match,
- [x.strip() for x in open(value)])
- except Exception:
- logging.warn("Failed to read mirror file")
- mirror_data = []
- for line in mirror_data:
- if line.startswith("#LOC:"):
- location = match_loc.sub(r"\1", line)
- continue
- (proto, hostname, dir) = split_url(line)
- if hostname in mirror_set:
- mirror_set[hostname].add_repository(proto, dir)
- else:
- mirror_set[hostname] = Mirror(
- proto, hostname, dir, location)
- map_mirror_sets[value] = mirror_set
- template.mirror_set = map_mirror_sets[value]
- elif field == 'Description':
- template.description = _(value)
- elif field == 'Component':
- if component and not template.has_component(component.name):
- template.components.append(component)
- component = Component(value)
- elif field == 'CompDescription':
- component.set_description(_(value))
- elif field == 'CompDescriptionLong':
- component.set_description_long(_(value))
- self.finish_template(template, component)
- template=None
- component=None
+ with open(dist_fname) as dist_file:
+ template = None
+ component = None
+ for line in dist_file:
+ tokens = line.split(':', 1)
+ if len(tokens) < 2:
+ continue
+ field = tokens[0].strip()
+ value = tokens[1].strip()
+ if field == 'ChangelogURI':
+ self.changelogs_uri = _(value)
+ elif field == 'MetaReleaseURI':
+ self.metarelease_uri = value
+ elif field == 'Suite':
+ self.finish_template(template, component)
+ component=None
+ template = Template()
+ template.name = value
+ template.distribution = dist
+ template.match_name = "^%s$" % value
+ elif field == 'MatchName':
+ template.match_name = value
+ elif field == 'ParentSuite':
+ template.child = True
+ for nanny in self.templates:
+ # look for parent and add back ref to it
+ if nanny.name == value:
+ template.parents.append(nanny)
+ nanny.children.append(template)
+ elif field == 'Available':
+ template.available = apt_pkg.string_to_bool(value)
+ elif field == 'Official':
+ template.official = apt_pkg.string_to_bool(value)
+ elif field == 'RepositoryType':
+ template.type = value
+ elif field == 'BaseURI' and not template.base_uri:
+ template.base_uri = value
+ elif field == 'BaseURI-%s' % self.arch:
+ template.base_uri = value
+ elif field == 'MatchURI' and not template.match_uri:
+ template.match_uri = value
+ elif field == 'MatchURI-%s' % self.arch:
+ template.match_uri = value
+ elif (field == 'MirrorsFile' or
+ field == 'MirrorsFile-%s' % self.arch):
+ # Make the path absolute.
+ value = os.path.isabs(value) and value or \
+ os.path.abspath(os.path.join(base_dir, value))
+ if value not in map_mirror_sets:
+ mirror_set = {}
+ try:
+ with open(value) as value_f:
+ mirror_data = filter(match_mirror_line.match,
+ [x.strip() for x in
+ value_f])
+ except Exception:
+ print "WARNING: Failed to read mirror file"
+ mirror_data = []
+ for line in mirror_data:
+ if line.startswith("#LOC:"):
+ location = match_loc.sub(r"\1", line)
+ continue
+ (proto, hostname, dir) = split_url(line)
+ if hostname in mirror_set:
+ mirror_set[hostname].add_repository(proto, dir)
+ else:
+ mirror_set[hostname] = Mirror(
+ proto, hostname, dir, location)
+ map_mirror_sets[value] = mirror_set
+ template.mirror_set = map_mirror_sets[value]
+ elif field == 'Description':
+ template.description = _(value)
+ elif field == 'Component':
+ if (component and not
+ template.has_component(component.name)):
+ template.components.append(component)
+ component = Component(value)
+ elif field == 'CompDescription':
+ component.set_description(_(value))
+ elif field == 'CompDescriptionLong':
+ component.set_description_long(_(value))
+ self.finish_template(template, component)
+ template=None
+ component=None
def finish_template(self, template, component):
" finish the current tempalte "
diff --git a/aptsources/sourceslist.py b/aptsources/sourceslist.py
index 04f6a5a7..b85e6947 100644
--- a/aptsources/sourceslist.py
+++ b/aptsources/sourceslist.py
@@ -1,4 +1,4 @@
-# aptsource.py - Provide an abstraction of the sources.list
+# sourceslist.py - Provide an abstraction of the sources.list
#
# Copyright (c) 2004-2009 Canonical Ltd.
# Copyright (c) 2004 Michiel Sikkes
@@ -276,6 +276,12 @@ class SourcesList(object):
yield entry
raise StopIteration
+ def __find(self, *predicates, **attrs):
+ for source in self.list:
+ if (all(getattr(source, key) == attrs[key] for key in attrs) and
+ all(predicate(source) for predicate in predicates)):
+ yield source
+
def add(self, type, uri, dist, orig_comps, comment="", pos=-1, file=None, architectures=[]):
"""
Add a new source to the sources.list.
@@ -287,39 +293,32 @@ class SourcesList(object):
# create a working copy of the component list so that
# we can modify it later
comps = orig_comps[:]
+ sources = self.__find(lambda s: set(s.architectures) == architectures,
+ disabled=False, invalid=False, type=type, uri=uri,
+ dist=dist)
# check if we have this source already in the sources.list
- for source in self.list:
- if not source.disabled and not source.invalid and \
- source.type == type and uri == source.uri and \
- source.dist == dist and \
- set(source.architectures) == architectures:
- for new_comp in comps:
- if new_comp in source.comps:
- # we have this component already, delete it
- # from the new_comps list
- del comps[comps.index(new_comp)]
- if len(comps) == 0:
- return source
- for source in self.list:
+ for source in sources:
+ for new_comp in comps:
+ if new_comp in source.comps:
+ # we have this component already, delete it
+ # from the new_comps list
+ del comps[comps.index(new_comp)]
+ if len(comps) == 0:
+ return source
+
+ sources = self.__find(lambda s: set(s.architectures) == architectures,
+ invalid=False, type=type, uri=uri, dist=dist)
+
+ for source in sources:
# if there is a repo with the same (type, uri, dist) just add the
# components
- if not source.disabled and not source.invalid and \
- source.type == type and uri == source.uri and \
- source.dist == dist and \
- set(source.architectures) == architectures:
- comps = uniq(source.comps + comps)
- source.comps = comps
- return source
- # if there is a corresponding repo which is disabled, enable it
- elif source.disabled and not source.invalid and \
- source.type == type and uri == source.uri and \
- set(source.architectures) == architectures and \
- source.dist == dist and \
- len(set(source.comps) & set(comps)) == len(comps):
+ if source.disabled and set(source.comps) == comps:
source.disabled = False
return source
+ elif not source.disabled:
+ source.comps = uniq(source.comps + comps)
+ return source
# there isn't any matching source, so create a new line and parse it
-
line = type
if architectures:
line += " [arch=%s]" % ",".join(architectures)
@@ -370,15 +369,12 @@ class SourcesList(object):
def load(self, file):
""" (re)load the current sources """
try:
- f = open(file, "r")
- lines = f.readlines()
- for line in lines:
- source = SourceEntry(line, file)
- self.list.append(source)
+ with open(file, "r") as f:
+ for line in f:
+ source = SourceEntry(line, file)
+ self.list.append(source)
except:
- logging.error("could not open file '%s'" % file)
- else:
- f.close()
+ print "could not open file '%s'" % file
def save(self):
""" save the current sources """
@@ -390,14 +386,19 @@ class SourcesList(object):
"## See sources.list(5) for more information, especialy\n"
"# Remember that you can only use http, ftp or file URIs\n"
"# CDROMs are managed through the apt-cdrom tool.\n")
- open(path, "w").write(header)
+
+ with open(path, "w") as f:
+ f.write(header)
return
- for source in self.list:
- if source.file not in files:
- files[source.file] = open(source.file, "w")
- files[source.file].write(source.str())
- for f in files:
- files[f].close()
+
+ try:
+ for source in self.list:
+ if source.file not in files:
+ files[source.file] = open(source.file, "w")
+ files[source.file].write(source.str())
+ finally:
+ for f in files:
+ files[f].close()
def check_for_relations(self, sources_list):
"""get all parent and child channels in the sources list"""