vlmaddon / utils /

#!/usr/bin/env python

#    This file is part of the vectorlinux multimedia bonus disc.
#    This file is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v2 as published by
#    the Free Software Foundation.
#    This file is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this file.  If not, see <>.

import os
import subprocess as sp

__author__ = 'Moises Henriquez'
__author_email__ = 'moc.liamg@xnl.E0M'[::-1]

def check_root():
    return os.getuid() is 0

def _get_popen(cmd):
    proc = sp.Popen(
            stdout = sp.PIPE,
            stderr = sp.STDOUT,
    return proc

def get_vl_version():
    """ Find the version of the running vectorlinux install
    WARNING: This result is based on the naming scheme of the
    vlconfig2 package"""
    for item in os.listdir('/var/log/packages'):
        (app, ver, arch, build) = item.rsplit('-', 3)
        if app == 'vlconfig2':
            return build[-2:]
    return None

def find_latest(pkg1, pkg2):
    Compares 2 package's build strings and find the version that is
    more suitable for the running installation

    handy when comparing 2 packages with the exact same name and 
    version, but different build numbers
    if 'vl' in pkg1 and not 'vl' in pkg2:
        return pkg1
    if 'vl' in pkg2 and not 'vl' in pkg1:
        return pkg2
    # Check the vl version, and always prefer the one that
    # matches the vl version
    if 'vl' in pkg1 and 'vl' in pkg2:
        vlver = get_vl_version().replace('.','')
        if pkg1.endswith(vlver) and not pkg2.endswith(vlver):
            return pkg1
        elif pkg2.endswith(vlver) and not pkg1.endswith(vlver):
            return pkg2
    return max(pkg1, pkg2)

class PackageManager(object):
    Package manager functions tied to VL"""
    manager = '/usr/sbin/slapt-get'

    def __init__(self, configfile = '/etc/slapt-get/slapt-getrc'):
        self.cache = PackageCache()
        if configfile is None:
            self.configfile = '/etc/slapt-get/slapt-getrc'
            self.configfile = configfile

    def update_cache(self):
        proc = _get_popen(
                "%s -c %s -y -u"% (self.manager,
        out = proc.communicate()
        return out

    def install_package(self, pkgname):
        proc = _get_popen(
                "%s -c %s -y -i %s" (self.manager,
        out = proc.communicate()
        return out

    def remove_package(self, package = str, rdeps = False):
        if rdeps is True:
            # Get list of deps and remove them one by one
            raise NotImplemented
            proc = _get_popen("removepkg %s"% package)
            out = proc.communicate()
            return out

    def restore_package(self, pkgname):
        Restore a package back to its working condition.
        This means forcing the version or release provided with
        the bonus disc over ANYTHING else
        if self.cache.is_app_installed(pkgname):
            # If package is installed, remove it first
            r = self.remove_package(package = pkgname)
        # Now install the package again.
        return self.install_package(pkgname)

class PackageCache(object):
    """ Cache of currently installed packages """
    def __init__(self, cachedir='/var/log/packages'):
        self.installed = os.listdir(cachedir)

    def search_installed(self, term):
        Searches the local cache for a package
                term - A package name
                        ie, 'foo-bar-1.2-i586-1vl70'
                True if a package is installed
                Term must be a full package name, not
                just the application name
        return tem in self.installed
    def is_app_installed(self, pkg):
        Search the cache for an application
        Returns true when the application is found. Result
        does not match version or release number, just the
        application name. Requires an app name as argument"""
        for item in self.installed:
            (app, ver, arch, build) = item.rsplit('-', 3)
            if app == pkg:
                return True
        return False

    def get_installed_version(self, pkg):
        """ Find the version of an installed package
        required an application name as argument and returns
        a string representing the version found in the running
        system. Returns None is app is not found"""
        for item in self.installed:
            (app, ver, arch, build) = item.rsplit('-', 3)
            if app == pkg:
                return ver
        return None