Debian Codegraph @

testsuite/systemtap.examples/examples-index-gen.pl

raw
1 #! /usr/bin/perl
2 # Generates index files from examples .meta file info.
3 # Copyright (C) 2008-2015 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 Cwd 'abs_path';
14 use File::Copy;
15 use File::Find;
16 use File::Path;
17 use Text::Wrap;
18 use HTML::Entities;
19 use DBI;
20
21 my $inputdir;
22 if ($#ARGV >= 0) {
23 $inputdir = $ARGV[0];
24 } else {
25 $inputdir = ".";
26 }
27 $inputdir = abs_path($inputdir);
28
29 # get current stap version
30 open(PIPE, " $inputdir/../../configure -V |") or die ("can't find stap version");
31 my $version_line = <PIPE>;
32 $version_line =~ /systemtap configure (.*)/ or die ("can't parse configure -V");
33 my $version = "For systemtap version ${1}.";
34 close(PIPE);
35
36 my $outputdir;
37 if ($#ARGV >= 1) {
38 $outputdir = $ARGV[1];
39 } else {
40 $outputdir = $inputdir;
41 }
42 $outputdir = abs_path($outputdir);
43
44 our $driver = "SQLite";
45 our $database = "metadatabase.db";
46 our $dsn = "DBI:$driver:dbname=$database";
47 our $dbh = DBI->connect($dsn, "", "", { RaiseError => 1 })
48 or die $DBI::errstr;
49
50 my $sqlcmd = $dbh->prepare('DELETE FROM metavirt');
51 $sqlcmd->execute();
52
53 my %scripts = ();
54 print "Parsing .meta files in $inputdir...\n";
55 find(\&parse_meta_files, $inputdir);
56
57 my $meta;
58 my $keyword;
59 my %keywords;
60
61 $dbh->disconnect();
62
63 # Adds a formatted meta entry to a given file handle as text.
64 sub add_meta_txt(*;$) {
65 my($file,$meta) = @_;
66
67 print $file "$scripts{$meta}{name} - $scripts{$meta}{title}\n";
68
69 # Don't output these, the description mentions all these in general.
70 #print $file "output: $scripts{$meta}{output}, ";
71 #print $file "exits: $scripts{$meta}{exit}, ";
72 #print $file "status: $scripts{$meta}{status}\n";
73
74 print $file "keywords: $scripts{$meta}{keywords}\n";
75
76 $Text::Wrap::columns = 72;
77 my $description = wrap(' ', ' ', $scripts{$meta}{description});
78 print $file "\n$description\n";
79
80 $Text::Wrap::separator = " \\\n";
81 my $usage = wrap('', ' ', $scripts{$meta}{test_installcheck});
82 if ($usage =~ /[a-z]/) {
83 print $file "\n # $usage\n";
84 }
85 $Text::Wrap::separator = "\n";
86
87 print $file "\n\n";
88 }
89
90 # Adds a formatted meta entry to a given file handle as text.
91 sub add_meta_html(*;$) {
92 my($file,$meta) = @_;
93
94 my $name = $scripts{$meta}{name};
95 print $file "<a href=\"$name\">$name</a> ";
96 print $file "- $scripts{$meta}{title}<br>\n";
97
98 # Don't output these, the description mentions all these in general.
99 #print $file "output: $scripts{$meta}{output}, ";
100 #print $file "exits: $scripts{$meta}{exit}, ";
101 #print $file "status: $scripts{$meta}{status}<br>\n";
102
103 print $file "keywords: ";
104 foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
105 print $file '<a href="keyword-index.html#' . (uc $keyword) . '">'
106 . (uc $keyword) . "</a> ";
107 }
108 print $file "<br>\n";
109
110 print $file "<p>".encode_entities($scripts{$meta}{description})."</p>";
111
112 my $txt = $name;
113 $txt =~ s/.stp$/.txt/;
114 if (-e $txt) { # prefer .txt file link
115 print $file "<p><i><a href=\"$txt\">sample usage in $txt</i></font>";
116 } else {
117 $Text::Wrap::separator = " \\\n";
118 my $usage = wrap('', '', $scripts{$meta}{test_installcheck});
119 $Text::Wrap::separator = "\n";
120 if ($usage =~ /[a-z]/) {
121 $usage = encode_entities($usage);
122 print $file "<p><font size=\"-2\"><pre># $usage</pre></font>";
123 }
124 }
125
126 print $file "</p>\n";
127 }
128
129 my $HEADER = "SYSTEMTAP EXAMPLES INDEX\n"
130 . "(see also keyword-index.txt)\n\n";
131
132 my $header_tmpl = "$inputdir/html/html_header.tmpl";
133 open(TEMPLATE, "<$header_tmpl")
134 || die "couldn't open $header_tmpl, $!";
135 my $HTMLHEADER = do { local $/; <TEMPLATE> };
136 close(TEMPLATE);
137 my $footer_tmpl = "$inputdir/html/html_footer.tmpl";
138 open(TEMPLATE, "<$footer_tmpl")
139 || die "couldn't open $footer_tmpl, $!";
140 my $HTMLFOOTER = do { local $/; <TEMPLATE> };
141 close(TEMPLATE);
142
143 # Output full index and collect keywords
144 my $fullindex = "$outputdir/index.txt";
145 open (FULLINDEX, ">$fullindex")
146 || die "couldn't open $fullindex: $!";
147 print "Creating $fullindex...\n";
148 print FULLINDEX $HEADER;
149 print FULLINDEX "\n$version\n\n";
150
151 my $fullhtml = "$outputdir/index.html";
152 open (FULLHTML, ">$fullhtml")
153 || die "couldn't open $fullhtml: $!";
154 print "Creating $fullhtml...\n";
155 print FULLHTML $HTMLHEADER;
156 print FULLHTML "<p><em>$version</em></p>";
157
158 print FULLHTML "<h2>Best Examples</h2>\n";
159 print FULLHTML "<ul>\n";
160 foreach $meta (sort keys %scripts) {
161 my $isbest = 0;
162 foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
163 if ($keyword eq "_best") { $isbest = 1; }
164 }
165 if ($isbest) {
166 print FULLHTML "<li><a href=\"#".$scripts{$meta}{name}."\">";
167 print FULLHTML $scripts{$meta}{name}." - ".$scripts{$meta}{title};
168 print FULLHTML "</a></li>\n";
169 }
170 }
171 print FULLHTML "</ul>\n\n";
172 print FULLHTML "<h2>All " . scalar (keys %scripts) . " Examples</h2>\n";
173 print FULLHTML "<ul>\n";
174
175 foreach $meta (sort keys %scripts) {
176
177 add_meta_txt(\*FULLINDEX, $meta);
178 print FULLHTML "<li>";
179 print FULLHTML "<a name=\"".$scripts{$meta}{name}."\"></a>";
180 print FULLHTML "<a href=\"#".$scripts{$meta}{name}."\">&para;</a> ";
181 add_meta_html(\*FULLHTML, $meta);
182 print FULLHTML "</li>";
183
184 # Collect keywords
185 foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
186 if (defined $keywords{$keyword}) {
187 push(@{$keywords{$keyword}}, $meta);
188 } else {
189 $keywords{$keyword} = [ $meta ];
190 }
191 }
192 }
193 print FULLHTML "</ul>\n";
194 print FULLHTML $HTMLFOOTER;
195 close (FULLINDEX);
196 close (FULLHTML);
197
198
199 my $KEYHEADER = "SYSTEMTAP EXAMPLES INDEX BY KEYWORD\n"
200 . "(see also index.txt)\n\n";
201
202 # Output keyword index
203 my $keyindex = "$outputdir/keyword-index.txt";
204 open (KEYINDEX, ">$keyindex")
205 || die "couldn't open $keyindex: $!";
206 print "Creating $keyindex...\n";
207 print KEYINDEX $KEYHEADER;
208 print KEYINDEX "\n$version\n\n";
209
210 my $keyhtml = "$outputdir/keyword-index.html";
211 open (KEYHTML, ">$keyhtml")
212 || die "couldn't open $keyhtml: $!";
213 print "Creating $keyhtml...\n";
214 print KEYHTML $HTMLHEADER;
215 print KEYHTML "<p><em>$version</em></p>";
216
217 print KEYHTML "<h2>Examples by Keyword</h2>\n";
218
219 # On top link list
220 print KEYHTML "<p><tt>";
221 foreach $keyword (sort keys %keywords) {
222 print KEYHTML '<a href="#' . (uc $keyword) . '">'
223 . (uc $keyword)
224 . "(". (scalar keys @{$keywords{$keyword}}). ")</a> ";
225 }
226 print KEYHTML "</tt></p>\n";
227
228 foreach $keyword (sort keys %keywords) {
229 print KEYINDEX "= " . (uc $keyword) . " =\n\n";
230 print KEYHTML "<h3>" . '<a name="' . (uc $keyword) . '">'
231 . "<a href=\"#". (uc $keyword) ."\">&para;</a> "
232 . (uc $keyword) . "</a></h3>\n";
233 print KEYHTML "<ul>\n";
234
235 foreach $meta (sort @{$keywords{$keyword}}) {
236 add_meta_txt(\*KEYINDEX,$meta);
237 print KEYHTML "<li>";
238 add_meta_html(\*KEYHTML,$meta);
239 print KEYHTML "</li>";
240 }
241 print KEYHTML "</ul>\n";
242 }
243 print KEYHTML $HTMLFOOTER;
244 close (KEYINDEX);
245 close (KEYHTML);
246
247 my @supportfiles
248 = ("html/systemtapcorner.gif",
249 "html/systemtap.css",
250 "html/systemtaplogo.png",
251 "README");
252 if ($inputdir ne $outputdir) {
253 my $file;
254 print "Copying support files...\n";
255 if (! -d "$outputdir/html") {
256 mkpath("$outputdir/html", 1, 0711);
257 }
258 foreach $file (@supportfiles) {
259 my $orig = "$inputdir/$file";
260 my $dest = "$outputdir/$file";
261 print "Copying $file to $dest...\n";
262 copy($orig, $dest) or die "$file cannot be copied to $dest, $!";
263 }
264 }
265
266 sub parse_meta_files {
267 my $file = $_;
268 my $filename = $File::Find::name;
269
270 my $sqlcmd = $dbh->prepare('INSERT INTO metavirt (title, name, keywords, description, path) VALUES (?,?,?,?,?)');
271
272 if (-f $file && $file =~ /\.meta$/) {
273 open FILE, $file or die "couldn't open '$file': $!\n";
274
275 print "Parsing $filename...\n";
276
277 my $title;
278 my $name;
279 my $keywords;
280 my $status;
281 my $exit;
282 my $output;
283 my $description;
284 my $test_installcheck;
285 while (<FILE>) {
286 if (/^title: (.*)/) { $title = $1; }
287 if (/^name: (.*)/) { $name = $1; }
288 if (/^keywords: (.*)/) { $keywords = $1; }
289 if (/^status: (.*)/) { $status = $1; }
290 if (/^exit: (.*)/) { $exit = $1; }
291 if (/^output: (.*)/) { $output = $1; }
292 if (/^description: (.*)/) { $description = $1; }
293 if (/^test_installcheck: (.*)/) { $test_installcheck = $1; }
294 }
295 close FILE;
296
297 # Remove extra whitespace
298 $keywords =~ s/^\s*//;
299 $keywords =~ s/\s*$//;
300
301 # The subdir without the inputdir prefix, nor any slashes.
302 my $subdir = substr $File::Find::dir, (length $inputdir);
303 $subdir =~ s/^\///;
304
305 $sqlcmd->execute($title,$name,$keywords,$description,$subdir);
306
307 if ($subdir ne "") {
308 $name = "$subdir/$name";
309 }
310
311 my $script = {
312 name => $name,
313 title => $title,
314 keywords => $keywords,
315 status => $status,
316 exit => $exit,
317 output => $output,
318 description => $description,
319 test_installcheck => $test_installcheck
320 };
321
322 # chop off the search dir prefix.
323 $inputdir =~ s/\/$//;
324 $meta = substr $filename, (length $inputdir) + 1;
325 $scripts{$meta} = $script;
326
327 # Put .stp script in output dir if necessary and create
328 # subdirs if they don't exist yet.
329 if ($inputdir ne $outputdir) {
330 # The subdir without the inputdir prefix, nor any slashes.
331 my $destdir = substr $File::Find::dir, (length $inputdir);
332 $destdir =~ s/^\///;
333 if ($subdir ne "") {
334 if (! -d "$outputdir/$subdir") {
335 mkpath("$outputdir/$subdir", 1, 0711);
336 }
337 }
338 my $orig = substr $name, (length $subdir);
339 $orig =~ s/^\///;
340 my $dest = "$outputdir/$name";
341 print "Copying $orig to $dest...\n";
342 copy($orig, $dest) or die "$orig cannot be copied to $dest, $!";
343 }
344 }
345 }