You are not logged in.

#1 2012-09-16 02:08:11

jakhead
Member
Registered: 2011-11-08
Posts: 9

Conky - Display available pacman updates

This is a python script I found and modified to display package update info with conky.

The original was written by Majkhii and Sabooky which I found here via the wiki entry for conky:
https://bbs.archlinux.org/viewtopic.php?id=37284

This one is much simpler than it's predecessor. It will simply display a list of package names and their sizes. I couldn't figure how to keep the rating system from the original which allowed for highlighting "important" packages. Anyway here it is. I'm pretty new to programming so if something doesn't seem to make sense, it's probably because it doesn't. smile

pacupdate.th.png    pacmanu2d.th.png

#!/usr/bin/env python2
"""
~/.conky/notify.py

-Description: Python script for displaying archlinux updates.
 
-Usage: create shell script with the contents:
-----------------------------------------------

#!/bin/bash
#
# ~/pacsync.sh
# This syncs pacman and creates a file to store
# info for each package. This is so the python 
# portion doesn't constantly use your network
# connection every 20 seconds to get package info 
# when it refreshes itself. Better to have the
# shell script update once every several minutes
# and have python use that info instead. Don't
# forget to make executeable with chmod +x

destfile=~/.pacsync.info

sudo pacman -Sy

if [ -f $destfile ]
then
  rm $destfile
fi

list=`pacman -Qu | cut -d ' ' -f 1`
for x in $list
do
  echo $x >> $destfile
  echo `pacman -Si $x | egrep -w 'Version|Download'` >> $destfile
done

-----------------------------------------------

-Create the following crontab with 'crontab -e'
 to have pacsync.sh run every 30 minutes:

 */30 * * * * /path/to/shellscript

-Conky: Add the line '${texeci 20 python path/to/this/file.py}'
 where 20 is the update interval in seconds

-Also, if part of the output is not showing in conky
 increase text_buffer_size value in conky config
 I use 'text_buffer_size 1024'

 Original authors: Michal Orlik <thror.fw@gmail.com>, sabooky <sabooky@yahoo.com>
 Original source: https://raw.github.com/Mihairu/config-files/master/scripts/pacman.py
 Edited/Mutilated by: jakhead <jakhead@gmail.com>
"""
def pkglist():
    """
    returns an alphabetized list of packages to be updated
    """
    import subprocess
    p = subprocess.Popen(['pacman', '-Qu'],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    pkgnames = []
    for i,v in enumerate(p.stdout):
        pkgnames.append(v.strip())
    return sorted(pkgnames)

def pkginfo(packages):
    """
    takes a list of package names generated by pkglist()
    and returns a list of dicts containing information
    for each
    """
    import os, subprocess
    pkgs = []
    for package in packages:
        pkg = {}
        pkg['name'] = package.split()[0]

        
        # ---- ~/.pacsync  will need to be changed below
        # ---- to reflect script location if elsewhere

        info = open(os.path.expanduser('~/.pacsync.info')).readlines()
        for i,line in enumerate(info):
            if pkg['name'] in line:
                pkg['ver'] = info[i+1].split()[2]
                pkg['dlSize'] = float(info[i+1].split()[6])/1024
        pkgs.append(pkg)
    return pkgs

def total(pkgs):
    """
    formats strings for each package
    and returns output ready for conky
    """
    # width of final output
    width = 30
    # pkg template - this is how individual pkg info is displayed ('' = disabled)
    # valid keywords - %(name)s, %(dlSize).2f, %(ver)s
    pkgTemplate = "%(name)s %(ver)s"
    # summary template - this is the summary line at the end
    summaryTemplate = "%(numpkg)d %(pkgstring)s"
    # pkg right column template - individual pkg right column
    pkgrightcolTemplate = "%(dlSize).2f MB"
    # summary right column template - summay line right column
    summaryrightcolTemplate = "%(size).2f MB"
    # seperator before summary ('' = disabled)
    block = '-' * 12
    # up to date msg
    u2d = '-system up to date-'
    # brief - one line summary
    brief = False
    # number of packages to display, 0 = all
    num_of_pkgs = 0

    summary = {}
    summary['numpkg'] = len(pkgs)
    summary['size'] = sum([x['dlSize'] for x in pkgs])
    if summary['numpkg'] == 1:
        summary['pkgstring'] = 'package'
    else:
        summary['pkgstring'] = 'packages'
    lines = []

    for pkg in pkgs:
        pkgString = pkgTemplate % pkg
        sizeValueString = pkgrightcolTemplate % pkg

        if len(pkgString)+len(sizeValueString)>width-1:
                pkgString = pkgString[:width-len(sizeValueString)-4]+'...'

        line = pkgString.ljust(width - len(sizeValueString)) + sizeValueString
        if line.strip():
            lines.append(line)

    if summary['numpkg'] == 0:
        """this is to center u2d message """
        buffer = " " * ((width - len(u2d))/2)
        print buffer + u2d + buffer
        return
    
    if not brief:
        if num_of_pkgs:
            print '\n'.join(lines[:num_of_pkgs])
        else:
            print '\n'.join(lines)
#        if block:
        print block.rjust(width)

    overallString = summaryTemplate % summary
    overallMBString = summaryrightcolTemplate % summary

    if len(overallString)+len(overallMBString)>width-1:
        overallString = overallString[:width-len(overallMBString)-4]+'...'
    summaryline = overallString.ljust(width - len(overallMBString)) + overallMBString

    if summaryline:
        print summaryline

if __name__ == '__main__':
    total(pkginfo(pkglist()))

Installation:

As the comments state, create a shell script with the following:

#!/bin/bash
#
# ~/pacsync.sh
# This syncs pacman and creates a file to store
# info for each package. This is so the python 
# portion doesn't constantly use your network
# connection every 20 seconds to get package info 
# when it refreshes itself. Better to have the
# shell script update once every several minutes
# and have python use that info instead. Don't
# forget to make executeable with chmod +x

destfile=~/.pacsync.info

sudo pacman -Sy

if [ -f $destfile ]
then
  rm $destfile
fi

list=`pacman -Qu | cut -d ' ' -f 1`
for x in $list
do
  echo $x >> $destfile
  echo `pacman -Si $x | egrep -w 'Version|Download'` >> $destfile
done

Make it executable:

chmod +x /path/to/shellscript

Then create the following crontab with 'crontab -e':

*/30 * * * * /path/to/shellscript

This will cause pacman to check for updates every 30 minutes.

Finally, place this line in your conky config:

${texeci 20 python path/to/notify.py}

Where 20 is the update interval in seconds.

Note: If conky doesn't display all of the output correctly, increase the text_buffer_size in your conky config. I use:

text_buffer_size 1024

Make sure to check out the basic settings like output width, format, or one line summary. All credit goes to Majkhii and Sabooky as they did all the heavy lifting and I merely butchered their work to suit my system.

That's it. I've been running this for 3 days with no issues so far. Questions/comments/concerns welcome. I'll try to work on this if need be, but I may have conflicts with school as I'm currently a student.

*EDIT* Fixed screenshots smile

Last edited by jakhead (2012-09-16 04:48:54)


-jakhead <jakhead@gmail.com>

Offline

#2 2012-09-16 13:19:31

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Conky - Display available pacman updates

1. No need for 'cut', we already have a pacman switch for that:

$ pacman -Qu | cut -d ' ' -f 1
wine
wine_gecko
$ pacman -Quq
wine
wine_gecko

2. You can use 'expac' to get version, size etc.
3. Doing 'pacman -Sy' is not necessarily a good idea. http://www.andreascarpino.it/blog/posts … s-part-ii/

Last edited by karol (2012-09-16 13:24:03)

Offline

#3 2012-09-16 20:04:26

jakhead
Member
Registered: 2011-11-08
Posts: 9

Re: Conky - Display available pacman updates

expac! Aha! That should make things MUCH simpler. I only put this together because I was unable to find something exactly like expac! Thanks for tips. I think I'll use the temp db method in the link you posted with expac in a shell script. I'll try and put it together tonight or tomorrow. Thanks again!


-jakhead <jakhead@gmail.com>

Offline

Board footer

Powered by FluxBB