File: verify_index.pl

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (167 lines) | stat: -rw-r--r-- 5,698 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w

#
# Script to verify that the index.html is in good shape.
# This will verify that the entries:
#   1) Are in order per section
#   2) Exist on disk if in the index.html
#   3) Are in the index.html if they exist on disk
#
# Run it from the contrib directory.
#
# tyranix [ tyranix at gmail]
#
#
# verify_index.pl is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#

use strict;
use warnings "all";
use diagnostics;

sub report_error($);

my $return_code = 0;   # Darcs needs a return code for a test suite.
my $line_count = 1;    # Line count inside index.html

{
    open(INDEX, "index.html") || die qq[Could not open "index.html": $!];

    my %index_files = ();        # Files found in "index.html"
    my %script_files = ();       # Files found in the directory
    my %old_ignored_files = ();  # Files marked for ion2 only
    my $section_counter = 1;     # Section number (not same as line number)

    for (my $line = <INDEX>; $line; $line = <INDEX>, $line_count++) {
        chomp($line);

        if ($line =~ m/^\s*<dt>\s*<a\s*href="([^\"]+)">([^<]+)<\/a>/) {
            my ($url, $name) = ($1, $2);
            my ($directory, $filename) = $url =~ m,(.+)/([^/]+),;

            if (!defined($directory) || !defined($filename)) {
                report_error("Malformed entry on line $line_count\n");
            }

            if ($filename ne $name) {
                report_error(qq["$name" should be named "$filename"]);
            }

            # Reset the section_counter and setup the hashes.
            if (!exists($index_files{$directory})) {
                $section_counter = 1;
                $index_files{$directory} = {};
                $script_files{$directory} = {};

                # Add all of the actual files so we can compare the two hashes.
                opendir(FILES, $directory)
                    || die qq[Could not open "$directory": $!];

                while (my $entry = readdir(FILES)) {
                    if ($entry !~ /^\./ && -f "$directory/$entry") {
                        ${$script_files{$directory}}{$entry} = 1;
                    }
                }
                closedir(FILES) || die qq[Could not close "$directory": $!];
            }

            if (exists(${$index_files{$directory}}{$filename})) {
                report_error("Duplicate entry $directory/$filename");
            }

            # Add this file to the hash of this directory for later use.
            ${$index_files{$directory}}{$filename} = $section_counter++;
        } elsif ($line =~ m/^\s*<dt>\s*<strike>\s*<a\s*href="([^\"]+)">([^<]+)<\/a>/) {
            my ($url, $name) = ($1, $2);
            my ($directory, $filename) = $url =~ m,(.+)/([^/]+),;

            if (!defined($directory) || !defined($filename)) {
                report_error("Malformed entry on line $line_count\n");
            }

            if ($filename ne $name) {
                report_error(qq["$name" should be named "$filename"]);
            }

            if (!exists($old_ignored_files{$directory})) {
                $old_ignored_files{$directory} = {};
            }

            if (exists(${$old_ignored_files{$directory}}{$filename})) {
                report_error("Duplicate entry $directory/$filename");
            }

            ${$old_ignored_files{$directory}}{$filename} = 1;
        } elsif ($line =~ m/^\s*<dt>/) {
            # This is a dt entry but it's invalid.
            report_error("Invalid line: $line");
        }
    }
    close(INDEX) || die qq[Could not close "index.html": $!];

    my @out_of_order = ();
    my @not_on_disk = ();

    # Compare the index.html entries to what's on disk.
    for my $dir (keys %index_files) {
        my $count = 1;
        for my $key (sort(keys %{$index_files{$dir}})) {
            if (!exists(${$script_files{$dir}}{$key})) {
                push(@not_on_disk, "$dir/$key");
            } else {
                if ($count != ${$index_files{$dir}}{$key}) {
                    push(@out_of_order, ["$dir/$key", $count, ${$index_files{$dir}}{$key}]);
                }

                # Delete it so we can go through it later and see what wasn't
                # in the html file.
                delete(${$script_files{$dir}}{$key});
            }
            $count++;
        }
    }

    if ($#not_on_disk != -1) {
        $return_code = 1;
        print "Files in the index.html but not on disk:\n";
        for my $key (@not_on_disk) {
            print "\t$key\n";
        }
    }

    if ($#out_of_order != -1) {
        $return_code = 1;
        print  "Files out of order in index.html:\n";
        for my $key (@out_of_order) {
            my ($name, $correct_value, $value_saw) = @{$key};
            print "\t$name \tshould be in spot $correct_value but found in $value_saw\n";
        }
    }

    my $printed_header = undef;
    for my $dir (keys %script_files) {
        for my $key (sort { uc($a) cmp uc($b) } keys %{$script_files{$dir}}) {
            if (!exists(${$old_ignored_files{$dir}}{$key})) {
                if (!defined($printed_header)) {
                    print "Files on the disk but not in index.html:\n";
                    $printed_header = 1;
                    $return_code = 1;
                }
                print "\t$dir/$key\n";
            }
        }
    }

    exit($return_code);
}

sub report_error($)
{
    my ($error) = @_;

    print STDERR qq[$0: Error in "index.html" on line $line_count\n\t$error\n];
    $return_code = 1;
}