You are not logged in.

#1 2005-06-24 14:30:36

sweiss
Member
Registered: 2004-02-16
Posts: 635

a python implementation of pacman -Rs for groups

I've created a simple python script which implements the pacman -Rs feature for package groups.

Here's the code:

#!/usr/bin/python

# Copyright (C) 2005 Shahar Weiss <sweiss4@gmx.net>
#
#   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 2 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#

# This script is a wrap for pacman which allows for removing package groups using the -Rs flag.
# It works by checking which packages of the group were explicitly installed and makes sure these packages are not
# needed by any other out-of-group packages. It then uses pacman and it's -Rs feature to remove the remaining packages
# in the list.

# Version 0.0.4

import os, sys

def returnIndex(_list, _string):
    """Returns the index in _list which contains _string, returns -1 is _string was not found"""
    for i in range(len(_list)):
        if _list[i].find(_string)!=-1:
            return i
            
    return -1

def removeRequiredForOtherPackages(packageList):
    """Returns a list of all the packages which are not required for other packages meant for removal."""
    tempList = packageList[:]
    for i in range(len(packageList)):
        Output = os.popen('pacman -Qi ' + packageList[i]).readlines()
        
        startIndex = returnIndex(Output, 'Required By')
        stopIndex = returnIndex(Output, 'Conflicts With')
        
        requiredList = Output[startIndex:stopIndex]
        
        tempReqList = requiredList[0].split(' ')
        for j in range(1,len(requiredList)):
            tempReqList.extend( requiredList[j].split(' '))
            
        requiredList = tempReqList[:]
        
        # Cleans requiredList from unneeded strings
        removeAllStrings(requiredList, '')
        removeAllStrings(requiredList, 'n')
        requiredList = requiredList[3:]
        
        if requiredList==['Nonen']:
            continue
        else:
            for j in range(len(requiredList)):
                # if there are packages who require the current package, check if they are enlisted for removal.
                # if not, then remove the current package from the list
                if requiredList[j] not in tempList:
                    tempList.remove( packageList[i] )
                    break                  
        
    return tempList

def removeNonExplicit(packageList):
    """Returns a list of all the packages which were explicitly installed."""
    # Creating a temporary list because removing an element from the original list instead
    # might cause us going out of bounds in the loop.
    tempList = packageList[:]
    for i in range(len(packageList)):
        packageInfoString = os.popen('pacman -Qi ' + packageList[i]).readlines()[11]
        
        # if the package was not explicitly installed, remove it from the list
        if packageInfoString.find('explicitly installed')==-1:
            tempList.remove(packageList[i])
    
    return tempList

def removeAllStrings(_list, _string):
    """Removes all instances of given _string from _list"""
    while _string in _list:
        _list.remove(_string)

def getGroupPackages(group_name):
    """Returns a list of the packages contained by a certain group"""
    tempList = os.popen('pacman -Qg ' + group_name + ' 2>/dev/null').readlines()
    
    # if there's no such group, quit.
    if tempList==['n']:
        print "No such group exists."
        sys.exit(1)
       
    packageList = []
    for i in range(len(tempList)):
        # Each element in tempList contains a string in the form of 'group packagen'.
        # The parsing we do adds only the 'package' part to packageList.
        packageList.append( tempList[i].split(' ')[1].split('n')[0] )
    
    return packageList
    
def main(args):
    if os.getuid()!=0:
        print "This program should be run as root."
        sys.exit(1)
    
    if len(args)==1:
        print "You did not provide a group for removal."
        sys.exit(1)
    
    packagesToRemove = getGroupPackages(args[-1])
    packagesToRemove = removeNonExplicit(packagesToRemove)
    packagesToRemove = removeRequiredForOtherPackages(packagesToRemove)
    
    if packagesToRemove==[]:
        print "No package was marked for removal."
        sys.exit(1)
    
    packageRemoveString=''
    for i in range(len(packagesToRemove)):
        packageRemoveString += packagesToRemove[i] + ' '
        
    os.system('pacman -Rs ' + packageRemoveString)
    
if __name__=="__main__":
    main(sys.argv)

Hope you like it and that it works well.
Enjoy.

Offline

#2 2005-06-24 14:45:22

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: a python implementation of pacman -Rs for groups

If you like, you could put this in the scripts section of the wiki:

http://wiki2.archlinux.org/index.php/Scripts

Dusty

Offline

#3 2005-06-24 15:07:43

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: a python implementation of pacman -Rs for groups

Will do. I've just added some error handling lines, gotta love rapid development.

EDIT: Done. Never used a wiki before, very nice system.

Offline

#4 2005-06-24 15:13:15

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: a python implementation of pacman -Rs for groups

sweiss wrote:

gotta love rapid development.

You mean 'gotta love Python' :-)

Dusty

Offline

#5 2005-06-24 15:25:16

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: a python implementation of pacman -Rs for groups

Synonyms, go figure wink

Offline

#6 2005-06-24 18:20:28

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: a python implementation of pacman -Rs for groups

You might check out xerxes' libpypac. It does most of the pacman functions in python. Also, libpysrc does most of the makepkg functionality in python.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#7 2005-06-24 21:04:19

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: a python implementation of pacman -Rs for groups

Well I only lacked this feature in pacman so far, and I get a good python practice on the way.

I'll give libpypac a look though, thank you.

Offline

#8 2005-07-18 07:26:17

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: a python implementation of pacman -Rs for groups

Changed the code a bit so that no segfault of pacman is triggered during the process.

Offline

Board footer

Powered by FluxBB