Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Latest commit

 

History

History
executable file
·
210 lines (166 loc) · 6.79 KB

repomd-pattern-builder.py

File metadata and controls

executable file
·
210 lines (166 loc) · 6.79 KB
 
Feb 12, 2020
Feb 12, 2020
1
#!/usr/bin/python3
Dec 12, 2011
Dec 12, 2011
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This script is used to convert .yaml files to patterns and package groups.
# Copyright (C) 2011 Marko Saukko <FIRSTNAME.LASTNAME@gmail.com>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# This script is based on MeeGo package-group scripts
# https://meego.gitorious.org/meego-os-base/package-groups/
23
24
25
26
27
28
29
30
31
import yaml
import sys, os
import optparse
from lxml import etree
rpm_ns="http://linux.duke.edu/metadata/rpm"
pattern_ns="http://novell.com/package/metadata/suse/pattern"
NSMAP = {None : pattern_ns, "rpm": rpm_ns}
Dec 8, 2011
Dec 8, 2011
32
33
NSMAP_GROUP = {None : pattern_ns, "rpm": rpm_ns, "patterns": pattern_ns}
Nov 26, 2012
Nov 26, 2012
34
def process_yaml(stream, version, release, xmlroot, nsmap_name, newobsapi):
Aug 7, 2012
Aug 7, 2012
35
"Process all documents in the yaml stream and return a count of number handled"
Nov 26, 2012
Nov 26, 2012
36
Feb 12, 2020
Feb 12, 2020
37
all_docs = yaml.load_all(stream, Loader = yaml.SafeLoader)
Nov 26, 2012
Nov 26, 2012
38
Aug 7, 2012
Aug 7, 2012
39
for y in all_docs:
Nov 26, 2012
Nov 26, 2012
40
41
42
# <pattern>
proot = etree.SubElement(xmlroot, "pattern", nsmap=nsmap_name)
Aug 7, 2012
Aug 7, 2012
43
44
45
46
47
48
# <name>
etree.SubElement(proot, "name").text = y['Name']
# Old OBS isn't able to handle these options.
if newobsapi:
# <version>
Feb 12, 2020
Feb 12, 2020
49
if 'Version' in y or version:
Nov 16, 2012
Nov 16, 2012
50
51
52
53
54
55
entry = etree.SubElement(proot, "version")
ver = "0"
if version:
ver = version
else:
ver = y['Version']
Aug 7, 2012
Aug 7, 2012
56
57
# Set to 0 by default as that is what OBS expects.
Nov 16, 2012
Nov 16, 2012
58
epoch = "0"
Feb 12, 2020
Feb 12, 2020
59
if 'Epoch' in y:
Aug 7, 2012
Aug 7, 2012
60
61
62
epoch = y['Epoch']
# As above...
Nov 16, 2012
Nov 16, 2012
63
64
65
rel = "0"
if release:
rel = release
Feb 12, 2020
Feb 12, 2020
66
if 'Release' in y:
Nov 16, 2012
Nov 16, 2012
67
rel = y['Release']
Aug 7, 2012
Aug 7, 2012
68
Nov 16, 2012
Nov 16, 2012
69
70
71
entry.set('ver', "%s" % ver)
entry.set('epoch', "%s" % epoch)
entry.set('rel', "%s" % rel)
Aug 7, 2012
Aug 7, 2012
72
73
# <arch>
Feb 12, 2020
Feb 12, 2020
74
if 'Arch' in y:
Aug 7, 2012
Aug 7, 2012
75
76
77
78
79
80
81
82
83
84
85
86
87
etree.SubElement(proot, "arch").text = "%s" % y['Arch']
# <summary>
etree.SubElement(proot, "summary").text = y['Summary']
# <description>
etree.SubElement(proot, "description").text = y['Description']
# <uservisible>
etree.SubElement(proot, "uservisible")
# <category>
cat = etree.SubElement(proot, "category")
cat.text = "Base Group"
cat.set("lang", "en")
Aug 13, 2014
Aug 13, 2014
88
package_keys = ['Packages','Conflicts', 'Requires', 'Recommends', 'Suggests', 'Provides', 'Obsoletes']
Nov 26, 2012
Nov 26, 2012
89
for key in package_keys:
Feb 12, 2020
Feb 12, 2020
90
if key not in y:
Nov 26, 2012
Nov 26, 2012
91
92
93
94
95
96
continue
collect = y[key]
if key == "Packages":
# Support obsoleted keys, this should be removed in the future
key = "Requires"
Feb 12, 2020
Feb 12, 2020
97
print ("WARNING: Oboleted key 'Packages' in .yaml please change to 'Requires'.")
Nov 26, 2012
Nov 26, 2012
98
99
100
101
req = etree.SubElement(proot, "{%s}%s" % (rpm_ns,key.lower()))
for p in collect:
Nov 16, 2012
Nov 16, 2012
102
if type(p).__name__=='dict':
Feb 12, 2020
Feb 12, 2020
103
print ("ERROR: Found dict and expected string value. '%s'" % (p))
Nov 26, 2012
Nov 26, 2012
104
105
sys.exit(1)
entry = etree.SubElement(req, "{%s}entry" %rpm_ns)
Sep 2, 2013
Sep 2, 2013
106
107
108
name = p
ver = None
Sep 2, 2013
Sep 2, 2013
109
110
111
112
op_in = [">=", "<=", ">", "<", "="]
op_out = ["GE", "LE", "GT", "LT", "EQ"]
opc = 0
for op in op_in:
Sep 2, 2013
Sep 2, 2013
113
114
115
if op in p:
name, ver = p.split(op)
break
Sep 2, 2013
Sep 2, 2013
116
opc = opc + 1
Sep 2, 2013
Sep 2, 2013
117
118
119
entry.set("name", name.strip())
if ver:
Sep 2, 2013
Sep 2, 2013
120
121
entry.set("flags", "%s" % (op_out[opc]))
entry.set("ver", "%s" % (ver.strip()))
Aug 7, 2012
Aug 7, 2012
122
Nov 16, 2012
Nov 16, 2012
123
def create_patterns(patterns_dir, version, release, outputdir, newobsapi):
Nov 26, 2012
Nov 26, 2012
124
125
126
dirlist = os.listdir(patterns_dir)
dirlist.sort()
for f in dirlist:
127
128
129
if not f.endswith('.yaml'):
continue
Feb 12, 2020
Feb 12, 2020
130
stream = open("%s/%s" %(patterns_dir,f), 'r')
Nov 26, 2012
Nov 26, 2012
131
132
133
xmlroot = etree.Element("temporary_root", nsmap=NSMAP)
process_yaml(stream, version, release, xmlroot, NSMAP, newobsapi)
Nov 28, 2012
Nov 28, 2012
135
136
137
138
for pattern in xmlroot.findall("pattern"):
name = pattern.find("name")
if name == None:
Feb 12, 2020
Feb 12, 2020
139
print ("Pattern didn't have name skipping.")
Nov 28, 2012
Nov 28, 2012
140
141
continue
output_file = "%s/%s.xml" % (outputdir,name.text.lower())
Feb 12, 2020
Feb 12, 2020
142
print ("Working on %s" % (output_file))
Nov 26, 2012
Nov 26, 2012
143
Nov 28, 2012
Nov 28, 2012
144
etree.ElementTree(pattern).write(output_file, pretty_print=True)
Nov 16, 2012
Nov 16, 2012
146
def merge_patterns(patterns_dir, version, release, outputdir, newobsapi):
Dec 8, 2011
Dec 8, 2011
147
xmlroot = etree.Element("patterns")
Nov 28, 2012
Nov 28, 2012
148
output_file = "%s/patterns.xml" % (outputdir)
Nov 26, 2012
Nov 26, 2012
149
150
dirlist = os.listdir(patterns_dir)
dirlist.sort()
Nov 26, 2012
Nov 26, 2012
151
Nov 26, 2012
Nov 26, 2012
152
for f in dirlist:
Dec 8, 2011
Dec 8, 2011
153
154
if not f.endswith('.yaml'):
continue
Feb 12, 2020
Feb 12, 2020
155
print ("Merging %s to %s." % (f,output_file))
Dec 8, 2011
Dec 8, 2011
156
stream = file("%s/%s" %(patterns_dir,f), 'r')
Nov 26, 2012
Nov 26, 2012
157
158
159
160
process_yaml(stream, version, release, xmlroot, NSMAP_GROUP, newobsapi)
patterns = xmlroot.findall("pattern")
xmlroot.set('count', "%d" % (len(patterns)))
Dec 8, 2011
Dec 8, 2011
161
Aug 7, 2012
Aug 7, 2012
162
etree.ElementTree(xmlroot).write(output_file, pretty_print=True)
163
164
165
166
if __name__ == '__main__':
parser = optparse.OptionParser()
Dec 8, 2011
Dec 8, 2011
167
168
169
parser.add_option("", "--patternxml", action="store_true", dest="patternxml",
default=False,
help="Create separated pattern XML file for each pattern.")
Nov 28, 2012
Nov 28, 2012
170
171
172
parser.add_option("", "--patternsxml", action="store_true", dest="patternsxml",
default=False,
help="Create merged patterns.xml from all the available patterns.")
Dec 8, 2011
Dec 8, 2011
173
174
parser.add_option("", "--groupxml", action="store_true", dest="groupxml",
default=False,
Nov 28, 2012
Nov 28, 2012
175
help="Create group.xml.")
176
177
178
179
parser.add_option("-p", "--patterndir", type="string", dest="patterndir",
default=None,
help="Directory where the pattern .yaml files are located.")
parser.add_option("-o", "--outputdir", type="string", dest="outputdir",
Dec 8, 2011
Dec 8, 2011
180
default=".",
181
help="Output directory where the resulting .xml files are created.")
Dec 12, 2011
Dec 12, 2011
182
183
184
parser.add_option("", "--old-obs-xml-format", action="store_false", dest="newobsapi",
default=True,
help="The old OBS api isn't able to handle the newer xml format.")
Nov 16, 2012
Nov 16, 2012
185
186
parser.add_option("--version", type="string", dest="version", default=None, help="Version number")
parser.add_option("--release", type="string", dest="release", default=None, help="Release number")
Dec 12, 2011
Dec 12, 2011
187
188
(options, args) = parser.parse_args()
Dec 8, 2011
Dec 8, 2011
189
Nov 28, 2012
Nov 28, 2012
190
if (options.groupxml):
Feb 12, 2020
Feb 12, 2020
191
print ("ERROR: Groupxml isn't supported atm.")
Nov 28, 2012
Nov 28, 2012
192
193
194
exit(1)
if (not options.patternsxml and not options.patternxml):
Nov 26, 2012
Nov 26, 2012
195
196
# Default to patternxml.
options.patternxml = True
Dec 12, 2011
Dec 12, 2011
197
Dec 8, 2011
Dec 8, 2011
198
if (not options.patterndir or not os.path.exists(options.patterndir)):
Feb 12, 2020
Feb 12, 2020
199
print ("Error: Pattern dir '%s' doesn't exist." % (options.patterndir))
Dec 8, 2011
Dec 8, 2011
200
exit(1)
Dec 12, 2011
Dec 12, 2011
201
Dec 8, 2011
Dec 8, 2011
202
203
204
205
if options.outputdir and not os.path.exists(options.outputdir):
os.makedirs(options.outputdir)
if options.patternxml:
Nov 16, 2012
Nov 16, 2012
206
create_patterns(options.patterndir, options.version, options.release, options.outputdir, options.newobsapi)
Nov 28, 2012
Nov 28, 2012
208
if options.patternsxml:
Nov 16, 2012
Nov 16, 2012
209
merge_patterns(options.patterndir, options.version, options.release, options.outputdir, options.newobsapi)