|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +This plugin is a `pacman` (Arch Linux Package Manager) wrapper. You can update, search, install and remove \ |
| 5 | +packages. |
| 6 | +""" |
| 7 | + |
| 8 | +import subprocess |
| 9 | +from time import sleep |
| 10 | +import pathlib |
| 11 | + |
| 12 | +from albert import Action, Item, QueryHandler, runTerminal |
| 13 | + |
| 14 | +md_iid = "0.5" |
| 15 | +md_version = "1.6" |
| 16 | +md_name = "PacMan" |
| 17 | +md_description = "Search, install and remove packages" |
| 18 | +md_license = "BSD-3" |
| 19 | +md_url = "https://github.com/albertlauncher/python/tree/master/pacman" |
| 20 | +md_bin_dependencies = ["pacman", "expac"] |
| 21 | + |
| 22 | + |
| 23 | +class Plugin(QueryHandler): |
| 24 | + |
| 25 | + pkgs_url = "https://www.archlinux.org/packages/" |
| 26 | + |
| 27 | + def id(self): |
| 28 | + return md_id |
| 29 | + |
| 30 | + def name(self): |
| 31 | + return md_name |
| 32 | + |
| 33 | + def description(self): |
| 34 | + return md_description |
| 35 | + |
| 36 | + def synopsis(self): |
| 37 | + return "<package name>" |
| 38 | + |
| 39 | + def defaultTrigger(self): |
| 40 | + return "pac " |
| 41 | + |
| 42 | + def initialize(self): |
| 43 | + self.icons = [ |
| 44 | + "xdg:archlinux-logo", |
| 45 | + "xdg:system-software-install", |
| 46 | + str(pathlib.Path(__file__).parent / "arch.svg") |
| 47 | + ] |
| 48 | + |
| 49 | + def handleQuery(self, query): |
| 50 | + stripped = query.string.strip() |
| 51 | + |
| 52 | + # Update item on empty queries |
| 53 | + if not stripped: |
| 54 | + query.add(Item( |
| 55 | + id="%s-update" % md_id, |
| 56 | + text="Pacman package manager", |
| 57 | + subtext="Enter the package you are looking for or hit enter to update.", |
| 58 | + icon=self.icons, |
| 59 | + actions=[ |
| 60 | + Action("up-nc", "Update packages (no confirm)", |
| 61 | + lambda: runTerminal("sudo pacman -Syu --noconfirm")), |
| 62 | + Action("up", "Update packages", lambda: runTerminal("sudo pacman -Syu")), |
| 63 | + Action("up-cache", "Update pacman cache", lambda: runTerminal("sudo pacman -Sy")) |
| 64 | + ] |
| 65 | + )) |
| 66 | + return |
| 67 | + |
| 68 | + # avoid rate limiting |
| 69 | + for number in range(50): |
| 70 | + sleep(0.01) |
| 71 | + if not query.isValid: |
| 72 | + return |
| 73 | + |
| 74 | + # Get data. Results are sorted so we can merge in O(n) |
| 75 | + proc_s = subprocess.Popen(["expac", "-Ss", "%n\t%v\t%r\t%d\t%u\t%E", stripped], |
| 76 | + stdout=subprocess.PIPE, universal_newlines=True) |
| 77 | + proc_q = subprocess.Popen(["expac", "-Qs", "%n", stripped], stdout=subprocess.PIPE, universal_newlines=True) |
| 78 | + proc_q.wait() |
| 79 | + |
| 80 | + items = [] |
| 81 | + local_pkgs = set(proc_q.stdout.read().split('\n')) |
| 82 | + remote_pkgs = [tuple(line.split('\t')) for line in proc_s.stdout.read().split('\n')[:-1]] # newline at end |
| 83 | + |
| 84 | + for pkg_name, pkg_vers, pkg_repo, pkg_desc, pkg_purl, pkg_deps in remote_pkgs: |
| 85 | + if stripped not in pkg_name : |
| 86 | + continue |
| 87 | + |
| 88 | + pkg_installed = True if pkg_name in local_pkgs else False |
| 89 | + |
| 90 | + actions = [] |
| 91 | + if pkg_installed: |
| 92 | + actions.extend([ |
| 93 | + Action("rem", "Remove", lambda: runTerminal("sudo pacman -Rs %s" % pkg_name)), |
| 94 | + Action("reinst", "Reinstall", lambda: runTerminal("sudo pacman -S %s" % pkg_name)) |
| 95 | + ]) |
| 96 | + else: |
| 97 | + actions.append(Action("inst", "Install", lambda: runTerminal("sudo pacman -S %s" % pkg_name))) |
| 98 | + actions.append(Action("pkg_url", "Show on packages.archlinux.org", |
| 99 | + lambda: openUrl(f"{pkg_repo}/x86_64/{pkg_name}/"))) |
| 100 | + if pkg_purl: |
| 101 | + actions.append(Action("proj_url", "Show project website", lambda: openUrl(pkg_purl))) |
| 102 | + |
| 103 | + item = Item( |
| 104 | + id="%s_%s_%s" % (md_id, pkg_repo, pkg_name), |
| 105 | + icon=self.icons, |
| 106 | + text="%s %s [%s]" % (pkg_name, pkg_vers, pkg_repo), |
| 107 | + subtext=f"{pkg_desc} [Installed]" if pkg_installed else f"{pkg_desc}", |
| 108 | + completion="%s%s" % (query.trigger, pkg_name), |
| 109 | + actions=actions |
| 110 | + ) |
| 111 | + items.append(item) |
| 112 | + |
| 113 | + if items: |
| 114 | + query.add(items) |
| 115 | + else: |
| 116 | + query.add(Item( |
| 117 | + id="%s-empty" % md_id, |
| 118 | + text="Search on archlinux.org", |
| 119 | + subtext="No results found in the local database", |
| 120 | + icon=self.icons, |
| 121 | + actions=[Action("search", "Search on archlinux.org", lambda: openUrl(f"{self.pkgs_url}?q={stripped}"))] |
| 122 | + )) |
0 commit comments