"""Some functions  associated with  manipulating the rpm  database and
inferences about required rpm's from the athena rpm infrastructure."""

# Given  the variety of  error messages  rpm is  likely to  give, this
# approach is highly error-prone.


import os, string, sys

def remove_rpm(rpm_name):

    """Remove the given rpm and everything that depends on it."""

    to_remove = [rpm_name]

    # Keep track of how many  times we've iterated the rpm command and
    # failed, so that we don't get into an infinite loop if the rpm -e
    # command fails for unanticipated reasons.
    repeat_count = 0
    while 1:
        rmv_proc = os.popen('rpm -e %s 2>&1' % string.join(to_remove))
        results = rmv_proc.readlines()
        if not rmv_proc.close(): break
        repeat_count = repeat_count + 1
        if repeat_count >= 10:
            sys.stderr.write("""
            rpm -e command kept failing.  Last output was

            %s
            """ % results)
            raise SystemError
        assert rmv_proc[0] == \
               "error: removing these packages would break " \
               "dependencies:\n"
        for line in rmv_proc[1:]:
            assert string.find(line, ' is needed by ') != -1
            pkg_name = string.split(line)[-1]
            to_remove.append(pkg_name)

def latest_athena_rpm(prefix):

    """Get the latest version of the athena rpm whose name starts with
    athena-<prefix>"""

    
