This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comps-cleanup.xsl
170 lines (167 loc) · 8.99 KB
/
comps-cleanup.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?xml version="1.0" encoding="UTF-8"?>
<!-- This stylesheet will:
- reorder, indent and normalize a comps file,
- merge duplicate groups and categories,
- warn about packages referenced by multiple groups,
- kill multiple references to the same package within a group,
Typical usage is:
$ xsltproc -o output-file comps-cleanup.xsl original-file
You can use the ‑‑novalid xsltproc switch to kill the warning about
Fedora not installing the comps DTD anywhere xsltproc can find it.
However without DTD there is no way to check the files completely.
© Nicolas Mailhot <nim at fedoraproject dot org> 2006-2008 -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" version="1.0" extension-element-prefixes="exsl">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-8" doctype-system="comps.dtd" doctype-public="-//Red Hat, Inc.//DTD Comps info//EN"/>
<xsl:key name="unique-groups" match="/comps/group" use="id/text()"/>
<xsl:key name="unique-environments" match="/comps/environment" use="id/text()"/>
<xsl:key name="unique-categories" match="/comps/category" use="id/text()"/>
<xsl:key name="packages-by-group" match="/comps/group/packagelist/packagereq" use="../../id/text()"/>
<xsl:key name="unique-package-entries" match="/comps/group/packagelist/packagereq" use="concat(../../id/text(),'/',text())"/>
<xsl:key name="unique-packages" match="/comps/group/packagelist/packagereq[generate-id(.) = generate-id(key('unique-package-entries',concat(../../id/text(),'/',text()))[1])]" use="text()"/>
<xsl:key name="groups-by-category" match="/comps/category/grouplist/groupid" use="../../id/text()"/>
<xsl:key name="unique-group-entries" match="/comps/category/grouplist/groupid" use="concat(../../id/text(),'/',text())"/>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="type-sort-order">
<unknown/>
<mandatory/>
<conditional/>
<default/>
<optional/>
</xsl:variable>
<xsl:variable name="attribute-sort-order">
<unknown/>
<arch/>
<name/>
<package/>
<type/>
<requires/>
<basearch/>
</xsl:variable>
<!-- Preserve most nodes -->
<xsl:template match="*" priority="0">
<xsl:apply-templates select="." mode="normalize"/>
</xsl:template>
<xsl:template match="*" mode="normalize">
<!-- Group comments with the logically-following element -->
<xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:sort select="count(exsl:node-set($attribute-sort-order)/*[name() = name(current())]/preceding-sibling::*)" data-type="number"/>
</xsl:apply-templates>
<xsl:apply-templates select="*|text()"/>
</xsl:copy>
</xsl:template>
<!-- Preserve attributes and text nodes -->
<xsl:template match="comment()|text()">
<xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
<xsl:copy/>
</xsl:template>
<!-- Preserve attributes -->
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
<!-- Sort groups by id, and categories by display order -->
<xsl:template match="comps" priority="1">
<xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
<xsl:copy>
<xsl:apply-templates select="group">
<xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
</xsl:apply-templates>
<xsl:apply-templates select="environment">
<xsl:sort select="display_order/text()"/>
<xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
</xsl:apply-templates>
<xsl:apply-templates select="category">
<xsl:sort select="display_order/text()"/>
<xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
</xsl:apply-templates>
<xsl:apply-templates select="langpacks"/>
<xsl:apply-templates select="blacklist"/>
<xsl:apply-templates select="whiteout"/>
</xsl:copy>
</xsl:template>
<xsl:template match="langpacks" priority="1">
<xsl:copy>
<xsl:apply-templates select="match">
<xsl:sort select="@name"/>
<xsl:sort select="@install"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="blacklist" priority="1">
<xsl:copy>
<xsl:apply-templates select="package">
<xsl:sort select="@arch"/>
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="whiteout" priority="1">
<xsl:copy>
<xsl:apply-templates select="ignoredep">
<xsl:sort select="@package"/>
<xsl:sort select="@requires"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Warn about empty groups -->
<xsl:template match="group[count(key('packages-by-group',id/text()))=0]" priority="2">
<xsl:message>☹☹☹ Empty group <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/>!</xsl:message>
<xsl:apply-templates select="." mode="normalize"/>
</xsl:template>
<!-- Warn about duplicate groups being merged -->
<xsl:template match="group[generate-id(.) != generate-id(key('unique-groups',id/text())[1])]" priority="3">
<xsl:message> ☹☹ Duplicate group <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
</xsl:template>
<!-- Warn about empty categories -->
<xsl:template match="category[count(key('groups-by-category',id/text()))=0]" priority="2">
<xsl:message>☹☹☹ Empty category <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/>!</xsl:message>
<xsl:apply-templates select="." mode="normalize"/>
</xsl:template>
<!-- Warn about duplicate environments being merged -->
<xsl:template match="environment[generate-id(.) != generate-id(key('unique-environments',id/text())[1])]" priority="3">
<xsl:message> ☹☹ Duplicate environment <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
</xsl:template>
<!-- Warn about duplicate categories being merged -->
<xsl:template match="category[generate-id(.) != generate-id(key('unique-categories',id/text())[1])]" priority="3">
<xsl:message> ☹☹ Duplicate category <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
</xsl:template>
<!-- Sort packages within a group by class then name -->
<xsl:template match="packagelist" priority="1">
<xsl:copy>
<xsl:apply-templates select="key('packages-by-group',../id/text())">
<xsl:sort select="count(exsl:node-set($type-sort-order)/*[name() = current()/@type]/preceding-sibling::*)" data-type="number"/>
<xsl:sort select="translate(text(),$lcletters,$ucletters)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Sort groups within a category by name -->
<xsl:template match="category/grouplist" priority="1">
<xsl:copy>
<xsl:apply-templates select="key('groups-by-category',../id/text())">
<xsl:sort select="translate(text(),$lcletters,$ucletters)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Kill duplicate package entries -->
<xsl:template match="packagereq[generate-id(.) != generate-id(key('unique-package-entries',concat(../../id/text(),'/',text()))[1])]" priority="2">
<xsl:message>☹☹☹ Ignoring duplicate reference to <xsl:value-of select="concat(@type,' package ',text())"/> in group <xsl:value-of select="concat(../../_name/text(),' (',../../id/text(),')')"/>.</xsl:message>
<xsl:message> ⇒ Only its first reference (<xsl:value-of select="key('unique-package-entries',concat(../../id/text(),'/',text()))[1]/@type"/> package) will be kept.</xsl:message>
</xsl:template>
<!-- Kill duplicate group entries -->
<xsl:template match="category/grouplist/groupid[generate-id(.) != generate-id(key('unique-group-entries',concat(../../id/text(),'/',text()))[1])]" priority="1">
<xsl:message> ☹ Ignoring duplicate reference to group <xsl:value-of select="text()"/> in category <xsl:value-of select="concat(../../_name/text(),' (',../../id/text(),')')"/>.</xsl:message>
</xsl:template>
<!-- Warn about packages referenced several times
<xsl:template match="packagereq[generate-id(.) = generate-id(key('unique-packages',text())[2])]" priority="1">
<xsl:variable name="dupes" select="key('unique-packages',text())"/>
<xsl:message> ☹ Package <xsl:value-of select="text()"/> is referenced in <xsl:value-of select="count($dupes)"/> groups:</xsl:message>
<xsl:for-each select="$dupes">
<xsl:sort select="translate(../../id/text(),$lcletters,$ucletters)"/>
<xsl:message> ✓ <xsl:value-of select="@type"/> package in group <xsl:value-of select="concat(../../_name/text(),' (',../../id/text(),')')"/></xsl:message>
</xsl:for-each>
<xsl:apply-templates select="." mode="normalize"/>
</xsl:template> -->
</xsl:stylesheet>