Skip to content
Snippets Groups Projects
mpcdf_push.py 4.21 KiB
#!/usr/bin/python2
from __future__ import print_function
from six.moves import input

import time
import mpcdf_common

import os
import osc
import osc.conf
import osc.core
import osc.cmdln

from xml.etree import ElementTree


@osc.cmdln.option('-b', "--batch", action="store_true",
                  help="do not ask for confirmation")
@osc.cmdln.option('-m', "--message", nargs=1,
                  help="message to include in the submit request")
def do_mpcdf_push(self, subcmd, opts, *args):
    """${cmd_name}: Push packages from a home project to a general project

    You would typically use this to push your home project package into the
    "software" project.

    If the package does not yet exist on the target project, it is created
    there and your source project is connected to that as a branch (by using
    the osc mpcdf_set_as_branch command)

    Usage:
        osc mpcdf_push TO_PROJECT           (from within a package checkout)
        osc mpcdf_push PACKAGE TO_PROJECT   (from within a project checkout)
        osc mpcdf_push FROM_PROJECT PACKAGE TO_PROJECT            (anywhere)

    Examples:
       - Add a new package from your home:project to software:
         #> osc mpcdf_push home:$USER newpkg_1_2 software

    ${cmd_option_list}
    """
    if len(args) < 1:
        raise osc.oscerr.WrongArgs("Not enough arguments")
    elif len(args) == 1:
        to_project, = args
        if osc.core.is_package_dir(os.curdir):
            package = osc.core.store_read_package(os.curdir)
            from_project = osc.core.store_read_project(os.curdir)
        else:
            raise osc.oscerr.WrongArgs('Specify FROM_PROJECT and/or PACKAGE or run command in a package or project checkout directory')
    elif len(args) == 2:
        package, to_project = args
        if osc.core.is_project_dir(os.curdir):
            from_project = osc.core.store_read_project(os.curdir)
        else:
            raise osc.oscerr.WrongArgs('Specify FROM_PROJECT or run command in an osc package or project checkout directory')
    elif len(args) == 3:
        from_project, package, to_project = args
    else:
        raise osc.oscerr.WrongArgs("Too many arguments")

    api_url = self.get_api_url()

    project_attributes = ["MPCDF:compiler_modules", "MPCDF:cuda_modules", "MPCDF:mpi_modules"]
    package_attributes = ["MPCDF:enable_repositories"] + project_attributes

    req_id = osc.core.create_submit_request(api_url, from_project, package, dst_project=to_project,
                                            message="Update {0} from {1}".format(package, from_project) if not opts.message else opts.message)
    print("Submitted package {0} to {1} in request {2}".format(package, to_project, req_id))
    if not opts.batch:
        print(" PLEASE review the request by 'osc request show {0} --diff', or in the web interface".format(req_id))
        print(" Do you want to accept the request right away? [y/N] ", end="")
        response = input().lower()
        if response == "y":
            result = osc.core.change_request_state(api_url, req_id, 'accepted', "Accepted on the command line via 'osc mpcdf_push'")
            print(" Accepting request {0}:".format(req_id), result)

    # Give the system some time, sadly there is no transactional guarantee
    time.sleep(2)

    url = osc.core.makeurl(api_url, ["source", to_project])
    entries = ElementTree.fromstringlist(osc.core.streamfile(url, osc.core.http_GET))

    if entries.find('./entry[@name="{0}"]'.format(package)) is not None:
        for attribute in package_attributes:
            if mpcdf_common.has_attribute(api_url, from_project, package, attribute):
                print("Setting attribute", attribute)
                attr = mpcdf_common.get_attribute(api_url, from_project, package, attribute)
                mpcdf_common.set_attribute(api_url, to_project, package, attr)

        if not mpcdf_common.mpcdf_enable_repositories(api_url, to_project, package):
            print("ATTENTION: Not changing unmanaged package {0}".format(package))

        filelist = osc.core.meta_get_filelist(api_url, from_project, package)
        if "_link" not in filelist:
            print("Setting branch relationship")
            mpcdf_common.set_as_branch(api_url, from_project, package, to_project, package)