python36: add libressl patch
[unleashed-userland.git] / tools / userland-incorporator
blobc37851fdeb24495a03ed6223245f40314839b6eb
1 #!/usr/bin/python2.7
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
20 # CDDL HEADER END
22 # Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
25 # incorporator - an utility to incorporate packages in a repo
28 import subprocess
29 import json
30 import sys
31 import getopt
32 import re
33 import os.path
34 from pkg.version import Version
36 Werror = False  # set to true to exit with any warning
38 def warning(msg):
39     if Werror == True:
40         print >>sys.stderr, "ERROR: %s" % msg
41         sys.exit(1)
42     else:
43         print >>sys.stderr, "WARNING: %s" % msg
45 class Incorporation(object):
46     name = None
47     version = '5.11'
48     packages = {}
50     def __init__(self, name, version):
51         self.name = name
52         self.version = version
53         self.packages = {}
55     def __package_to_str(self, name, version):
56         # strip the :timestamp from the version string
57         version = version.split(':', 1)[0]
58         # strip the ,{build-release} from the version string
59         version = re.sub(",[\d\.]+", "", version) 
61         return "depend fmri=%s@%s facet.version-lock.%s=true type=incorporate" % (name, version, name)
63     def add_package(self, name, version):
64         self.packages[name] = version
66     def __str__(self):
67         result = """
68 set name=pkg.fmri value=pkg:/%s@%s
69 set name=info.classification value="org.opensolaris.category.2008:Meta Packages/Incorporations"
70 set name=org.opensolaris.consolidation value=userland
71 set name=pkg.depend.install-hold value=core-os.userland
72 set name=pkg.summary value="userland consolidation incorporation (%s)"
73 set name=pkg.description value="This incorporation constrains packages from the userland consolidation"
74 """ % (self.name, self.version, self.name)
76         names = self.packages.keys()
77         names.sort()
78         for name in names:
79             result += (self.__package_to_str(name, self.packages[name]) + '\n')
81         return result
84 # This should probably use the pkg APIs at some point, but this appears to be
85 # a stable and less complicated interface to gathering information from the
86 # manifests in the package repo.
88 def get_incorporations(repository, publisher, inc_version='5.11'):
89     tmp = subprocess.Popen(["/usr/bin/pkgrepo", "list", "-F", "json",
90                                                         "-s", repository,
91                                                         "-p", publisher],
92                            stdout=subprocess.PIPE)
93     incorporations = {}
94     packages = json.load(tmp.stdout)
95     inc_name='consolidation/userland/userland-incorporation'
97     # Check for multiple versions of packages in the repo, but keep track of
98     # the latest one.
99     versions = {}
100     for package in packages:
101         pkg_name = package['name']
102         pkg_version = package['version']
104         if pkg_name in versions:
105             warning("%s is in the repo at multiple versions (%s, %s)" % (pkg_name, pkg_version, versions[pkg_name]))
106             if(Version(package['version']) < Version(versions[pkg_name])):
107                pkg_version = versions[pkg_name]
108         versions[pkg_name] = pkg_version
110     for package in packages:
111         pkg_name = package['name']
112         pkg_version = package['version']
114         # skip older packages and those that explicitly don't want to be incorporated
115         # also skip the incorporation itself
116         if 'pkg.tmp.noincorporate' in package or pkg_version != versions[pkg_name] or pkg_name == inc_name:
117             continue
119         # We don't want to support multiple incorporations for now
120         # a dict inside a list inside a dict
121         # incorporate = package['pkg.tmp.incorporate'][0]['value']
122         
123         # for inc_name in incorporate:
124             # if we haven't started to build this incorporation, create one.
125         if inc_name not in incorporations:
126            incorporations[inc_name] = Incorporation(inc_name, inc_version)
127         # find the incorporation and add the package
128         tmp = incorporations[inc_name]
129         tmp.add_package(pkg_name, pkg_version)
130     return incorporations
132 def main_func():
133     global Werror
135     try: 
136         opts, pargs = getopt.getopt(sys.argv[1:], "c:s:p:v:d:w",
137                                     ["repository=", "publisher=", "version=",
138                                      "consolidation=", "destdir=", "Werror"])
139     except getopt.GetoptError, e:
140         usage(_("illegal option: %s") % e.opt)
142     repository = None
143     publisher = None
144     version = None
145     destdir = None
146     consolidation = None
148     for opt, arg in opts:
149         if opt in ("-s", "--repository"):
150             repository = arg
151         elif opt in ("-p", "--publisher"):
152             publisher = arg
153         elif opt in ("-v", "--version"):
154             version = arg
155         elif opt in ("-d", "--destdir"):
156             destdir = arg
157         elif opt in ("-c", "--consolidation"):
158             consolidation = arg
159         elif opt in ("-w", "--Werror"):
160             Werror = True
162     incorporations = get_incorporations(repository, publisher, version)
164     for incorporation_name in incorporations.keys():
165         filename = ''
166         if destdir != None:
167             filename = destdir + '/'
168         filename += os.path.basename(incorporation_name) + '.p5m'
170         print("Writing %s manifest to %s" % (incorporation_name, filename))
171         fd = open(filename, "w+")
172         fd.write(str(incorporations[incorporation_name]))
173         fd.close()
175 if __name__ == "__main__":
176     main_func()