Ejemplo n.º 1
0
 def _feed_in_private_sources_list_entry(self, source_entry):
     """
     this feeds in a private sources.list entry that is
     available to the user (like a private PPA) that may or
     may not be active 
     """
     # FIXME: strip out password and use apt/auth.conf
     potential_new_entry = SourceEntry(source_entry)
     # look if we have it
     sources = SourcesList()
     for source in sources.list:
         if source == potential_new_entry:
             return False
     # need to add it as a not yet enabled channel
     name = human_readable_name_from_ppa_uri(potential_new_entry.uri)
     # FIXME: use something better than uri as name
     private_channel = SoftwareChannel(name,
                                       None,
                                       None,
                                       source_entry=source_entry)
     private_channel.needs_adding = True
     if private_channel in self.extra_channels:
         return False
     # add it
     self.extra_channels.append(private_channel)
     return True
Ejemplo n.º 2
0
    def __init__(self, datadir=None, options=None, rootdir="/"):
        """ Provides the core functionality to configure the used software 
        repositories, the corresponding authentication keys and 
        update automation """
        self.popconfile = rootdir + "/etc/popularity-contest.conf"

        self.rootdir = rootdir
        if rootdir != "/":
            apt_pkg.config.set("Dir", rootdir)

        # FIXME: some saner way is needed here
        if datadir == None:
            datadir = "/usr/share/software-properties/"
        self.options = options
        self.datadir = datadir

        self.sourceslist = SourcesList()
        self.distro = aptsources.distro.get_distro()

        self.seen_server = []
        self.modified_sourceslist = False

        self.reload_sourceslist()
        self.backup_sourceslist()

        self.backup_apt_conf()

        # FIXME: we need to store this value in a config option
        #self.custom_mirrors = ["http://adasdwww.de/ubuntu"]
        self.custom_mirrors = []

        # apt-key stuff
        self.apt_key = AptAuth(rootdir=rootdir)

        atexit.register(self.wait_for_threads)
    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 ""
Ejemplo n.º 4
0
class Daemon(PolicyKitService):
    #TODO use signal
    liststate = None
    list = SourcesList()
    #    cache = apt.Cache()
    stable_url = 'http://ppa.launchpad.net/tualatrix/ppa/ubuntu'
    ppa_list = []
    p = None
    SOURCES_LIST = '/etc/apt/sources.list'

    def __init__(self, bus, mainloop):
        bus_name = dbus.service.BusName(INTERFACE, bus=bus)
        PolicyKitService.__init__(self, bus_name, PATH)
        self.mainloop = mainloop

    @dbus.service.method(INTERFACE, in_signature='b', out_signature='bv')
    def check_sources_is_valid(self, convert_source):
        try:
            if not os.path.exists(self.SOURCES_LIST):
                os.system('touch %s' % self.SOURCES_LIST)
        except Exception, e:
            log.error(e)

        self.list.refresh()
        to_add_entry = []
        to_rm_entry = []
        disabled_list = ['']

        for entry in self.list:
            entry_line = entry.str().strip()
            if entry.invalid and not entry.disabled and entry_line and not entry_line.startswith(
                    '#'):
                try:
                    entry.set_enabled(False)
                except Exception, e:
                    log.error(e)
                if entry.file not in disabled_list:
                    disabled_list.append(entry.file)
                continue

            if convert_source:
                if os.path.basename(entry.file) == 'sources.list':
                    continue
                log.debug("Check for url: %s, type: %s, filename: %s" %
                          (entry.uri, entry.type, entry.file))
                if ppa.is_ppa(entry.uri):
                    filename = '%s-%s.list' % (ppa.get_source_file_name(
                        entry.uri), entry.dist)
                    if filename != os.path.basename(entry.file):
                        log.debug("[%s] %s need rename to %s" %
                                  (entry.type, entry.file, filename))
                        to_rm_entry.append(entry)
                        if os.path.exists(entry.file):
                            log.debug("%s is exists, so remove it" %
                                      entry.file)
                            os.remove(entry.file)
                        entry.file = os.path.join(os.path.dirname(entry.file),
                                                  filename)
                        to_add_entry.append(entry)
Ejemplo n.º 5
0
 def get_unique_hosts_from_apt_list(self):
     sl = SourcesList()
     sl.refresh()
     hosts = [
         urlparse(e.uri).netloc for e in sl if e.uri.startswith('http')
     ]
     unique_hosts = list(dict.fromkeys(hosts))
     self.logger.debug("Parsed unique hosts from apt lists: %s" %
                       json.dumps(unique_hosts))
     return unique_hosts
