#!/usr/bin/python
#
# Copyright 2002-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2 or later

"""equery is a flexible utility for Gentoo linux which can display various
information about packages, such as the files they own, their USE flags,
the MD5 sum of each file owned by a given package, and many other things.
"""

import os
import sys
# This block ensures that ^C interrupts are handled quietly.
try:
    import signal

    def exithandler(signum, frame):
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        print()
        sys.exit(1)

except KeyboardInterrupt:
    print()
    sys.exit(1)

from gentoolkit import equery, errors
import warnings


def formatwarning(message, category, filename, llineno, line=None):
    from gentoolkit import pprinter as pp
    return pp.warn(str(message))


if __name__ == "__main__":
    signal.signal(signal.SIGINT, exithandler)
    signal.signal(signal.SIGTERM, exithandler)
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    if '--debug' not in sys.argv and not bool(os.getenv('DEBUG', False)):
        warnings.formatwarning = formatwarning

    try:
        equery.main(sys.argv)
    except errors.GentoolkitNonZeroExit as err:
        sys.exit(err.return_code)
    except errors.GentoolkitException as err:
        if '--debug' in sys.argv or bool(os.getenv('DEBUG', False)):
            raise
        else:
            from gentoolkit import pprinter as pp
            sys.stderr.write(pp.error(str(err)))
            if err.is_serious:
                print()
                print("Add '--debug' to global options for traceback.")
            sys.exit(1)
