arrfab / centos / comps

Forked from centos/comps 5 years ago
Clone

Blame tools/comps-cleanup.xsl

1cd2b1
1cd2b1
1cd2b1
     - reorder, indent and normalize a comps file,
1cd2b1
     - merge duplicate groups and categories,
1cd2b1
     - warn about packages referenced by multiple groups,
1cd2b1
     - kill multiple references to the same package within a group,
1cd2b1
1cd2b1
     Typical usage is:
1cd2b1
     $ xsltproc -o output-file comps-cleanup.xsl original-file
1cd2b1
1cd2b1
     You can use the ‑‑novalid xsltproc switch to kill the warning about
1cd2b1
     Fedora not installing the comps DTD anywhere xsltproc can find it.
1cd2b1
     However without DTD there is no way to check the files completely.
1cd2b1
1cd2b1
     © Nicolas Mailhot <nim at fedoraproject dot org> 2006-2008 -->
1cd2b1
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" version="1.0" extension-element-prefixes="exsl">
1cd2b1
  <xsl:strip-space elements="*"/>
1cd2b1
  <xsl:output method="xml" indent="yes" encoding="UTF-8" doctype-system="comps.dtd" doctype-public="-//CentOS//DTD Comps info//EN"/>
1cd2b1
  <xsl:key name="unique-groups" match="/comps/group" use="id/text()"/>
1cd2b1
  <xsl:key name="unique-environments" match="/comps/environment" use="id/text()"/>
1cd2b1
  <xsl:key name="unique-categories" match="/comps/category" use="id/text()"/>
1cd2b1
  <xsl:key name="packages-by-group" match="/comps/group/packagelist/packagereq" use="../../id/text()"/>
1cd2b1
  <xsl:key name="unique-package-entries" match="/comps/group/packagelist/packagereq" use="concat(../../id/text(),'/',text())"/>
1cd2b1
  <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()"/>
1cd2b1
  <xsl:key name="groups-by-category" match="/comps/category/grouplist/groupid" use="../../id/text()"/>
1cd2b1
  <xsl:key name="unique-group-entries" match="/comps/category/grouplist/groupid" use="concat(../../id/text(),'/',text())"/>
1cd2b1
  <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
1cd2b1
  <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
1cd2b1
  <xsl:variable name="type-sort-order">
1cd2b1
    <unknown/>
1cd2b1
    <mandatory/>
1cd2b1
    <conditional/>
1cd2b1
    <default/>
1cd2b1
    <optional/>
1cd2b1
  </xsl:variable>
1cd2b1
  <xsl:variable name="attribute-sort-order">
1cd2b1
    <unknown/>
1cd2b1
    <arch/>
1cd2b1
    <name/>
1cd2b1
    <package/>
1cd2b1
    <type/>
1cd2b1
    <requires/>
1cd2b1
    <basearch/>
1cd2b1
  </xsl:variable>
1cd2b1
  
1cd2b1
  <xsl:template match="*" priority="0">
1cd2b1
    <xsl:apply-templates select="." mode="normalize"/>
1cd2b1
  </xsl:template>
1cd2b1
  <xsl:template match="*" mode="normalize">
1cd2b1
    
1cd2b1
    <xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="@*">
1cd2b1
        <xsl:sort select="count(exsl:node-set($attribute-sort-order)/*[name() = name(current())]/preceding-sibling::*)" data-type="number"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
      <xsl:apply-templates select="*|text()"/>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="comment()|text()">
1cd2b1
    <xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
1cd2b1
    <xsl:copy/>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="@*">
1cd2b1
    <xsl:copy/>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="comps" priority="1">
1cd2b1
    <xsl:apply-templates select="preceding-sibling::node()[normalize-space()][1][self::comment()] "/>
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="group">
1cd2b1
        <xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
      <xsl:apply-templates select="category">
1cd2b1
        <xsl:sort select="display_order/text()"/>
1cd2b1
        <xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
      <xsl:apply-templates select="environment">
1cd2b1
        <xsl:sort select="display_order/text()"/>
1cd2b1
        <xsl:sort select="translate(id/text(),$lcletters,$ucletters)"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
      <xsl:apply-templates select="langpacks"/>