Ejemplo n.º 6
0
    def check_source_upgradable(self):
        log.debug("The check source string is: \"%s\"" %
                  self.__get_disable_string())
        for source in SourcesList():
            if self.__get_disable_string() in source.str() and \
                    source.uri in UPGRADE_DICT and \
                    source.disabled:
                return True

        return False
Ejemplo n.º 7
0
    def get_channels(self):
        """Return a list of channels configured.

        A channel is a deb line in sources.list or sources.list.d. It's
        represented by a dict with baseurl, distribution, components,
        and type keys.
        """
        sources_list = SourcesList()
        return [{"baseurl": entry.uri, "distribution": entry.dist,
                 "components": " ".join(entry.comps), "type": entry.type}
                for entry in sources_list if not entry.disabled]
Ejemplo n.º 8
0
 def _remove_no_longer_needed_extra_channels(self, backend, res):
     """ go over the extra channels and remove no longer needed ones"""
     removed = False
     for channel in self.extra_channels:
         if not channel._source_entry:
             continue
         sources = SourcesList()
         for source in sources.list:
             if source == SourceEntry(channel._source_entry):
                 self.extra_channels.remove(channel)
                 removed = True
     if removed:
         self.backend.emit("channels-changed", True)
Ejemplo n.º 9
0
    def update_sources(self):
        #First hide the item not supported by current distribution
        self.execute_script('''
                var system_codename = "%s";
                var ubuntu_codenames = %s;
                console.log("Updating source for system: " + system_codename + ', codenames: ' + ubuntu_codenames);
                var appController = Utapp.get('router.appController');
                appController.currentApp.sources.forEach(function(source) {
                    var distro_value = '';
                    console.log(source.name + " is filtering codename for: ");
                    source.distros.forEach(function(distro) {
                        var codename = distro.codename;
                        console.log('\t' + codename);
                        if (ubuntu_codenames.contains(codename)){
                            console.log('\t\tThis is ubuntu codename');
                            if (system_codename == codename) {
                                console.log('\t\tCodename match!');
                                distro_value = codename;
                                return false;
                            }
                        } else {
                            console.log("\t\tThis isn't Ubuntu codename!");
                            distro_value = codename;
                            return false;
                        };
                    });
                    if (distro_value == '') {
                        console.log('Set source: ' + source.name + ' to hide');
                        source.set('is_available', false);
                    } else {
                        source.set('distro_value', distro_value);
                    }
                });
                ''' % (system.CODENAME, list(system.UBUNTU_CODENAMES)))

        enabled_list = []

        for source in SourcesList().list:
            if source.type == 'deb' and not source.disabled:
                enabled_list.append(source.uri)

        self.execute_script('''
                var enabled_list = %s;
                $(".source-view").each(function(index) {
                    if (enabled_list.contains($(this).attr('urldata')))
                    {
                        this.checked = true;
                    }
                    console.log(index + ': ' + $(this).attr('urldata'));
                });
                ''' % enabled_list)
Ejemplo n.º 10
0
 def _getSourcesList(self):
     try:
         import apt_pkg
         from aptsources.sourceslist import SourcesList
         apt_pkg.init()
     except (SystemError, ImportError):
         return []
     sources_list = set([])
     # The matcher parses info files from
     # /usr/share/python-apt/templates/
     # But we don't use the calculated data, skip it
     for source in SourcesList(withMatcher=False):
         if not source.disabled and not source.invalid:
             for component in source.comps:
                 sources_list.add(component)
     return sources_list
Ejemplo n.º 11
0
def main(allowed_protocols, allowed_domains):
    check_debianos()
    sourceslist = SourcesList()
    external = ''
    protocols = '|'.join(allowed_protocols)
    domains = '|'.join(allowed_domains)

    sources = set(e.uri for e in sourceslist
                  if e.uri and not str(e).startswith('#'))
    regex = re.compile(r'^({0})://([^:]+:[^@]+@)?({1})(/)?'.format(
        protocols, domains))
    for source in sources:
        if not regex.match(source):
            external += '{}, '.format(source)
    if external:
        print('WARNING: external sources found: {0}'.format(external))
        sys.exit(1)

    print('OK: no external repos found')
