def mirror_from_sources_list(self, uri, default_uri): """ try to figure what the mirror is from current sources.list do this by looing for matching DEFAULT_COMPONENT, current dist in sources.list and then doing a http HEAD/ftp size request to see if the uri is available on this server """ self._debug("mirror_from_sources_list: %s" % self.current_dist_name) sources = SourcesList(withMatcher=False) seen = set() for e in sources.list: if e.disabled or e.invalid or not e.type == "deb": continue # check if we probed this mirror already if e.uri in seen: continue # we are using the main mirror already, so we are fine if (e.uri.startswith(default_uri) and e.dist == self.current_dist_name and self.DEFAULT_COMPONENT in e.comps): return uri elif (e.dist == self.current_dist_name and "main" in e.comps): mirror_uri = e.uri+uri[len(default_uri):] if url_downloadable(mirror_uri, self._debug): return mirror_uri seen.add(e.uri) self._debug("no mirror found") return ""
def _expandUri(self, uri): """ expand the uri so that it uses a mirror if the url starts with a well know string (like archive.ubuntu.com) """ # try to guess the mirror from the sources.list if uri.startswith(self.DEFAULT_MIRROR): self._debug("trying to find suitable mirror") new_uri = self.mirror_from_sources_list(uri, self.DEFAULT_MIRROR) if new_uri: return new_uri # if that fails, use old method uri_template = Template(uri) m = country_mirror() new_uri = uri_template.safe_substitute(countrymirror=m) # be paranoid and check if the given uri is really downloadable try: if not url_downloadable(new_uri, self._debug): raise Exception("failed to download %s" % new_uri) except Exception,e: self._debug("url '%s' could not be downloaded" % e) # else fallback to main server new_uri = uri_template.safe_substitute(countrymirror='')