]> sourceware.org Git - systemtap.git/blob - testsuite/systemtap.examples/examples-index-gen.pl
PR6823: store generated sample index files
[systemtap.git] / testsuite / systemtap.examples / examples-index-gen.pl
1 #! /usr/bin/perl
2 # Generates index files from examples .meta file info.
3 # Copyright (C) 2008 Red Hat Inc.
4 #
5 # This file is part of systemtap, and is free software. You can
6 # redistribute it and/or modify it under the terms of the GNU General
7 # Public License (GPL); either version 2, or (at your option) any
8 # later version.
9
10 use strict;
11 use warnings;
12
13 use File::Copy;
14 use File::Find;
15 use File::Path;
16 use Text::Wrap;
17
18 my $inputdir;
19 if ($#ARGV >= 0) {
20 $inputdir = $ARGV[0];
21 } else {
22 $inputdir = ".";
23 }
24
25 my $outputdir;
26 if ($#ARGV >= 1) {
27 $outputdir = $ARGV[1];
28 } else {
29 $outputdir = $inputdir;
30 }
31
32 my %scripts = ();
33 print "Parsing .meta files in $inputdir...\n";
34 find(\&parse_meta_files, $inputdir);
35
36 my $meta;
37 my $subsystem;
38 my %subsystems;
39 my $keyword;
40 my %keywords;
41
42 # Adds a formatted meta entry to a given file handle as text.
43 sub add_meta_txt(*;$) {
44 my($file,$meta) = @_;
45
46 print $file "$scripts{$meta}{name} - $scripts{$meta}{title}\n";
47
48 print $file "output: $scripts{$meta}{output}, ";
49 print $file "exits: $scripts{$meta}{exit}, ";
50 print $file "status: $scripts{$meta}{status}\n";
51
52 print $file "subsystem: $scripts{$meta}{subsystem}, ";
53 print $file "keywords: $scripts{$meta}{keywords}\n\n";
54
55 $Text::Wrap::columns = 72;
56 my $description = wrap(' ', ' ', $scripts{$meta}{description});
57 print $file "$description\n\n\n";
58 }
59
60 # Adds a formatted meta entry to a given file handle as text.
61 sub add_meta_html(*;$) {
62 my($file,$meta) = @_;
63
64 my $name = $scripts{$meta}{name};
65 print $file "<li><a href=\"$name\">$name</a> ";
66 print $file "- $scripts{$meta}{title}<br>\n";
67
68 print $file "output: $scripts{$meta}{output}, ";
69 print $file "exits: $scripts{$meta}{exit}, ";
70 print $file "status: $scripts{$meta}{status}<br>\n";
71
72 print $file "subsystem: $scripts{$meta}{subsystem}, ";
73 print $file "keywords: $scripts{$meta}{keywords}<br>\n";
74
75 print $file "<p>$scripts{$meta}{description}";
76 print $file "</p></li>\n";
77 }
78
79 my $HEADER = "SYSTEMTAP EXAMPLES INDEX\n"
80 . "(see also subsystem-index.txt, keyword-index.txt)\n\n";
81
82 my $header_tmpl = "$inputdir/html/html_header.tmpl";
83 open(TEMPLATE, "<$header_tmpl")
84 || die "couldn't open $header_tmpl, $!";
85 my $HTMLHEADER = do { local $/; <TEMPLATE> };
86 close(TEMPLATE);
87 my $footer_tmpl = "$inputdir/html/html_footer.tmpl";
88 open(TEMPLATE, "<$footer_tmpl")
89 || die "couldn't open $footer_tmpl, $!";
90 my $HTMLFOOTER = do { local $/; <TEMPLATE> };
91 close(TEMPLATE);
92
93 # Output full index and collect subsystems and keywords
94 my $fullindex = "$outputdir/index.txt";
95 open (FULLINDEX, ">$fullindex")
96 || die "couldn't open $fullindex: $!";
97 print "Creating $fullindex...\n";
98 print FULLINDEX $HEADER;
99
100 my $fullhtml = "$outputdir/index.html";
101 open (FULLHTML, ">$fullhtml")
102 || die "couldn't open $fullhtml: $!";
103 print "Creating $fullhtml...\n";
104 print FULLHTML $HTMLHEADER;
105 print FULLHTML "<h2>All Examples</h2>\n";
106 print FULLHTML "<ul>\n";
107
108 foreach $meta (sort keys %scripts) {
109
110 add_meta_txt(\*FULLINDEX, $meta);
111 add_meta_html(\*FULLHTML, $meta);
112
113 # Collect subsystems
114 foreach $subsystem (split(/ /, $scripts{$meta}{subsystem})) {
115 if (defined $subsystems{$subsystem}) {
116 push(@{$subsystems{$subsystem}}, $meta);
117 } else {
118 $subsystems{$subsystem} = [ $meta ];
119 }
120 }
121
122 # Collect keywords
123 foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
124 if (defined $keywords{$keyword}) {
125 push(@{$keywords{$keyword}}, $meta);
126 } else {
127 $keywords{$keyword} = [ $meta ];
128 }
129 }
130 }
131 print FULLHTML "</ul>\n";
132 print FULLHTML $HTMLFOOTER;
133 close (FULLINDEX);
134 close (FULLHTML);
135
136 my $SUBHEADER = "SYSTEMTAP EXAMPLES INDEX BY SUBSYSTEM\n"
137 . "(see also index.txt, keyword-index.txt)\n\n";
138
139 # Output subsystem index
140 my $subindex = "$outputdir/subsystem-index.txt";
141 open (SUBINDEX, ">$subindex")
142 || die "couldn't open $subindex: $!";
143 print "Creating $subindex...\n";
144 print SUBINDEX $SUBHEADER;
145
146 my $subhtml = "$outputdir/subsystem-index.html";
147 open (SUBHTML, ">$subhtml")
148 || die "couldn't open $subhtml: $!";
149 print "Creating $subhtml...\n";
150 print SUBHTML $HTMLHEADER;
151 print SUBHTML "<h2>Examples by Subsystem</h2>\n";
152
153 foreach $subsystem (sort keys %subsystems) {
154 print SUBINDEX "= " . (uc $subsystem) . " =\n\n";
155 print SUBHTML "<h3>" . (uc $subsystem) . "</h3>\n";
156 print SUBHTML "<ul>\n";
157
158 foreach $meta (sort @{$subsystems{$subsystem}}) {
159 add_meta_txt(\*SUBINDEX,$meta);
160 add_meta_html(\*SUBHTML,$meta);
161 }
162 print SUBHTML "</ul>\n";
163 }
164 print SUBHTML $HTMLFOOTER;
165 close (SUBINDEX);
166 close (SUBHTML);
167
168 my $KEYHEADER = "SYSTEMTAP EXAMPLES INDEX BY KEYWORD\n"
169 . "(see also index.txt, subsystem-index.txt)\n\n";
170
171 # Output subsystem index
172 my $keyindex = "$outputdir/keyword-index.txt";
173 open (KEYINDEX, ">$keyindex")
174 || die "couldn't open $keyindex: $!";
175 print "Creating $keyindex...\n";
176 print KEYINDEX $KEYHEADER;
177
178 my $keyhtml = "$outputdir/keyword-index.html";
179 open (KEYHTML, ">$keyhtml")
180 || die "couldn't open $keyhtml: $!";
181 print "Creating $keyhtml...\n";
182 print KEYHTML $HTMLHEADER;
183 print KEYHTML "<h2>Examples by Keyword</h2>\n";
184
185 foreach $keyword (sort keys %keywords) {
186 print KEYINDEX "= " . (uc $keyword) . " =\n\n";
187 print KEYHTML "<h3>" . (uc $keyword) . "</h3>\n";
188 print KEYHTML "<ul>\n";
189
190 foreach $meta (sort @{$keywords{$keyword}}) {
191 add_meta_txt(\*KEYINDEX,$meta);
192 add_meta_html(\*KEYHTML,$meta);
193 }
194 print KEYHTML "</ul>\n";
195 }
196 print KEYHTML $HTMLFOOTER;
197 close (KEYINDEX);
198 close (KEYHTML);
199
200 my @supportfiles
201 = ("systemtapcorner.gif",
202 "systemtap.css",
203 "systemtaplogo.png");
204 if ($inputdir ne $outputdir) {
205 my $file;
206 print "Copying support files...\n";
207 foreach $file (@supportfiles) {
208 my $orig = "$inputdir/$file";
209 my $dest = "$outputdir/$file";
210 print "Copying $file to $dest...\n";
211 copy("$orig", $dest) or die "$file cannot be copied to $dest, $!";
212 }
213 }
214
215 sub parse_meta_files {
216 my $file = $_;
217 my $filename = $File::Find::name;
218
219 if (-f $file && $file =~ /\.meta$/) {
220 open FILE, $file or die "couldn't open '$file': $!\n";
221
222 print "Parsing $filename...\n";
223
224 my $title;
225 my $name;
226 my $keywords;
227 my $subsystem;
228 my $status;
229 my $exit;
230 my $output;
231 my $description;
232 while (<FILE>) {
233 if (/^title: (.*)/) { $title = $1; }
234 if (/^name: (.*)/) { $name = $1; }
235 if (/^keywords: (.*)/) { $keywords = $1; }
236 if (/^subsystem: (.*)/) { $subsystem = $1; }
237 if (/^status: (.*)/) { $status = $1; }
238 if (/^exit: (.*)/) { $exit = $1; }
239 if (/^output: (.*)/) { $output = $1; }
240 if (/^description: (.*)/) { $description = $1; }
241 }
242 close FILE;
243
244 # Remove extra whitespace
245 $keywords =~ s/^\s*//;
246 $keywords =~ s/\s*$//;
247 $subsystem =~ s/^\s*//;
248 $subsystem =~ s/\s*$//;
249
250 # The subdir without the inputdir prefix, nor any slashes.
251 my $subdir = substr $File::Find::dir, (length $inputdir);
252 $subdir =~ s/^\///;
253 if ($subdir ne "") {
254 $name = "$subdir/$name";
255 }
256
257 my $script = {
258 name => $name,
259 title => $title,
260 keywords => $keywords,
261 subsystem => $subsystem,
262 status => $status,
263 exit => $exit,
264 output => $output,
265 description => $description
266 };
267
268 # chop off the search dir prefix.
269 $inputdir =~ s/\/$//;
270 $meta = substr $filename, (length $inputdir) + 1;
271 $scripts{$meta} = $script;
272
273 # Put .stp script in output dir if necessary and create
274 # subdirs if they don't exist yet.
275 if ($inputdir ne $outputdir) {
276 # The subdir without the inputdir prefix, nor any slashes.
277 my $destdir = substr $File::Find::dir, (length $inputdir);
278 $destdir =~ s/^\///;
279 if ($subdir ne "") {
280 if (! -d "$outputdir/$subdir") {
281 mkpath("$outputdir/$subdir", 1, 0711);
282 }
283 }
284 my $orig = substr $name, (length $subdir);
285 $orig =~ s/^\///;
286 my $dest = "$outputdir/$name";
287 print "Copying $orig to $dest...\n";
288 copy($orig, $dest) or die "$orig cannot be copied to $dest, $!";
289 }
290 }
291 }
This page took 0.113391 seconds and 5 git commands to generate.