Ejemplo n.º 1
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        res = []

        if snapd.is_running():
            self.logger.info(
                'Downloading suggestions file {}'.format(SUGGESTIONS_FILE))
            file = self.http_client.get(SUGGESTIONS_FILE)

            if not file or not file.text:
                self.logger.warning(
                    "No suggestion found in {}".format(SUGGESTIONS_FILE))
                return res
            else:
                self.logger.info('Mapping suggestions')

                suggestions, threads = [], []
                snapd_client = SnapdClient(self.logger)
                installed = {
                    s['name'].lower()
                    for s in snapd_client.list_all_snaps()
                }

                for l in file.text.split('\n'):
                    if l:
                        if limit <= 0 or len(suggestions) < limit:
                            sug = l.strip().split('=')
                            name = sug[1]

                            if not installed or name not in installed:
                                cached_sug = self.suggestions_cache.get(name)

                                if cached_sug:
                                    res.append(cached_sug)
                                else:
                                    t = Thread(target=self._fill_suggestion,
                                               args=(name,
                                                     SuggestionPriority(
                                                         int(sug[0])),
                                                     snapd_client, res))
                                    t.start()
                                    threads.append(t)
                                    time.sleep(0.001)  # to avoid being blocked
                        else:
                            break

                for t in threads:
                    t.join()

                res.sort(key=lambda s: s.priority.value, reverse=True)
        return res
Ejemplo n.º 2
0
 def read_installed(self,
                    disk_loader: DiskCacheLoader,
                    limit: int = -1,
                    only_apps: bool = False,
                    pkg_types: Set[Type[SoftwarePackage]] = None,
                    internet_available: bool = None) -> SearchResult:
     if snap.is_installed() and snapd.is_running():
         snapd_client = SnapdClient(self.logger)
         app_names = {a['snap'] for a in snapd_client.list_only_apps()}
         installed = [
             self._map_to_app(app_json=appjson,
                              installed=True,
                              disk_loader=disk_loader,
                              is_application=app_names
                              and appjson['name'] in app_names)
             for appjson in snapd_client.list_all_snaps()
         ]
         return SearchResult(installed, None, len(installed))
     else:
         return SearchResult([], None, 0)
Ejemplo n.º 3
0
    def list_suggestions(
            self, limit: int,
            filter_installed: bool) -> Optional[List[PackageSuggestion]]:
        if limit == 0 or not snapd.is_running():
            return

        if self.is_local_suggestions_file_mapped():
            suggestions_str = self._read_local_suggestions_file()
        else:
            suggestions_str = self._download_remote_suggestions_file()

        if suggestions_str is None:
            return

        if not suggestions_str:
            self.logger.warning(
                f"No Snap suggestion found in {self.suggestions_url}")
            return

        ids_prios = suggestions.parse(suggestions_str, self.logger, 'Snap')

        if not ids_prios:
            self.logger.warning(
                f"No Snap suggestion could be parsed from {self.suggestions_url}"
            )
            return

        suggestion_by_priority = suggestions.sort_by_priority(ids_prios)
        snapd_client = SnapdClient(self.logger)

        if filter_installed:
            installed = {
                s['name'].lower()
                for s in snapd_client.list_all_snaps()
            }

            if installed:
                suggestion_by_priority = tuple(n
                                               for n in suggestion_by_priority
                                               if n not in installed)

        if suggestion_by_priority and 0 < limit < len(suggestion_by_priority):
            suggestion_by_priority = suggestion_by_priority[0:limit]

        self.logger.info(
            f'Available Snap suggestions: {len(suggestion_by_priority)}')

        if not suggestion_by_priority:
            return

        self.logger.info("Mapping Snap suggestions")

        instances, threads = [], []

        res, cached_count = [], 0
        for name in suggestion_by_priority:
            cached_sug = self.suggestions_cache.get(name)

            if cached_sug:
                res.append(cached_sug)
                cached_count += 1
            else:
                t = Thread(target=self._fill_suggestion,
                           args=(name, ids_prios[name], snapd_client, res))
                t.start()
                threads.append(t)
                time.sleep(0.001)  # to avoid being blocked

        for t in threads:
            t.join()

        if cached_count > 0:
            self.logger.info(
                f"Returning {cached_count} cached Snap suggestions")

        return res