Пример #1
0
    def _uninstall(self, pkg_name: str, root_password: str,
                   handler: ProcessHandler) -> bool:
        res = handler.handle(
            SystemProcess(
                new_root_subprocess(['pacman', '-R', pkg_name, '--noconfirm'],
                                    root_password)))

        if res:
            cached_paths = [
                ArchPackage.disk_cache_path(pkg_name, 'aur'),
                ArchPackage.disk_cache_path(pkg_name, 'mirror')
            ]

            for path in cached_paths:
                if os.path.exists(path):
                    shutil.rmtree(path)
                    break

        return res
Пример #2
0
    def run(self):
        if not any([self.aur, self.repositories]):
            return

        ti = time.time()
        self.task_man.register_task(self.task_id,
                                    self.i18n['arch.task.disk_cache'],
                                    get_icon_path())

        self.logger.info('Pre-caching installed Arch packages data to disk')
        installed = pacman.map_installed(repositories=self.repositories,
                                         aur=self.aur)

        self.task_man.update_progress(
            self.task_id, 0, self.i18n['arch.task.disk_cache.reading'])
        for k in ('signed', 'not_signed'):
            installed[k] = {
                p
                for p in installed[k]
                if not os.path.exists(ArchPackage.disk_cache_path(p))
            }

        saved = 0
        pkgs = {*installed['signed'], *installed['not_signed']}

        repo_map = {}

        if installed['not_signed']:
            repo_map.update({p: 'aur' for p in installed['not_signed']})

        if installed['signed']:
            repo_map.update(pacman.map_repositories(installed['signed']))

        self.to_index = len(pkgs)
        self.progress = self.to_index * 2
        self.update_prepared(None, add=False)

        saved += disk.save_several(pkgs,
                                   repo_map,
                                   when_prepared=self.update_prepared,
                                   after_written=self.update_indexed)
        self.task_man.update_progress(self.task_id, 100, None)
        self.task_man.finish_task(self.task_id)

        tf = time.time()
        time_msg = 'Took {0:.2f} seconds'.format(tf - ti)
        self.logger.info(
            'Pre-cached data of {} Arch packages to the disk. {}'.format(
                saved, time_msg))
Пример #3
0
def save_several(pkgnames: Set[str], mirror: str, overwrite: bool = True, maintainer: str = None) -> int:
    to_cache = {n for n in pkgnames if overwrite or not os.path.exists(ArchPackage.disk_cache_path(n, mirror))}
    desktop_files = pacman.list_desktop_entries(to_cache)

    no_desktop_files = {}

    to_write = []
    if desktop_files:
        desktop_matches, no_exact_match = {}, set()
        for pkg in to_cache:  # first try to find exact matches
            ends_with = re.compile('/usr/share/applications/{}.desktop$'.format(pkg), re.IGNORECASE)

            for f in desktop_files:
                if ends_with.match(f):
                    desktop_matches[pkg] = f
                    break

            if pkg not in desktop_matches:
                no_exact_match.add(pkg)
        if no_exact_match:  # check every not matched app individually
            for pkg in no_exact_match:
                entries = pacman.list_desktop_entries({pkg})

                if entries:
                    desktop_matches[pkg] = entries[0]

                    if len(entries) > 1:
                        for e in entries:
                            if e.startswith('/usr/share/applications'):
                                desktop_matches[pkg] = e
                                break

        if not desktop_matches:
            no_desktop_files = to_cache
        else:
            if len(desktop_matches) != len(to_cache):
                no_desktop_files = {p for p in to_cache if p not in desktop_matches}

            pkgs, apps_icons_noabspath = [], []

            for pkgname, file in desktop_matches.items():
                p = ArchPackage(name=pkgname, mirror=mirror)
                p.desktop_entry = file

                with open(file) as f:
                    desktop_entry = f.read()

                for field in RE_DESKTOP_ENTRY.findall(desktop_entry):
                    if field[0] == 'Exec':
                        p.command = field[1].strip().replace('"', '')
                    elif field[0] == 'Icon':
                        p.icon_path = field[1].strip()

                        if p.icon_path and '/' not in p.icon_path:  # if the icon full path is not defined
                            apps_icons_noabspath.append(p)

                pkgs.append(p)

            if apps_icons_noabspath:
                icon_paths = pacman.list_icon_paths({app.name for app in apps_icons_noabspath})
                if icon_paths:
                    for p in apps_icons_noabspath:
                        fill_icon_path(p, icon_paths, False)

            for p in pkgs:
                to_write.append(p)

    if no_desktop_files:
        pkgs = {ArchPackage(name=n, mirror=mirror) for n in no_desktop_files}
        bin_paths = pacman.list_bin_paths(no_desktop_files)

        if bin_paths:
            for p in pkgs:
                ends_with = re.compile(r'.+/{}$'.format(p.name), re.IGNORECASE)

                for path in bin_paths:
                    if ends_with.match(path):
                        p.command = path
                        break

        icon_paths = pacman.list_icon_paths(no_desktop_files)

        if icon_paths:
            for p in pkgs:
                fill_icon_path(p, icon_paths, only_exact_match=True)

        for p in pkgs:
            to_write.append(p)

    if to_write:
        for p in to_write:
            p.maintainer = maintainer
            write(p)
        return len(to_write)
    return 0
