#!/usr/bin/python
#
# ./pyrpmyum -r /home/n -c /etc/nix -y --test --fileconflicts install /var/www/html/mirror/fedora/development/i386/Fedora/RPMS
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library 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.
# Copyright 2004, 2005 Red Hat, Inc.
#
# Author: Phil Knirsch
#

import sys, os.path

PYRPMDIR = "/usr/share/pyrpm"
if not PYRPMDIR in sys.path:
    sys.path.append(PYRPMDIR)
from pyrpm import __version__
from pyrpm import *
from pyrpm.yum import *
from pyrpm.logger import log

def usage():
    print """
INSTALLING, UPGRADING, AND REMOVING PACKAGES using REPOS:
    pyrpmyum [options] install      PACKAGE_FILE ...
    pyrpmyum [options] groupinstall PACKAGE_FILE ...
    pyrpmyum [options] upgrade      PACKAGE_FILE ...
    pyrpmyum [options] groupupgrade PACKAGE_FILE ...
    pyrpmyum [options] update       PACKAGE_FILE ...
    pyrpmyum [options] groupupdate  PACKAGE_FILE ...
    pyrpmyum [options] remove       PACKAGE_FILE ...
    pyrpmyum [options] groupremove  PACKAGE_FILE ...
    pyrpmyum [options] list [all|available|extras|installed|
                             obsoletes|recent|updates] [PACKAGE_GLOB] ...

options:
    [-?, --help] [--version]
    [--quiet] [-v, --verbose] [-q] [-y]
    [-c CONFIGFILE] [--dbpath DIRECTORY]
    [-r, --root DIRECTORY, --installroot DIRECTORY]
    [-h, --hash] [--force] [--oldpackage] [--justdb] [--test]
    [--ignoresize] [--ignorearch] [--exactarch]
    [--noconflicts] [--fileconflicts]
    [--nodeps] [--signature]
    [--noorder] [--noscripts] [--notriggers]
    [--autoerase] [--installpkgs="pkg1 pkg2 pkg2 ..."]
    [--enablerepo repoid|repoglob] [--disablerepo repoid|repoglob]
    [--exclude pkgname/pkgglob]
    [--nocache] [--cachedir DIRECTORY]
    [--obsoletes] [--noplugins] [--releaseversion]
"""

#
# Main program
#
def main():
    if isSelinuxRunning() and not rpmconfig.selinux_enabled:
        log.warning("SELinux support disabled because python interface not available. Please run fixfiles manually after operation is complete.")

    log.setInfoLogLevel(log.INFO2)

    # Our yum worker object
    yum = RpmYum(rpmconfig)

    # Disabled fileconflicts per default in yum
    rpmconfig.nofileconflicts = 1

    # Argument parsing
    args = parseYumOptions(sys.argv[1:], yum)

    if not args:
        usage()
        return 1

    if not yum.lock():
        log.error("couldn't lock pyrpmyum")
        return 0

    try:
        if not yum.setCommand(args[0]):
            return 0
        if not yum.prepareTransaction():
            return 0
        if not yum.runArgs(args[1:]):
            return 0
        if not yum.runDepRes():
            return 0
        if not yum.runCommand(clearrepos=True):
            return 0
    finally:
        if yum.pydb:
            yum.pydb.close()
        if not yum.unLock():
            log.error("couldn't unlock pyrpmyum")

    return 1

if __name__ == '__main__':
    if not run_main(main):
        sys.exit(1)

# vim:ts=4:sw=4:showmatch:expandtab
