diff --git a/mpcdf_push.py b/mpcdf_push.py index 91b56c2469da725609178aa46a50679a39fa93fe..ae042e72fd638be7af864d6479019e67953a5a4c 100644 --- a/mpcdf_push.py +++ b/mpcdf_push.py @@ -1,34 +1,59 @@ #!/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}: Branch all packages from one project to another + """${cmd_name}: Push packages from a home project to a general project - All packages from FROM_PROJECT (that do not yet exist in the target - project) are branched into TO_PROJECT. + You would typically use this to push your home project package into the + "software" project. - Packages that already exist are not branched/modified but the repository - configuration is updated to match the attribute settings from FROM_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 FROM_PROJECT TO_PROJECT - osc mpcdf_push FROM_PROJECT PACKAGE TO_PROJECT + 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) < 2: + 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: - from_project, to_project = args - package = None + 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: @@ -36,53 +61,37 @@ def do_mpcdf_push(self, subcmd, opts, *args): api_url = self.get_api_url() - if package is None: - from_packages = osc.core.meta_get_packagelist(api_url, from_project) - else: - from_packages = [package] - to_packages = osc.core.meta_get_packagelist(api_url, to_project) - project_attributes = ["MPCDF:compiler_modules", "MPCDF:cuda_modules", "MPCDF:mpi_modules"] package_attributes = ["MPCDF:enable_repositories"] + project_attributes - for attribute in project_attributes: - try: - mpcdf_common.get_attribute(api_url, to_project, None, attribute) - except Exception: - attr = mpcdf_common.get_attribute(api_url, from_project, None, attribute) - print("Setting attribute", attribute) - mpcdf_common.set_attribute(api_url, (to_project,), attr) - - for orig_package in from_packages: - if orig_package not in to_packages: - filelist = osc.core.meta_get_filelist(api_url, from_project, orig_package) - if "_link" in filelist: - print("Not branching package {0}, is a link".format(orig_package)) - else: - print("Branching package {0}".format(orig_package)) - exists, targetprj, targetpkg, srcprj, srcpkg = \ - osc.core.branch_pkg(api_url, from_project, orig_package, target_project=to_project, nodevelproject=True) - else: - print("Not branching package {0}, already present in target".format(orig_package)) + 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: - try: - attr = mpcdf_common.get_attribute(api_url, from_project, orig_package, attribute) - except Exception: - continue - mpcdf_common.set_attribute(api_url, (to_project, orig_package), attr) - - if package is None: - # Check if distribution is already set in to_project - to_prj_meta = mpcdf_common.project_meta(api_url, to_project) - sys_repo = to_prj_meta.find('./repository[@name="System"]') - if sys_repo is None: - distribution = mpcdf_common.project_meta(api_url, from_project).find('./repository[@name="System"]/path[@project="distributions"]').get("repository") - else: - distribution = sys_repo.find('./path[@project="distributions"]').get("repository") + 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) - print("Creating repository configuration") - mpcdf_common.mpcdf_setup_repos(api_url, to_project, distribution) - else: 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)