File: examples-index-gen.pl

package info (click to toggle)
systemtap 5.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,964 kB
  • sloc: cpp: 80,838; ansic: 54,757; xml: 49,725; exp: 43,665; sh: 11,527; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (345 lines) | stat: -rwxr-xr-x 9,929 bytes parent folder | download | duplicates (5)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#! /usr/bin/perl
# Generates index files from examples .meta file info.
# Copyright (C) 2008-2015 Red Hat Inc.
#
# This file is part of systemtap, and is free software.  You can
# redistribute it and/or modify it under the terms of the GNU General
# Public License (GPL); either version 2, or (at your option) any
# later version.

use strict;
use warnings;

use Cwd 'abs_path';
use File::Copy;
use File::Find;
use File::Path;
use Text::Wrap;
use HTML::Entities;
use DBI;

my $inputdir;
if ($#ARGV >= 0) {
    $inputdir = $ARGV[0];
} else {
    $inputdir = ".";
}
$inputdir = abs_path($inputdir);

# get current stap version
open(PIPE, " $inputdir/../../configure -V |") or die ("can't find stap version");
my $version_line = <PIPE>;
$version_line =~ /systemtap configure (.*)/ or die ("can't parse configure -V");
my $version = "For systemtap version ${1}.";
close(PIPE);

my $outputdir;
if ($#ARGV >= 1) {
    $outputdir = $ARGV[1];
} else {
    $outputdir = $inputdir;
}
$outputdir = abs_path($outputdir);

our $driver   = "SQLite";
our $database = "metadatabase.db";
our $dsn = "DBI:$driver:dbname=$database";
our $dbh = DBI->connect($dsn, "", "", { RaiseError => 1 })
                        or die $DBI::errstr;

my $sqlcmd = $dbh->prepare('DELETE FROM metavirt');
$sqlcmd->execute();

my %scripts = ();
print "Parsing .meta files in $inputdir...\n";
find(\&parse_meta_files, $inputdir);

my $meta;
my $keyword;
my %keywords;

$dbh->disconnect();

# Adds a formatted meta entry to a given file handle as text.
sub add_meta_txt(*;$) {
    my($file,$meta) = @_;

    print $file "$scripts{$meta}{name} - $scripts{$meta}{title}\n";

    # Don't output these, the description mentions all these in general.
    #print $file "output: $scripts{$meta}{output}, ";
    #print $file "exits: $scripts{$meta}{exit}, ";
    #print $file "status: $scripts{$meta}{status}\n";

    print $file "keywords: $scripts{$meta}{keywords}\n";

    $Text::Wrap::columns = 72;
    my $description = wrap('  ', '  ', $scripts{$meta}{description});
    print $file "\n$description\n";

    $Text::Wrap::separator = " \\\n";
    my $usage = wrap('', '  ', $scripts{$meta}{test_installcheck});
    if ($usage =~ /[a-z]/) {
        print $file "\n  # $usage\n";
    }
     $Text::Wrap::separator = "\n";

    print $file "\n\n";
}

# Adds a formatted meta entry to a given file handle as text.
sub add_meta_html(*;$) {
    my($file,$meta) = @_;

    my $name = $scripts{$meta}{name};
    print $file "<a href=\"$name\">$name</a> ";
    print $file "- $scripts{$meta}{title}<br>\n";

    # Don't output these, the description mentions all these in general.
    #print $file "output: $scripts{$meta}{output}, ";
    #print $file "exits: $scripts{$meta}{exit}, ";
    #print $file "status: $scripts{$meta}{status}<br>\n";

    print $file "keywords: ";
    foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
        print $file '<a href="keyword-index.html#' . (uc $keyword) . '">'
            . (uc $keyword) . "</a> ";
    }
    print $file "<br>\n";

    print $file "<p>".encode_entities($scripts{$meta}{description})."</p>";

    my $txt = $name;
    $txt =~ s/.stp$/.txt/;
    if (-e $txt) { # prefer .txt file link
        print $file "<p><i><a href=\"$txt\">sample usage in $txt</i></font>";
    } else {
        $Text::Wrap::separator = " \\\n";
        my $usage = wrap('', '', $scripts{$meta}{test_installcheck});
        $Text::Wrap::separator = "\n";
        if ($usage =~ /[a-z]/) {
            $usage = encode_entities($usage);
            print $file "<p><font size=\"-2\"><pre># $usage</pre></font>";
        }
    }

    print $file "</p>\n";
}