Пример #4
0
def save_several(pkgnames: Iterable[str],
                 repo_map: Dict[str, str],
                 overwrite: bool = True,
                 maintainer: str = None,
                 categories: dict = None,
                 when_prepared=None,
                 after_written=None) -> int:
    if overwrite:
        to_cache = pkgnames
    else:
        to_cache = {
            n
            for n in pkgnames
            if not os.path.exists(ArchPackage.disk_cache_path(n))
        }

    desktop_files = pacman.list_desktop_entries(to_cache)

    no_desktop_files = set()

    to_write = []

    if desktop_files:
        desktop_matches, no_exact_match = {}, set()
        for pkg in to_cache:  # first try to find exact matches
            try:
                clean_name = RE_CLEAN_NAME.sub('', pkg)
                ends_with = re.compile(
                    r'/usr/share/applications/{}.desktop$'.format(clean_name),
                    re.IGNORECASE)
            except:
                raise

            for f in desktop_files:
                if ends_with.match(f) and os.path.isfile(f):
                    desktop_matches[pkg] = f
                    break

            if pkg not in desktop_matches:
                no_exact_match.add(pkg)
        if no_exact_match:  # check every not matched app individually
            for pkg in no_exact_match:
                entries = pacman.list_desktop_entries({pkg})

                if entries:
                    if len(entries) > 1:
                        for e in entries:
                            if e.startswith('/usr/share/applications'
                                            ) and os.path.isfile(e):
                                desktop_matches[pkg] = e
                                break
                    else:
                        if os.path.isfile(entries[0]):
                            desktop_matches[pkg] = entries[0]

        if not desktop_matches:
            no_desktop_files = to_cache
        else:
            if len(desktop_matches) != len(to_cache):
                no_desktop_files = {
                    p
                    for p in to_cache if p not in desktop_matches
                }

            pkgs, apps_icons_noabspath = [], []

            for pkgname, file in desktop_matches.items():
                p = ArchPackage(name=pkgname, repository=repo_map.get(pkgname))

                with open(file) as f:
                    try:
                        desktop_entry = f.read()
                        p.desktop_entry = file

                        for field in RE_DESKTOP_ENTRY.findall(desktop_entry):
                            if field[0] == 'Exec':
                                p.command = field[1].strip().replace('"', '')
                            elif field[0] == 'Icon':
                                p.icon_path = field[1].strip()

                                if p.icon_path and '/' not in p.icon_path:  # if the icon full path is not defined
                                    apps_icons_noabspath.append(p)
                            elif field[0] == 'NoDisplay' and field[1].strip(
                            ).lower() == 'true':
                                p.command = None

                                if p.icon_path:
                                    apps_icons_noabspath.remove(p.icon_path)
                                    p.icon_path = None
                    except:
                        continue

                pkgs.append(p)

                if when_prepared:
                    when_prepared(p.name)

            if apps_icons_noabspath:
                icon_paths = pacman.list_icon_paths(
                    {app.name
                     for app in apps_icons_noabspath})
                if icon_paths:
                    for p in apps_icons_noabspath:
                        fill_icon_path(p, icon_paths, False)

            for p in pkgs:
                to_write.append(p)
    else:
        no_desktop_files = {*pkgnames}

    if no_desktop_files:
        bin_paths = pacman.list_bin_paths(no_desktop_files)
        icon_paths = pacman.list_icon_paths(no_desktop_files)

        for n in no_desktop_files:
            p = ArchPackage(name=n, repository=repo_map.get(n))

            if bin_paths:
                clean_name = RE_CLEAN_NAME.sub('', p.name)
                ends_with = re.compile(r'.+/{}$'.format(clean_name),
                                       re.IGNORECASE)

                for path in bin_paths:
                    if ends_with.match(path):
                        p.command = path
                        break
            if icon_paths:
                fill_icon_path(p, icon_paths, only_exact_match=True)

            to_write.append(p)

            if when_prepared:
                when_prepared(p.name)

    if to_write:
        written = set()
        for p in to_write:
            if categories:
                p.categories = categories.get(p.name)

            if maintainer and not p.maintainer:
                p.maintainer = maintainer

            write(p)

            if after_written:
                after_written(p.name)

            written.add(p.name)

        if len(to_write) != len(to_cache):
            for n in pkgnames:
                if n not in written:
                    Path(ArchPackage.disk_cache_path(n)).mkdir(parents=True,
                                                               exist_ok=True)
                    if after_written:
                        after_written(n)

        return len(to_write)
    return 0