#!/usr/bin/python
#
# pull-lp-source -- pull a source package from Launchpad
# Basic usage: pull-lp-source <source package> [<release>]
#
# Copyright (C) 2008,      Iain Lane <iain@orangesquash.org.uk>,
#               2010-2011, Stefano Rivera <stefanor@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; either version 3
# of the License, or (at your option) any later version.
#
# 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.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################


import os
import sys
from optparse import OptionParser

from devscripts.logger import Logger
from distro_info import UbuntuDistroInfo

from ubuntutools.archive import UbuntuSourcePackage, DownloadError
from ubuntutools.config import UDTConfig
from ubuntutools.lp.lpapicache import Distribution, Launchpad
from ubuntutools.lp.udtexceptions import (SeriesNotFoundException,
                                          PackageNotFoundException,
                                          PocketDoesNotExistError)
from ubuntutools.misc import split_release_pocket

def main():
    usage = "Usage: %prog <package> [release|version]"
    opt_parser = OptionParser(usage)
    opt_parser.add_option('-d', '--download-only',
                          dest='download_only', default=False,
                          action='store_true',
                          help="Do not extract the source package")
    opt_parser.add_option('-m', '--mirror', metavar='UBUNTU_MIRROR',
                          dest='ubuntu_mirror',
                          help='Preferred Ubuntu mirror (default: Launchpad)')
    opt_parser.add_option('--no-conf',
                          dest='no_conf', default=False, action='store_true',
                          help="Don't read config files or environment "
                               "variables")
    (options, args) = opt_parser.parse_args()
    if not args:
        opt_parser.error("Must specify package name")

    config = UDTConfig(options.no_conf)
    if options.ubuntu_mirror is None:
        options.ubuntu_mirror = config.get_value('UBUNTU_MIRROR')

    # Login anonymously to LP
    Launchpad.login_anonymously()

    package = str(args[0]).lower()

    ubuntu_info = UbuntuDistroInfo()
    if len(args) > 1: # Custom distribution specified.
        version = str(args[1])
    else:
        version = os.getenv('DIST') or ubuntu_info.devel()
    component = None

    # Release, not package version number:
    release = None
    pocket = None
    try:
        (release, pocket) = split_release_pocket(version)
    except PocketDoesNotExistError, e:
        pass
    if release in ubuntu_info.all and pocket is not None:
        try:
            spph = Distribution('ubuntu').getArchive().getSourcePackage(package,
                                                                        release,
                                                                        pocket)
        except (SeriesNotFoundException, PackageNotFoundException), e:
            Logger.error(str(e))
            sys.exit(1)
        version = spph.getVersion()
        component = spph.getComponent()

    Logger.normal('Downloading %s version %s', package, version)
    srcpkg = UbuntuSourcePackage(package, version, component=component,
                                 mirrors=[options.ubuntu_mirror])
    try:
        srcpkg.pull()
    except DownloadError, e:
        Logger.error('Failed to download: %s', str(e))
        sys.exit(1)
    if not options.download_only:
        srcpkg.unpack()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        Logger.normal('User abort.')