#! /usr/bin/python

# Copyright 2012 Canonical Ltd.
# Author: Colin Watson <cjwatson@ubuntu.com>

# 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; version 3 of the License.
#
# 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, see <http://www.gnu.org/licenses/>.

"""Remove a package from the archive."""

from __future__ import print_function

from optparse import OptionParser, SUPPRESS_HELP
import sys

from launchpadlib.launchpad import Launchpad
try:
    from ubuntutools.question import YesNoQuestion
except ImportError:
    print("Could not find ubuntutools.question; run sudo apt-get install "
          "python-ubuntutools")
    sys.exit()

import lputils


def find_removables(options, package):
    if options.binaryonly:
        for binary in lputils.find_latest_published_binaries(options, package):
            if not binary.is_debug:
                yield binary, True
    else:
        source = lputils.find_latest_published_source(options, package)
        yield source, True
        for binary in source.getPublishedBinaries():
            if not binary.is_debug:
                yield binary, False


def find_all_removables(options, packages):
    for package in packages:
        try:
            for removable in find_removables(options, package):
                yield removable
        except lputils.PackageMissing as message:
            print(message)
            if options.skip_missing:
                print("Skipping")
            else:
                print("Exiting")
                sys.exit(1)


def remove_package(options, packages):
    removables = []

    print("Removing packages from %s:" % options.suite)
    for removable, direct in find_all_removables(options, packages):
        removables.append((removable, direct))
        print("\t%s%s" % ("" if direct else "\t", removable.display_name))
    print("Comment: %s" % options.removal_comment)

    if options.dry_run:
        print("Dry run; no packages removed.")
    else:
        if not options.confirm_all:
            if YesNoQuestion().ask("Remove", "no") == "no":
                return

        removals = []
        for removable, direct in removables:
            if direct:
                removable.requestDeletion(
                    removal_comment=options.removal_comment)
                removals.append(removable)

        print("%d %s successfully removed." %
              (len(removals), "package" if len(removals) == 1 else "packages"))


def main():
    parser = OptionParser(
        usage='usage: %prog -m "comment" [options] package [...]',
        epilog=lputils.ARCHIVE_REFERENCE_DESCRIPTION)
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-n", "--dry-run", default=False, action="store_true",
        help="only show removals that would be performed")
    parser.add_option(
        "-y", "--confirm-all", default=False, action="store_true",
        help="do not ask for confirmation")
    parser.add_option("-A", "--archive", help="remove from ARCHIVE")
    parser.add_option(
        "-s", "--suite", metavar="SUITE", help="remove from SUITE")
    parser.add_option(
        "-a", "--architecture", dest="architectures", action="append",
        metavar="ARCHITECTURE",
        help="architecture tag (may be given multiple times)")
    parser.add_option(
        "-e", "--version",
        metavar="VERSION", help="package version (default: current version)")
    parser.add_option(
        "-b", "--binary", dest="binaryonly",
        default=False, action="store_true", help="remove binaries only")
    parser.add_option("-m", "--removal-comment", help="removal comment")
    parser.add_option(
        "--skip-missing", default=False, action="store_true",
        help=(
            "When a package cannot be removed, normally this script exits "
            "with a non-zero status. With --skip-missing instead, the "
            "error is printed and removing continues"))

    # Deprecated in favour of -A.
    parser.add_option(
        "-d", "--distribution", default="ubuntu", help=SUPPRESS_HELP)
    parser.add_option("-p", "--ppa", help=SUPPRESS_HELP)
    parser.add_option("--ppa-name", help=SUPPRESS_HELP)
    parser.add_option(
        "-j", "--partner", default=False, action="store_true",
        help=SUPPRESS_HELP)

    options, args = parser.parse_args()

    options.launchpad = Launchpad.login_with(
        "remove-package", options.launchpad_instance, version="devel")
    lputils.setup_location(options)

    if options.removal_comment is None:
        parser.error(
            "You must provide a comment/reason for all package removals.")

    remove_package(options, args)


if __name__ == '__main__':
    main()
