Slackware – check installed packages with Python

Querying installed package is something I do every now and then. It’s usually needed to find out if all the necessary dependencies of a package have been met. On Slackware it is usually accomplished with a simple:

ls /var/log/packages | grep -i package

For example:

$ ls /var/log/packages | grep -i gconf

As a way of practising programming, I decided to simulate the above command in Python 3. Here’s what I’ve coded:

#!/usr/bin/env python3

import glob, os, sys

if len(sys.argv) != 2:
        '''Check if the user provided the required argument'''
        print('The {0} command takes exactly 1 argument!'.format(sys.argv[0]))
        packages_directory = '/var/log/packages/'
        package = sys.argv[1].lower()
        for files in glob.glob(packages_directory + '*' + package + '*'):

It might be slightly slower than using Bash but it does the job. As I’m not a seasoned Python programmer, there’ll probably be ways of optimising the code, which I obviously welcome. Feel free to provide feedback.

4 Responses to Slackware – check installed packages with Python

  1. Hi, just wanted to note, that parentheses around the condition of if-statement aren’t necessary in Python and are even considered a bad style.

    Also, what you’re doing with



    fnmatch.fnmatch()<code> could be done using the functions from <code>glob

    module (in fact, they’re implemented in a similar way). That won’t make your code run faster, but it’ll sure be smaller and simpler.

  2. admin

    Thanks for your feedback, Audrius.

    I have adjusted the code. It is more concise now. As I said, I am not an experienced Python programmer so don’t hesitate to leave more comments.


  3. Antonio Hernández Blas

    Hi Marcin, some time ago i wrote an script, sbo_inst [1], that lets you get a list or perfome a search of certain packages. For instance:

    $ sbo_inst torrent // searches for any packages with the string 'gconf' in it's name
    2013-11-27 10:29:01 rtorrent-0.9.3-x86_64-1_hba
    2013-11-27 10:18:44 libtorrent-0.13.3-x86_64-1_hba
    $ sbo_inst | head // list the last 10 packages installed/upgraded
    2013-11-27  10:29:01  rtorrent-0.9.3-x86_64-1_hba
    2013-11-27  10:18:44  libtorrent-0.13.3-x86_64-1_hba
    2013-11-27  10:14:22  ghc-7.6.3-x86_64-1_hba
    2013-11-26  22:14:30  sbcl-1.1.13-x86_64-1_hba
    2013-11-26  21:02:27  racket-5.3.6-x86_64-1_hba
    2013-11-26  02:16:26  cmus-20131125-x86_64-1_hba
    2013-11-25  19:56:07  vim-7.4.103-x86_64-1_hba
    2013-11-25  11:51:02  vcdimager-0.7.24-x86_64-1_hba
    2013-11-25  11:50:59  libshout-2.3.1-x86_64-1_hba
    2013-11-23  17:22:30  k3b-2.0.2_20120226.git-x86_64-1

    – Cheers


  4. Antonio Hernández Blas

    Opps sorry, it should be “// searches for any packages with the string ‘torrent’ in it’s name” 😉

Leave a Reply

Your email address will not be published. Required fields are marked *