my $HEADER = "SYSTEMTAP EXAMPLES INDEX\n"
    . "(see also keyword-index.txt)\n\n";

my $header_tmpl = "$inputdir/html/html_header.tmpl";
open(TEMPLATE, "<$header_tmpl")
    || die "couldn't open $header_tmpl, $!";
my $HTMLHEADER = do { local $/;  <TEMPLATE> };
close(TEMPLATE);
my $footer_tmpl = "$inputdir/html/html_footer.tmpl";
open(TEMPLATE, "<$footer_tmpl")
    || die "couldn't open $footer_tmpl, $!";
my $HTMLFOOTER = do { local $/;  <TEMPLATE> };
close(TEMPLATE);

# Output full index and collect keywords
my $fullindex = "$outputdir/index.txt";
open (FULLINDEX, ">$fullindex")
    || die "couldn't open $fullindex: $!";
print "Creating $fullindex...\n";
print FULLINDEX $HEADER;
print FULLINDEX "\n$version\n\n";
    
my $fullhtml = "$outputdir/index.html";
open (FULLHTML, ">$fullhtml")
    || die "couldn't open $fullhtml: $!";
print "Creating $fullhtml...\n";
print FULLHTML $HTMLHEADER;
print FULLHTML "<p><em>$version</em></p>";

print FULLHTML "<h2>Best Examples</h2>\n";
print FULLHTML "<ul>\n"; 
foreach $meta (sort keys %scripts) {
    my $isbest = 0;
    foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
	if ($keyword eq "_best") { $isbest = 1; }
    }
    if ($isbest) {
        print FULLHTML "<li><a href=\"#".$scripts{$meta}{name}."\">";
        print FULLHTML $scripts{$meta}{name}." - ".$scripts{$meta}{title};
        print FULLHTML "</a></li>\n";
    }
}
print FULLHTML "</ul>\n\n";
print FULLHTML "<h2>All " . scalar (keys %scripts) . " Examples</h2>\n";
print FULLHTML "<ul>\n";

foreach $meta (sort keys %scripts) {

    add_meta_txt(\*FULLINDEX, $meta);
    print FULLHTML "<li>";
    print FULLHTML "<a name=\"".$scripts{$meta}{name}."\"></a>";
    print FULLHTML "<a href=\"#".$scripts{$meta}{name}."\">&para;</a> ";
    add_meta_html(\*FULLHTML, $meta);
    print FULLHTML "</li>";

    # Collect keywords
    foreach $keyword (split(/ /, $scripts{$meta}{keywords})) {
	if (defined $keywords{$keyword}) {
	    push(@{$keywords{$keyword}}, $meta);
	} else {
	    $keywords{$keyword} = [ $meta ];
	}
    }
}
print FULLHTML "</ul>\n";
print FULLHTML $HTMLFOOTER;
close (FULLINDEX);
close (FULLHTML);


my $KEYHEADER = "SYSTEMTAP EXAMPLES INDEX BY KEYWORD\n"
    . "(see also index.txt)\n\n";

# Output keyword index
my $keyindex = "$outputdir/keyword-index.txt";
open (KEYINDEX, ">$keyindex")
    || die "couldn't open $keyindex: $!";
print "Creating $keyindex...\n";
print KEYINDEX $KEYHEADER;
print KEYINDEX "\n$version\n\n";

my $keyhtml = "$outputdir/keyword-index.html";
open (KEYHTML, ">$keyhtml")
    || die "couldn't open $keyhtml: $!";
print "Creating $keyhtml...\n";
print KEYHTML $HTMLHEADER;
print KEYHTML "<p><em>$version</em></p>";

print KEYHTML "<h2>Examples by Keyword</h2>\n";

# On top link list
print KEYHTML "<p><tt>";
foreach $keyword (sort keys %keywords) {
    print KEYHTML '<a href="#' . (uc $keyword) . '">'
		  . (uc $keyword)
                  . "(". (scalar keys @{$keywords{$keyword}}). ")</a> ";
}
print KEYHTML "</tt></p>\n";

