userland-incorporator revision 3294
3294N/A#!/usr/bin/python2.7
500N/A#
500N/A# CDDL HEADER START
500N/A#
500N/A# The contents of this file are subject to the terms of the
500N/A# Common Development and Distribution License (the "License").
500N/A# You may not use this file except in compliance with the License.
500N/A#
500N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
500N/A# or http://www.opensolaris.org/os/licensing.
500N/A# See the License for the specific language governing permissions
500N/A# and limitations under the License.
500N/A#
500N/A# When distributing Covered Code, include this CDDL HEADER in each
500N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
500N/A# If applicable, add the following below this CDDL HEADER, with the
500N/A# fields enclosed by brackets "[]" replaced with your own identifying
500N/A# information: Portions Copyright [yyyy] [name of copyright owner]
500N/A#
500N/A# CDDL HEADER END
500N/A#
3294N/A# Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
500N/A#
500N/A#
500N/A# incorporator - an utility to incorporate packages in a repo
500N/A#
500N/A
3294N/Aimport subprocess
3294N/Aimport json
3294N/Aimport sys
3294N/Aimport getopt
3294N/Aimport re
3294N/Aimport os.path
500N/A
3294N/AWerror = False # set to true to exit with any warning
500N/A
3294N/Adef warning(msg):
3294N/A if Werror == True:
3294N/A print >>sys.stderr, "ERROR: %s" % msg
3294N/A sys.exit(1)
3294N/A else:
3294N/A print >>sys.stderr, "WARNING: %s" % msg
500N/A
3294N/Aclass Incorporation(object):
3294N/A name = None
3294N/A version = '5.12'
3294N/A packages = {}
3294N/A
3294N/A def __init__(self, name, version):
3294N/A self.name = name
3294N/A self.version = version
3294N/A self.packages = {}
500N/A
3294N/A def __package_to_str(self, name, version):
3294N/A # strip the :timestamp from the version string
3294N/A version = version.split(':', 1)[0]
3294N/A # strip the ,{build-release} from the version string
3294N/A version = re.sub(",[\d\.]+", "", version)
500N/A
3294N/A return "depend fmri=%s@%s facet.version-lock.%s=true type=incorporate" % (name, version, name)
500N/A
3294N/A def add_package(self, name, version):
3294N/A self.packages[name] = version
500N/A
3294N/A def __str__(self):
3294N/A result = """
3294N/Aset name=pkg.fmri value=pkg:/%s@%s
3294N/Aset name=info.classification value="org.opensolaris.category.2008:Meta Packages/Incorporations"
3294N/Aset name=org.opensolaris.consolidation value=userland
3294N/Aset name=pkg.depend.install-hold value=core-os.userland
3294N/Aset name=pkg.summary value="userland consolidation incorporation (%s)"
3294N/Aset name=pkg.description value="This incorporation constrains packages from the userland consolidation"
3294N/A""" % (self.name, self.version, self.name)
500N/A
3294N/A names = self.packages.keys()
3294N/A names.sort()
3294N/A for name in names:
3294N/A result += (self.__package_to_str(name, self.packages[name]) + '\n')
500N/A
3294N/A return result
500N/A
500N/A#
3294N/A# This should probably use the pkg APIs at some point, but this appears to be
3294N/A# a stable and less complicated interface to gathering information from the
3294N/A# manifests in the package repo.
500N/A#
3294N/Adef get_incorporations(repository, publisher, inc_version='5.12'):
3294N/A tmp = subprocess.Popen(["/usr/bin/pkgrepo", "list", "-F", "json",
3294N/A "-s", repository,
3294N/A "-p", publisher],
3294N/A stdout=subprocess.PIPE)
3294N/A incorporations = {}
3294N/A packages = json.load(tmp.stdout)
3294N/A
3294N/A # Check for multiple versions of packages in the repo, but keep track of
3294N/A # the latest one.
3294N/A versions = {}
3294N/A for package in packages:
3294N/A pkg_name = package['name']
3294N/A pkg_version = package['version']
3294N/A
3294N/A if pkg_name in versions:
3294N/A warning("%s is in the repo at multiple versions (%s, %s)" % (pkg_name, pkg_version, versions[pkg_name]))
3294N/A pkg_version = max(pkg_version, versions[pkg_name])
3294N/A versions[pkg_name] = pkg_version
3294N/A
3294N/A for package in packages:
3294N/A pkg_name = package['name']
3294N/A pkg_version = package['version']
3294N/A
3294N/A # skip older packages and those that don't want to be incorporated
3294N/A if 'pkg.tmp.incorporate' not in package or pkg_version != versions[pkg_name]:
3294N/A continue
3294N/A
3294N/A # a dict inside a list inside a dict
3294N/A incorporate = package['pkg.tmp.incorporate'][0]['value']
3294N/A
3294N/A for inc_name in incorporate:
3294N/A # if we haven't started to build this incorporation, create one.
3294N/A if inc_name not in incorporations:
3294N/A incorporations[inc_name] = Incorporation(inc_name, inc_version)
3294N/A # find the incorporation and add the package
3294N/A tmp = incorporations[inc_name]
3294N/A tmp.add_package(pkg_name, pkg_version)
3294N/A return incorporations
500N/A
3294N/Adef main_func():
3294N/A global Werror
3294N/A
3294N/A try:
3294N/A opts, pargs = getopt.getopt(sys.argv[1:], "c:s:p:v:d:w",
3294N/A ["repository=", "publisher=", "version=",
3294N/A "consolidation=", "destdir=", "Werror"])
3294N/A except getopt.GetoptError, e:
3294N/A usage(_("illegal option: %s") % e.opt)
3294N/A
3294N/A repository = None
3294N/A publisher = None
3294N/A version = None
3294N/A destdir = None
3294N/A consolidation = None
2213N/A
3294N/A for opt, arg in opts:
3294N/A if opt in ("-s", "--repository"):
3294N/A repository = arg
3294N/A elif opt in ("-p", "--publisher"):
3294N/A publisher = arg
3294N/A elif opt in ("-v", "--version"):
3294N/A version = arg
3294N/A elif opt in ("-d", "--destdir"):
3294N/A destdir = arg
3294N/A elif opt in ("-c", "--consolidation"):
3294N/A consolidation = arg
3294N/A elif opt in ("-w", "--Werror"):
3294N/A Werror = True
3294N/A
3294N/A incorporations = get_incorporations(repository, publisher, version)
3294N/A
3294N/A for incorporation_name in incorporations.keys():
3294N/A filename = ''
3294N/A if destdir != None:
3294N/A filename = destdir + '/'
3294N/A filename += os.path.basename(incorporation_name) + '.p5m'
3294N/A
3294N/A print("Writing %s manifest to %s" % (incorporation_name, filename))
3294N/A fd = open(filename, "w+")
3294N/A fd.write(str(incorporations[incorporation_name]))
3294N/A fd.close()
3294N/A
3294N/Aif __name__ == "__main__":
3294N/A main_func()