1cd2b1
      <xsl:apply-templates select="blacklist"/>
1cd2b1
      <xsl:apply-templates select="whiteout"/>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  <xsl:template match="langpacks" priority="1">
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="match">
1cd2b1
       <xsl:sort select="@name"/>
1cd2b1
       <xsl:sort select="@install"/>
1cd2b1
    </xsl:apply-templates>
1cd2b1
   </xsl:copy>
1cd2b1
 </xsl:template>
1cd2b1
  <xsl:template match="blacklist" priority="1">
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="package">
1cd2b1
        <xsl:sort select="@arch"/>
1cd2b1
        <xsl:sort select="@name"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  <xsl:template match="whiteout" priority="1">
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="ignoredep">
1cd2b1
        <xsl:sort select="@package"/>
1cd2b1
        <xsl:sort select="@requires"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="group[count(key('packages-by-group',id/text()))=0]" priority="2">
1cd2b1
    <xsl:message>☹☹☹ Empty group <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/>!</xsl:message>
1cd2b1
    <xsl:apply-templates select="." mode="normalize"/>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="group[generate-id(.) != generate-id(key('unique-groups',id/text())[1])]" priority="3">
1cd2b1
    <xsl:message> ☹☹ Duplicate group <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="category[count(key('groups-by-category',id/text()))=0]" priority="2">
1cd2b1
    <xsl:message>☹☹☹ Empty category <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/>!</xsl:message>
1cd2b1
    <xsl:apply-templates select="." mode="normalize"/>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="environment[generate-id(.) != generate-id(key('unique-environments',id/text())[1])]" priority="3">
1cd2b1
    <xsl:message> ☹☹ Duplicate environment <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="category[generate-id(.) != generate-id(key('unique-categories',id/text())[1])]" priority="3">
1cd2b1
    <xsl:message> ☹☹ Duplicate category <xsl:value-of select="concat(_name/text(),' (',id/text(),')')"/> will be merged.</xsl:message>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="packagelist" priority="1">
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="key('packages-by-group',../id/text())">
1cd2b1
        <xsl:sort select="count(exsl:node-set($type-sort-order)/*[name() = current()/@type]/preceding-sibling::*)" data-type="number"/>
1cd2b1
        <xsl:sort select="translate(text(),$lcletters,$ucletters)"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="category/grouplist" priority="1">
1cd2b1
    <xsl:copy>
1cd2b1
      <xsl:apply-templates select="key('groups-by-category',../id/text())">
1cd2b1
        <xsl:sort select="translate(text(),$lcletters,$ucletters)"/>
1cd2b1
      </xsl:apply-templates>
1cd2b1
    </xsl:copy>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="packagereq[generate-id(.) != generate-id(key('unique-package-entries',concat(../../id/text(),'/',text()))[1])]" priority="2">
1cd2b1
    <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>
1cd2b1
    <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>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="category/grouplist/groupid[generate-id(.) != generate-id(key('unique-group-entries',concat(../../id/text(),'/',text()))[1])]" priority="1">
1cd2b1
    <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>
1cd2b1
  </xsl:template>
1cd2b1
  
1cd2b1
  <xsl:template match="packagereq[generate-id(.) = generate-id(key('unique-packages',text())[2])]" priority="1">
1cd2b1
    <xsl:variable name="dupes" select="key('unique-packages',text())"/>
1cd2b1
    <xsl:message>  ☹ Package <xsl:value-of select="text()"/> is referenced in <xsl:value-of select="count($dupes)"/> groups:</xsl:message>
1cd2b1
    <xsl:for-each select="$dupes">
1cd2b1
      <xsl:sort select="translate(../../id/text(),$lcletters,$ucletters)"/>
1cd2b1
      <xsl:message>     ✓ <xsl:value-of select="@type"/> package in group <xsl:value-of select="concat(../../_name/text(),' (',../../id/text(),')')"/></xsl:message>
1cd2b1
    </xsl:for-each>
1cd2b1
    <xsl:apply-templates select="." mode="normalize"/>
1cd2b1
  </xsl:template> -->
1cd2b1
</xsl:stylesheet>