foreach $keyword (sort keys %keywords) {
    print KEYINDEX "= " . (uc $keyword) . " =\n\n";
    print KEYHTML "<h3>" . '<a name="' . (uc $keyword) . '">'
                  . "<a href=\"#". (uc $keyword) ."\">&para;</a> "
		  . (uc $keyword) . "</a></h3>\n";
    print KEYHTML "<ul>\n";

    foreach $meta (sort @{$keywords{$keyword}}) {
	add_meta_txt(\*KEYINDEX,$meta);
        print KEYHTML "<li>";
	add_meta_html(\*KEYHTML,$meta);
        print KEYHTML "</li>";
    }
    print KEYHTML "</ul>\n";
}
print KEYHTML $HTMLFOOTER;
close (KEYINDEX);
close (KEYHTML);

my @supportfiles
    = ("html/systemtapcorner.gif",
       "html/systemtap.css",
       "html/systemtaplogo.png",
       "README");
if ($inputdir ne $outputdir) {
    my $file;
    print "Copying support files...\n";
    if (! -d "$outputdir/html") {
	mkpath("$outputdir/html", 1, 0711);
    }
    foreach $file (@supportfiles) {
	my $orig = "$inputdir/$file";
	my $dest = "$outputdir/$file";
	print "Copying $file to $dest...\n";
	copy($orig, $dest) or die "$file cannot be copied to $dest, $!";
    }
}

sub parse_meta_files {
    my $file = $_;
    my $filename = $File::Find::name;

    my $sqlcmd = $dbh->prepare('INSERT INTO metavirt (title, name, keywords, description, path) VALUES (?,?,?,?,?)');

    if (-f $file && $file =~ /\.meta$/) {
	open FILE, $file or die "couldn't open '$file': $!\n";

	print "Parsing $filename...\n";
	
	my $title;
	my $name;
	my $keywords;
	my $status;
	my $exit;
	my $output;
	my $description;
	my $test_installcheck;
	while (<FILE>) {
	    if (/^title: (.*)/) { $title = $1; }
	    if (/^name: (.*)/) { $name = $1; }
	    if (/^keywords: (.*)/) { $keywords = $1; }
	    if (/^status: (.*)/) { $status = $1; }
	    if (/^exit: (.*)/) { $exit = $1; }
	    if (/^output: (.*)/) { $output = $1; }
	    if (/^description: (.*)/) { $description = $1; }
	    if (/^test_installcheck: (.*)/) { $test_installcheck = $1; }
	}
	close FILE;

	# Remove extra whitespace
	$keywords =~ s/^\s*//;
	$keywords =~ s/\s*$//;

	# The subdir without the inputdir prefix, nor any slashes.
	my $subdir = substr $File::Find::dir, (length $inputdir);
	$subdir =~ s/^\///;

        $sqlcmd->execute($title,$name,$keywords,$description,$subdir);

	if ($subdir ne "") {
	    $name = "$subdir/$name";
	}

	my $script = {
	    name => $name,
	    title => $title,
	    keywords => $keywords,
	    status => $status,
	    exit => $exit,
	    output => $output,
	    description => $description,
	    test_installcheck => $test_installcheck
	};

	# chop off the search dir prefix.
	$inputdir =~ s/\/$//;
	$meta = substr $filename, (length $inputdir) + 1;
	$scripts{$meta} = $script;

	# Put .stp script in output dir if necessary and create
	# subdirs if they don't exist yet.
	if ($inputdir ne $outputdir) {
	    # The subdir without the inputdir prefix, nor any slashes.
	    my $destdir = substr $File::Find::dir, (length $inputdir);
	    $destdir =~ s/^\///;
	    if ($subdir ne "") {
		if (! -d "$outputdir/$subdir") {
		    mkpath("$outputdir/$subdir", 1, 0711);
		}
	    }
	    my $orig = substr $name, (length $subdir);
	    $orig =~ s/^\///;
	    my $dest = "$outputdir/$name";
	    print "Copying $orig to $dest...\n";
	    copy($orig, $dest) or die "$orig cannot be copied to $dest, $!";
	}
    }
}