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