Ejemplo n.º 12
0
    def _rewrite_sources_list(self, targetdir, new_distro):
        from aptsources.sourceslist import SourcesList, SourceEntry
        apt_pkg.config.set(
            "Dir::Etc::sourcelist",
            os.path.abspath(
                os.path.join(targetdir, "etc", "apt", "sources.list")))
        apt_pkg.config.set(
            "Dir::Etc::sourceparts",
            os.path.abspath(
                os.path.join(targetdir, "etc", "apt", "sources.list.d")))
        sources = SourcesList()

        for entry in sources.list[:]:
            if entry.invalid or entry.disabled:
                continue
            replacement = ''
            for pocket in ('updates', 'security', 'backports'):
                if entry.dist.endswith('-%s' % pocket):
                    replacement = '%s-%s' % (new_distro, pocket)
                    break
            if replacement:
                entry.dist = replacement
            else:
                entry.dist = new_distro

        existing = os.path.join(targetdir, "etc", "apt",
                                "sources.list.apt-clone")
        sourcelist = apt_pkg.config.find_file("Dir::Etc::sourcelist")
        if os.path.exists(existing):
            with open(existing, 'r') as fp:
                for line in fp:
                    src = SourceEntry(line, sourcelist)
                    if (src.invalid or src.disabled) or src not in sources:
                        sources.list.append(src)
            os.remove(existing)

        for entry in sources.list:
            if entry.uri.startswith('cdrom:'):
                # Make sure CD entries come first.
                sources.list.remove(entry)
                sources.list.insert(0, entry)
                entry.disabled = True
        sources.save()
Ejemplo n.º 13
0
class Daemon(PolicyKitService):
    #TODO use signal
    liststate = None
    list = SourcesList()
    cache = None
    stable_url = 'http://ppa.launchpad.net/tualatrix/ppa/ubuntu'
    ppa_list = []
    p = None
    SOURCES_LIST = '/etc/apt/sources.list'

    def __init__(self, bus, mainloop):
        bus_name = dbus.service.BusName(INTERFACE, bus=bus)
        PolicyKitService.__init__(self, bus_name, PATH)
        self.mainloop = mainloop

    def get_cache(self):
        try:
            self.update_apt_cache()
        except Exception, e:
            log.error("Error happened when get_cache(): %s" % str(e))
        finally:
Ejemplo n.º 14
0
 def _restore_sources_list(self, statefile, targetdir, mirror=None):
     with tarfile.open(statefile) as tar:
         existing = os.path.join(targetdir, "etc", "apt", "sources.list")
         if os.path.exists(existing):
             shutil.copy(existing, '%s.apt-clone' % existing)
         tar.extract(self.TARPREFIX + "etc/apt/sources.list", targetdir)
         td_sources = os.path.join(targetdir, "etc", "apt", "sources.list")
         os.chmod(td_sources,
                  stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
         if mirror:
             from aptsources.sourceslist import SourcesList
             apt_pkg.config.set("Dir::Etc::sourcelist", td_sources)
             sources = SourcesList()
             for entry in sources.list[:]:
                 if entry.uri != mirror:
                     entry.uri = mirror
             sources.save()
         try:
             tar.extract(self.TARPREFIX + "etc/apt/sources.list.d",
                         targetdir)
         except KeyError:
             pass
Ejemplo n.º 15
0
    def enableSection(self, apturl):
        # parse sources.list
        sourceslist = SourcesList()
        distro = aptsources.distro.get_distro()
        distro.get_sources(sourceslist)

        # check if we actually need to enable anything
        requested_components = []
        for component in apturl.section:
            if not component in distro.enabled_comps:
                requested_components.append(component)
        # if not, we are fine
        if not requested_components:
            return RESULT_OK
        # otherwise ask the user if the really wants to anble them
        if not self.ui.askEnableSections(apturl.section):
            return RESULT_CANCELT
        if not self.ui.doEnableSection(apturl.section):
            self.ui.error(_("Enabling '%s' failed") %
                          ", ".join(apturl.section))
            return RESULT_ERROR
        self.ui.doUpdate()
        self.openCache()
        return RESULT_OK
def main():
    uri = sys.argv[1].strip()
    dist = sys.argv[2].strip()
    comps = sys.argv[3].split(' ')
    comment = sys.argv[4].strip()

    distro = aptsources.distro.get_distro()
    sourceslist = SourcesList()

    for source in sourceslist:
        if source.disabled == False:
            if source.uri == uri and source.comps == comps:
                print "套件庫已存在。"
            else:
                distro.get_sources(sourceslist)
                distro.add_source(uri=uri,
                                  dist=dist,
                                  comps=comps,
                                  comment=comment)

    sourceslist.backup()
    sourceslist.save()

    return 0
Ejemplo n.º 17
0
 def get_sourceslist(self):
     return SourcesList()
Ejemplo n.º 18
0
 def reset_channels(self):
     """Remove all the configured channels."""
     sources_list = SourcesList()
     for entry in sources_list:
         entry.set_enabled(False)
     sources_list.save()