source: BLFS/xsl/scripts.xsl@ c4ad7bf

2.4 ablfs-more legacy new_features trunk
Last change on this file since c4ad7bf was 642722f, checked in by Pierre Labastie <pierre@…>, 11 years ago

Improve script generation for BLFS tools:

  • introduce a template for downloading code
  • account for various layout of the BLFS pages for package information and additional

downloads

  • Add the possibility for addtional downloads in xorg pages
  • Property mode set to 100644
File size: 18.3 KB
Line 
1<?xml version="1.0"?>
2
3<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:exsl="http://exslt.org/common"
5 extension-element-prefixes="exsl"
6 version="1.0">
7
8<!-- $Id: scripts.xsl 34 2012-02-21 16:05:09Z labastie $ -->
9
10<!-- XSLT stylesheet to create shell scripts from "linear build" BLFS books. -->
11
12 <!-- Build as user (y) or as root (n)? -->
13 <xsl:param name="sudo" select="'y'"/>
14
15 <xsl:template match="/">
16 <xsl:apply-templates select="//sect1"/>
17 </xsl:template>
18
19<!--=================== Master chunks code ======================-->
20
21 <xsl:template match="sect1">
22
23 <xsl:if test="@id != 'bootscripts'">
24 <!-- The file names -->
25 <xsl:variable name="filename" select="@id"/>
26
27 <!-- The build order -->
28 <xsl:variable name="position" select="position()"/>
29 <xsl:variable name="order">
30 <xsl:choose>
31 <xsl:when test="string-length($position) = 1">
32 <xsl:text>00</xsl:text>
33 <xsl:value-of select="$position"/>
34 </xsl:when>
35 <xsl:when test="string-length($position) = 2">
36 <xsl:text>0</xsl:text>
37 <xsl:value-of select="$position"/>
38 </xsl:when>
39 <xsl:otherwise>
40 <xsl:value-of select="$position"/>
41 </xsl:otherwise>
42 </xsl:choose>
43 </xsl:variable>
44
45 <!-- Depuration code -->
46 <xsl:message>
47 <xsl:text>SCRIPT is </xsl:text>
48 <xsl:value-of select="concat($order,'-z-',$filename)"/>
49 <xsl:text>&#xA; FTPDIR is </xsl:text>
50 <xsl:value-of select="$filename"/>
51 <xsl:text>&#xA;&#xA;</xsl:text>
52 </xsl:message>
53
54 <!-- Creating the scripts -->
55 <exsl:document href="{$order}-z-{$filename}" method="text">
56 <xsl:text>#!/bin/bash&#xA;set -e&#xA;&#xA;</xsl:text>
57 <xsl:choose>
58 <!-- Package page -->
59 <xsl:when test="sect2[@role='package']">
60 <!-- We build in a subdirectory -->
61 <xsl:text>PKG_DIR=</xsl:text>
62 <xsl:value-of select="$filename"/>
63 <xsl:text>&#xA;</xsl:text>
64 <!-- Download code and build commands -->
65 <xsl:apply-templates select="sect2"/>
66 <!-- Clean-up -->
67 <xsl:text>cd $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
68 <!-- In some case, some files in the build tree are owned
69 by root -->
70 <xsl:if test="$sudo='y'">
71 <xsl:text>sudo </xsl:text>
72 </xsl:if>
73 <xsl:text>rm -rf $UNPACKDIR unpacked&#xA;&#xA;</xsl:text>
74 </xsl:when>
75 <!-- Non-package page -->
76 <xsl:otherwise>
77 <xsl:apply-templates select=".//screen"/>
78 </xsl:otherwise>
79 </xsl:choose>
80 <xsl:text>exit</xsl:text>
81 </exsl:document>
82 </xsl:if>
83 </xsl:template>
84
85<!--======================= Sub-sections code =======================-->
86
87 <xsl:template match="sect2">
88 <xsl:choose>
89 <xsl:when test="@role = 'package'">
90 <xsl:text>mkdir -p $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
91 <xsl:text>cd $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
92 <!-- Download information is in bridgehead tags -->
93 <xsl:apply-templates select="bridgehead[@renderas='sect3']"/>
94 <xsl:text>&#xA;</xsl:text>
95 </xsl:when>
96 <xsl:when test="@role = 'installation'">
97 <xsl:text>
98if [ "${PACKAGE%.zip}" = "${PACKAGE}" ]; then
99 if [[ -e unpacked ]] ; then
100 UNPACKDIR=`grep '[^./]\+' unpacked | head -n1 | sed 's@^./@@;s@/.*@@'`
101 [[ -n $UNPACKDIR ]] &amp;&amp; [[ -d $UNPACKDIR ]] &amp;&amp; rm -rf $UNPACKDIR
102 fi
103 tar -xvf $PACKAGE > unpacked
104 UNPACKDIR=`grep '[^./]\+' unpacked | head -n1 | sed 's@^./@@;s@/.*@@'`
105else
106 UNPACKDIR=${PACKAGE%.zip}
107 [[ -n $UNPACKDIR ]] &amp;&amp; [[ -d $UNPACKDIR ]] &amp;&amp; rm -rf $UNPACKDIR
108 unzip -d $UNPACKDIR ${PACKAGE}
109fi
110cd $UNPACKDIR&#xA;&#xA;</xsl:text>
111 <xsl:apply-templates select=".//screen | .//para/command"/>
112 <xsl:if test="$sudo = 'y'">
113 <xsl:text>sudo /sbin/</xsl:text>
114 </xsl:if>
115 <xsl:text>ldconfig&#xA;&#xA;</xsl:text>
116 </xsl:when>
117 <xsl:when test="@role = 'configuration'">
118 <xsl:apply-templates select=".//screen" mode="config"/>
119 </xsl:when>
120 </xsl:choose>
121 </xsl:template>
122
123<!--==================== Download code =======================-->
124
125 <!-- template for extracting the filename from an url in the form:
126 proto://internet.name/dir1/.../dirn/filename?condition.
127 Needed, because substring-after(...,'/') returns only the
128 substring after the first '/'. -->
129 <xsl:template name="package_name">
130 <xsl:param name="url" select="foo"/>
131 <xsl:param name="sub-url" select="substring-after($url,'/')"/>
132 <xsl:choose>
133 <xsl:when test="contains($sub-url,'/')">
134 <xsl:call-template name="package_name">
135 <xsl:with-param name="url" select="$sub-url"/>
136 </xsl:call-template>
137 </xsl:when>
138 <xsl:otherwise>
139 <xsl:choose>
140 <xsl:when test="contains($sub-url,'?')">
141 <xsl:value-of select="substring-before($sub-url,'?')"/>
142 </xsl:when>
143 <xsl:otherwise>
144 <xsl:value-of select="$sub-url"/>
145 </xsl:otherwise>
146 </xsl:choose>
147 </xsl:otherwise>
148 </xsl:choose>
149 </xsl:template>
150
151 <!-- Generates the code to download a package, an additional package or
152 a patch. -->
153 <xsl:template name="download-file">
154 <xsl:param name="httpurl" select="''"/>
155 <xsl:param name="ftpurl" select="''"/>
156 <xsl:param name="md5" select="''"/>
157 <xsl:param name="varname" select="''"/>
158 <xsl:variable name="package">
159 <xsl:call-template name="package_name">
160 <xsl:with-param name="url">
161 <xsl:choose>
162 <xsl:when test="string-length($httpurl) &gt; 10">
163 <xsl:value-of select="$httpurl"/>
164 </xsl:when>
165 <xsl:otherwise>
166 <xsl:value-of select="$ftpurl"/>
167 </xsl:otherwise>
168 </xsl:choose>
169 </xsl:with-param>
170 </xsl:call-template>
171 </xsl:variable>
172 <xsl:variable name="first_letter"
173 select="translate(substring($package,1,1),
174 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
175 'abcdefghijklmnopqrstuvwxyz')"/>
176 <xsl:text>&#xA;</xsl:text>
177 <xsl:value-of select="$varname"/>
178 <xsl:text>=</xsl:text>
179 <xsl:value-of select="$package"/>
180 <xsl:text>&#xA;if [[ ! -f $</xsl:text>
181 <xsl:value-of select="$varname"/>
182 <xsl:text> ]] ; then&#xA;</xsl:text>
183 <!-- SRC_ARCHIVE may have subdirectories or not -->
184 <xsl:text> if [[ -f $SRC_ARCHIVE/$PKG_DIR/$</xsl:text>
185 <xsl:value-of select="$varname"/>
186 <xsl:text> ]] ; then&#xA;</xsl:text>
187 <xsl:text> cp $SRC_ARCHIVE/$PKG_DIR/$</xsl:text>
188 <xsl:value-of select="$varname"/>
189 <xsl:text> $</xsl:text>
190 <xsl:value-of select="$varname"/>
191 <xsl:text>&#xA;</xsl:text>
192 <xsl:text> elif [[ -f $SRC_ARCHIVE/$</xsl:text>
193 <xsl:value-of select="$varname"/>
194 <xsl:text> ]] ; then&#xA;</xsl:text>
195 <xsl:text> cp $SRC_ARCHIVE/$</xsl:text>
196 <xsl:value-of select="$varname"/>
197 <xsl:text> $</xsl:text>
198 <xsl:value-of select="$varname"/>
199 <xsl:text>&#xA; else&#xA;</xsl:text>
200 <!-- The FTP_SERVER mirror -->
201 <xsl:text> wget -T 30 -t 5 ${FTP_SERVER}svn/</xsl:text>
202 <xsl:value-of select="$first_letter"/>
203 <xsl:text>/$</xsl:text>
204 <xsl:value-of select="$varname"/>
205 <xsl:if test="string-length($httpurl) &gt; 10">
206 <xsl:text> ||
207 wget -T 30 -t 5 </xsl:text>
208 <xsl:value-of select="$httpurl"/>
209 </xsl:if>
210 <xsl:if test="string-length($ftpurl) &gt; 10">
211 <xsl:text> ||
212 wget -T 30 -t 5 </xsl:text>
213 <xsl:value-of select="$ftpurl"/>
214 </xsl:if>
215 <xsl:text>
216 cp $</xsl:text>
217 <xsl:value-of select="$varname"/>
218 <xsl:text> $SRC_ARCHIVE
219 fi
220fi
221</xsl:text>
222 <xsl:if test="string-length($md5) &gt; 10">
223 <xsl:text>echo "</xsl:text>
224 <xsl:value-of select="$md5"/>
225 <xsl:text>&#x20;&#x20;$</xsl:text>
226 <xsl:value-of select="$varname"/>
227 <xsl:text>" | md5sum -c -
228</xsl:text>
229 </xsl:if>
230 </xsl:template>
231
232 <!-- Extract the MD5 sum information -->
233 <xsl:template match="para" mode="md5">
234 <xsl:choose>
235 <xsl:when test="contains(substring-after(string(),'sum: '),'&#xA;')">
236 <xsl:value-of select="substring-before(substring-after(string(),'sum: '),'&#xA;')"/>
237 </xsl:when>
238 <xsl:otherwise>
239 <xsl:value-of select="substring-after(string(),'sum: ')"/>
240 </xsl:otherwise>
241 </xsl:choose>
242 </xsl:template>
243
244 <!-- We have several templates itemizedlist, depending on whether we
245 expect the package information, or additional package(s) or patch(es)
246 information. Select the appropriate mode here. -->
247 <xsl:template match="bridgehead">
248 <xsl:choose>
249 <!-- Special case for Openjdk -->
250 <xsl:when test="contains(string(),'Source Package Information')">
251 <xsl:apply-templates
252 select="following-sibling::itemizedlist[1]//simplelist">
253 <xsl:with-param name="varname" select="'PACKAGE'"/>
254 </xsl:apply-templates>
255 <xsl:apply-templates select="following-sibling::itemizedlist
256 [preceding-sibling::bridgehead[1]=current()
257 and position() &gt;1]//simplelist">
258 <xsl:with-param name="varname" select="'PACKAGE1'"/>
259 </xsl:apply-templates>
260 </xsl:when>
261 <!-- Package information -->
262 <xsl:when test="contains(string(),'Package Information')">
263 <xsl:apply-templates select="following-sibling::itemizedlist
264 [preceding-sibling::bridgehead[1]=current()]"
265 mode="package"/>
266 </xsl:when>
267 <!-- Additional package information -->
268 <xsl:when test="contains(string(),'Additional')">
269 <xsl:apply-templates select="following-sibling::itemizedlist"
270 mode="additional"/>
271 </xsl:when>
272 <!-- Do not do anything if the dev has created another type of
273 bridgehead. -->
274 <xsl:otherwise/>
275 </xsl:choose>
276 </xsl:template>
277
278 <!-- Call the download code template with appropriate parameters -->
279 <xsl:template match="itemizedlist" mode="package">
280 <xsl:call-template name="download-file">
281 <xsl:with-param name="httpurl">
282 <xsl:value-of select="./listitem[1]/para/ulink/@url"/>
283 </xsl:with-param>
284 <xsl:with-param name="ftpurl">
285 <xsl:value-of select="./listitem/para[contains(string(),'FTP')]/ulink/@url"/>
286 </xsl:with-param>
287 <xsl:with-param name="md5">
288 <xsl:apply-templates select="./listitem/para[contains(string(),'MD5')]"
289 mode="md5"/>
290 </xsl:with-param>
291 <xsl:with-param name="varname" select="'PACKAGE'"/>
292 </xsl:call-template>
293 </xsl:template>
294
295 <xsl:template match="itemizedlist" mode="additional">
296 <!-- The normal layout is "one listitem"<->"one url", but some devs
297 find amusing to have FTP and/or MD5sum listitems, or to
298 enclose the download information inside a simplelist tag... -->
299 <xsl:for-each select="listitem[.//ulink]">
300 <xsl:choose>
301 <!-- hopefully, there was a HTTP line before -->
302 <xsl:when test="contains(string(./para),'FTP')"/>
303 <xsl:when test=".//simplelist">
304 <xsl:apply-templates select=".//simplelist">
305 <xsl:with-param name="varname" select="'PACKAGE1'"/>
306 </xsl:apply-templates>
307 </xsl:when>
308 <xsl:otherwise>
309 <xsl:call-template name="download-file">
310 <xsl:with-param name="httpurl">
311 <xsl:value-of select="./para/ulink/@url"/>
312 </xsl:with-param>
313 <xsl:with-param name="ftpurl">
314 <xsl:value-of
315 select="following-sibling::listitem[1]/
316 para[contains(string(),'FTP')]/ulink/@url"/>
317 </xsl:with-param>
318 <xsl:with-param name="md5">
319 <xsl:apply-templates
320 select="following-sibling::listitem[position()&lt;3]/
321 para[contains(string(),'MD5')]"
322 mode="md5"/>
323 </xsl:with-param>
324 <xsl:with-param name="varname">
325 <xsl:choose>
326 <xsl:when test="contains(./para/ulink/@url,'.patch')">
327 <xsl:text>PATCH</xsl:text>
328 </xsl:when>
329 <xsl:otherwise>
330 <xsl:text>PACKAGE1</xsl:text>
331 </xsl:otherwise>
332 </xsl:choose>
333 </xsl:with-param>
334 </xsl:call-template>
335 </xsl:otherwise>
336 </xsl:choose>
337 </xsl:for-each>
338 </xsl:template>
339
340 <!-- the simplelist case. Hopefully, the layout is one member for
341 url, one for md5 and others for various information, that we do not
342 use -->
343 <xsl:template match="simplelist">
344 <xsl:param name="varname" select="'PACKAGE1'"/>
345 <xsl:call-template name="download-file">
346 <xsl:with-param name="httpurl" select=".//ulink/@url"/>
347 <xsl:with-param name="md5">
348 <xsl:value-of select="substring-after(member[contains(string(),'MD5')],'sum: ')"/>
349 </xsl:with-param>
350 <xsl:with-param name="varname" select="$varname"/>
351 </xsl:call-template>
352 </xsl:template>
353<!--======================== Commands code ==========================-->
354
355 <xsl:template match="screen">
356 <xsl:if test="child::* = userinput and not(@role = 'nodump')">
357 <xsl:choose>
358 <xsl:when test="@role = 'root'">
359 <xsl:if test="$sudo = 'y'">
360 <xsl:text>sudo -E sh &lt;&lt; ROOT_EOF&#xA;</xsl:text>
361 </xsl:if>
362 <xsl:apply-templates mode="root"/>
363 <xsl:if test="$sudo = 'y'">
364 <xsl:text>&#xA;ROOT_EOF</xsl:text>
365 </xsl:if>
366 </xsl:when>
367 <xsl:otherwise>
368 <xsl:apply-templates select="userinput"/>
369 </xsl:otherwise>
370 </xsl:choose>
371 <xsl:text>&#xA;</xsl:text>
372 </xsl:if>
373 </xsl:template>
374
375 <xsl:template match="screen" mode="config">
376 <xsl:if test="preceding-sibling::para[1]/xref[@linkend='bootscripts']">
377 <xsl:text>[[ ! -d $SRC_DIR/blfs-bootscripts ]] &amp;&amp; mkdir $SRC_DIR/blfs-bootscripts
378pushd $SRC_DIR/blfs-bootscripts
379URL=</xsl:text>
380 <xsl:value-of select="id('bootscripts')//itemizedlist//ulink/@url"/><xsl:text>
381BOOTPACKG=$(basename $URL)
382if [[ ! -f $BOOTPACKG ]] ; then
383 if [[ -f $SRC_ARCHIVE/$PKG_DIR/$BOOTPACKG ]] ; then
384 cp $SRC_ARCHIVE/$PKG_DIR/$BOOTPACKG $BOOTPACKG
385 elif [[ -f $SRC_ARCHIVE/$BOOTPACKG ]] ; then
386 cp $SRC_ARCHIVE/$BOOTPACKG $BOOTPACKG
387 else
388 wget -T 30 -t 5 $URL
389 cp $BOOTPACKG $SRC_ARCHIVE
390 fi
391 rm -f unpacked
392fi
393
394if [[ -e unpacked ]] ; then
395 UNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
396 if ! [[ -d $UNPACKDIR ]]; then
397 rm unpacked
398 tar -xvf $BOOTPACKG > unpacked
399 UNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
400 fi
401else
402 tar -xvf $BOOTPACKG > unpacked
403 UNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
404fi
405cd $UNPACKDIR
406</xsl:text>
407 </xsl:if>
408 <xsl:apply-templates select='.'/>
409 <xsl:if test="preceding-sibling::para[1]/xref[@linkend='bootscripts']">
410 <xsl:text>
411popd</xsl:text>
412 </xsl:if>
413 <xsl:text>&#xA;</xsl:text>
414 </xsl:template>
415
416 <xsl:template match="para/command">
417 <xsl:if test="(contains(string(),'test') or
418 contains(string(),'check'))">
419 <xsl:text>#</xsl:text>
420 <xsl:value-of select="substring-before(string(),'make ')"/>
421 <xsl:text>make </xsl:text>
422 <xsl:if test="not(contains(string(),'-k'))">
423 <xsl:text>-k </xsl:text>
424 </xsl:if>
425 <xsl:value-of select="substring-after(string(),'make ')"/>
426 <xsl:text> || true&#xA;</xsl:text>
427 </xsl:if>
428 </xsl:template>
429
430 <xsl:template match="userinput">
431 <xsl:apply-templates/>
432 </xsl:template>
433
434 <xsl:template match="text()" mode="root">
435 <xsl:call-template name="output-root">
436 <xsl:with-param name="out-string" select="string()"/>
437 </xsl:call-template>
438 </xsl:template>
439
440 <xsl:template name="output-root">
441 <xsl:param name="out-string" select="''"/>
442 <xsl:choose>
443 <xsl:when test="contains($out-string,'make ')">
444 <xsl:call-template name="output-root">
445 <xsl:with-param name="out-string"
446 select="substring-before($out-string,'make ')"/>
447 </xsl:call-template>
448 <xsl:text>make -j1 </xsl:text>
449 <xsl:call-template name="output-root">
450 <xsl:with-param name="out-string"
451 select="substring-after($out-string,'make ')"/>
452 </xsl:call-template>
453 </xsl:when>
454 <xsl:when test="contains($out-string,'$') and $sudo = 'y'">
455 <xsl:call-template name="output-root">
456 <xsl:with-param name="out-string"
457 select="substring-before($out-string,'$')"/>
458 </xsl:call-template>
459 <xsl:text>\$</xsl:text>
460 <xsl:call-template name="output-root">
461 <xsl:with-param name="out-string"
462 select="substring-after($out-string,'$')"/>
463 </xsl:call-template>
464 </xsl:when>
465 <xsl:when test="contains($out-string,'`') and $sudo = 'y'">
466 <xsl:call-template name="output-root">
467 <xsl:with-param name="out-string"
468 select="substring-before($out-string,'`')"/>
469 </xsl:call-template>
470 <xsl:text>\`</xsl:text>
471 <xsl:call-template name="output-root">
472 <xsl:with-param name="out-string"
473 select="substring-after($out-string,'`')"/>
474 </xsl:call-template>
475 </xsl:when>
476 <xsl:when test="contains($out-string,'\') and $sudo = 'y'">
477 <xsl:call-template name="output-root">
478 <xsl:with-param name="out-string"
479 select="substring-before($out-string,'\')"/>
480 </xsl:call-template>
481 <xsl:text>\\</xsl:text>
482 <xsl:call-template name="output-root">
483 <xsl:with-param name="out-string"
484 select="substring-after($out-string,'\')"/>
485 </xsl:call-template>
486 </xsl:when>
487 <xsl:otherwise>
488 <xsl:value-of select="$out-string"/>
489 </xsl:otherwise>
490 </xsl:choose>
491 </xsl:template>
492
493 <xsl:template match="replaceable">
494 <xsl:text>**EDITME</xsl:text>
495 <xsl:apply-templates/>
496 <xsl:text>EDITME**</xsl:text>
497 </xsl:template>
498
499 <xsl:template match="replaceable" mode="root">
500 <xsl:text>**EDITME</xsl:text>
501 <xsl:apply-templates/>
502 <xsl:text>EDITME**</xsl:text>
503 </xsl:template>
504
505</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.