(self, new_mirror=None, update=True)
Change the main mirror in use in :attr:`main_sources_list`.

        :param new_mirror: The URL of the new mirror (a string, defaults to
                           :attr:`best_mirror`).
        :param update: Whether an ``apt-get update`` should be run after
                       changing the mirror (a boolean, defaults to
                       :data:`True`).
Change the main mirror in use in :attr:`main_sources_list`.
Change the main mirror in use in : attr : main_sources_list .
def change_mirror(self, new_mirror=None, update=True):
        """
        Change the main mirror in use in :attr:`main_sources_list`.

        :param new_mirror: The URL of the new mirror (a string, defaults to
                           :attr:`best_mirror`).
        :param update: Whether an ``apt-get update`` should be run after
                       changing the mirror (a boolean, defaults to
                       :data:`True`).
        """
        timer = Timer()
        # Default to the best available mirror.
        if new_mirror:
            logger.info("Changing mirror of %s to %s ..", self.context, new_mirror)
        else:
            logger.info("Changing mirror of %s to best available mirror ..", self.context)
            new_mirror = self.best_mirror
            logger.info("Selected mirror: %s", new_mirror)
        # Parse /etc/apt/sources.list to replace the old mirror with the new one.
        sources_list = self.get_sources_list()
        mirrors_to_replace = [normalize_mirror_url(self.current_mirror)]
        if self.release_is_eol:
            # When a release goes EOL the security updates mirrors stop
            # serving that release as well, so we need to remove them.
            logger.debug("Replacing %s URLs as well ..", self.security_url)
            mirrors_to_replace.append(normalize_mirror_url(self.security_url))
        else:
            logger.debug("Not replacing %s URLs.", self.security_url)
        lines = sources_list.splitlines()
        sources_list_options = self.get_sources_list_options
        for i, line in enumerate(lines):
            # The first token should be `deb' or `deb-src', the second token is
            # the mirror's URL, the third token is the `distribution' and any
            # further tokens are `components'.
            tokens = line.split()
            if (len(tokens) >= 4
                    and tokens[0] in ('deb', 'deb-src')
                    and normalize_mirror_url(tokens[1]) in mirrors_to_replace):
                tokens[1] = new_mirror
                if i in sources_list_options:
                    tokens.insert(1, '[' + sources_list_options[i] + ']')  # Get the [options] back
                lines[i] = u' '.join(tokens)
        # Install the modified package resource list.
        self.install_sources_list(u'\n'.join(lines))
        # Clear (relevant) cached properties.
        del self.current_mirror
        # Make sure previous package lists are removed.
        self.clear_package_lists()
        # Make sure the package lists are up to date.
        if update:
            self.smart_update(switch_mirrors=False)
        logger.info("Finished changing mirror of %s in %s.", self.context, timer)
def change_mirror ( self , new_mirror = None , update = True ) : timer = Timer ( ) # Default to the best available mirror. if new_mirror : logger . info ( "Changing mirror of %s to %s .." , self . context , new_mirror ) else : logger . info ( "Changing mirror of %s to best available mirror .." , self . context ) new_mirror = self . best_mirror logger . info ( "Selected mirror: %s" , new_mirror ) # Parse /etc/apt/sources.list to replace the old mirror with the new one. sources_list = self . get_sources_list ( ) mirrors_to_replace = [ normalize_mirror_url ( self . current_mirror ) ] if self . release_is_eol : # When a release goes EOL the security updates mirrors stop # serving that release as well, so we need to remove them. logger . debug ( "Replacing %s URLs as well .." , self . security_url ) mirrors_to_replace . append ( normalize_mirror_url ( self . security_url ) ) else : logger . debug ( "Not replacing %s URLs." , self . security_url ) lines = sources_list . splitlines ( ) sources_list_options = self . get_sources_list_options for i , line in enumerate ( lines ) : # The first token should be `deb' or `deb-src', the second token is # the mirror's URL, the third token is the `distribution' and any # further tokens are `components'. tokens = line . split ( ) if ( len ( tokens ) >= 4 and tokens [ 0 ] in ( 'deb' , 'deb-src' ) and normalize_mirror_url ( tokens [ 1 ] ) in mirrors_to_replace ) : tokens [ 1 ] = new_mirror if i in sources_list_options : tokens . insert ( 1 , '[' + sources_list_options [ i ] + ']' ) # Get the [options] back lines [ i ] = u' ' . join ( tokens ) # Install the modified package resource list. self . install_sources_list ( u'\n' . join ( lines ) ) # Clear (relevant) cached properties. del self . current_mirror # Make sure previous package lists are removed. self . clear_package_lists ( ) # Make sure the package lists are up to date. if update : self . smart_update ( switch_mirrors = False ) logger . info ( "Finished changing mirror of %s in %s." , self . context , timer )