diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-03-01 12:01:17 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-03-01 12:01:17 +0100 |
commit | 4855300f98ab9ffe393b2aa26c3d22b2dd9f4636 (patch) | |
tree | ba6a3b2a45142457df3fcace6fb1f145a59c136a | |
parent | add uname (diff) | |
download | xpybar-4855300f98ab9ffe393b2aa26c3d22b2dd9f4636.tar.gz xpybar-4855300f98ab9ffe393b2aa26c3d22b2dd9f4636.tar.bz2 xpybar-4855300f98ab9ffe393b2aa26c3d22b2dd9f4636.tar.xz |
add pacman
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | examples/plugin-test | 30 | ||||
-rw-r--r-- | src/plugins/pacman.py | 141 |
2 files changed, 171 insertions, 0 deletions
diff --git a/examples/plugin-test b/examples/plugin-test index d9ead29..2945e95 100644 --- a/examples/plugin-test +++ b/examples/plugin-test @@ -7,6 +7,7 @@ import os from plugins.uptime import Uptime from plugins.loadavg import AverageLoad from plugins.users import Users +from plugins.pacman import Pacman from plugins.uname import Uname @@ -44,10 +45,39 @@ def redraw(): users = ['%s{%i}' % (colour_user(u) % u, len(users_[u])) for u in users_.keys()] users = 'Users: %s' % (' '.join(users)) + have_linux_libre, have_pacman = True, True + linux_installed, linux_latest = None, None + try: + linux_installed = Pacman('linux-libre', True) + except: + have_linux_libre = False + try: + linux_installed = Pacman('linux', True) + except: + have_pacman = False + if have_pacman: + linux_latest = Pacman('linux-libre' if have_linux_libre else 'linux', False) + uname_ = Uname() nodename = uname_.nodename kernel_release = uname_.kernel_release operating_system = uname_.operating_system + if have_pacman: + linux_running = kernel_release.split('-') + linux_running, kernel_release = linux_running[:2], linux_running[2:] + linux_running = '-'.join(linux_running) + kernel_release = '-' + '-'.join(kernel_release) + linux_installed = linux_installed.version + linux_latest = linux_latest.version + if linux_installed == linux_latest: + if linux_running == linux_installed: + linux_running = '\033[32m%s\033[39m' % linux_running + else: + if linux_running == linux_installed: + linux_running = '\033[33m%s\033[39m' % linux_running + else: + linux_running = '\033[31m%s\033[39m' % linux_running + kernel_release = linux_running + kernel_release uname = '%s %s %s' uname %= (nodename, kernel_release, operating_system) diff --git a/src/plugins/pacman.py b/src/plugins/pacman.py new file mode 100644 index 0000000..5d2aef8 --- /dev/null +++ b/src/plugins/pacman.py @@ -0,0 +1,141 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +''' + +from util import * + + +class Pacman: + ''' + Retrieve package information using Arch Linux's pacman + + Unique variables for installed: + + @variable required:list<str> Packages that require this package + @variable optrequired:list<str> Packages that this package is optional for + @variable install_date:str The date and time the package was installed + @variable install_explicitly:bool Whether the package was installed explicitly + @variable install_script:bool Whether the package contains and install script + + Unique variables for not installed: + + @variable repository:str The repository the package is located in + @variable download_size:float The download size of the package in kilobytes + + Common variables: + + @variable name:str The name of the package + @variable version:str The version of the package + @variable description:str The description of the package + @variable architecture:str The microprocessor architecture of the package + @variable url:str The home URL of the project + @variable licenses:list<str> The licenses used by the package + @variable groups:list<str> Package groups the package is a member of + @variable provides:list<str> Packages the package provides + @variable depends:list<str> Packages the package depends on + @variable optdepends:list<(str, str?, bool)> Optional packages for this package, stored as a tuple of: + The name of the package, the reason for using it, + and whether or not it is installed. + @variable conflicts:list<str> Packages the package conflicts with + @variable replaces:list<str> Packages the package replaces + @variable installed_size:float The installed size of the package in kilobytes + @variable packager:str? The packager of the package, `None` if unknown + @variable build_date:str The date and time the package was built + @variable validated_by:list<str> Methods used to validate the package's integrity + ''' + + + def __init__(self, package, installed): + ''' + Constructor + + @param package:str The name of the package, can be %repo/%name + @param installed:bool Whether it is information about the installed version that should be fetched + ''' + verb = '-Qi' if installed else '-Si' # -Sii and -Qii, while a bit different, would get us more info. + info = spawn_read('pacman', verb, package).split('\n') # TODO fixate locale + if len(info) == 1: + raise Exception('Package `%s\' is not installed' % package) + + last_field = None + fields = {} + + for line in info: + continued = line.startswith(' ') + line = list(filter(lambda cell : not cell == '', line.split(' '))) + colon = -1 + if not continued: + for i in range(len(line)): + if line[i] == ':': + colon = i + break + field = last_field + if colon >= 0: + field = ' '.join(line[:colon]) + line = line[colon + 1:] + if field not in fields: + fields[field] = [] + fields[field].append(line) + + + word = lambda x : fields[x][0][0] + text = lambda x : ' '.join(fields[x][0]) + plur = lambda x : list(filter(lambda w : not w == 'None', fields[x][0])) + + if installed: + self.required = plur('Required By') + self.optrequired = plur('Optional For') + self.install_date = text('Install Date') + self.install_explicitly = text('Install Reason') == 'Explicitly installed' + self.install_script = text('Install Script') == 'Yes' + + if not installed: + self.repository = word('Name') + self.download_size = float(word('Download Size')) + + self.name = word('Name') + self.version = word('Version') + self.description = text('Description') + self.architecture = word('Architecture') + self.url = text('URL') + self.licenses = plur('Licences') + self.groups = plur('Groups') + self.provides = plur('Provides') + self.depends = plur('Depends On') + self.optdepends = [] + optdepends_ = [' '.join(x) for x in fields['Optional Deps']] + for deps in optdepends_: + inst = deps.endswith(' [installed]') + if installed: + deps = deps[:-len(' [installed]')] + if ': ' in deps: + deps = deps.split(': ') + name = deps[0] + deps = ': '.join(deps[1:]) + self.optdepends.append((name, deps, inst)) + else: + self.optdepends.append((deps, None, inst)) + self.conflicts = plur('Conflicts With') + self.replaces = plur('Replaces') + self.installed_size = float(word('Installed Size')) + self.packager = text('Packager') + if self.packager == 'Unknown Packager': + self.packager = None + self.build_date = text('Build Date') + self.validated_by = list(filter(lambda x : not x == 'Sum', plur('Validated By'))) + |