summaryrefslogtreecommitdiffstats
path: root/release/scripts/checkindex.pl
blob: 44612dcbee5c6d4e726ccdbe39d1505f55d0f8bd (plain)
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
#!/usr/bin/perl
# -----------------------------------------------------------------
#  FreeBSD Release Checking Utility - Package Index Check
#
#  This program checks the packages/INDEX file to verify that
#  the index is in the correct format and that every package
#  needed by a release is included on the CD.
#
#  Copyright(c) 2000 BSDi
#  Murray Stokely
# -----------------------------------------------------------------
# 08 Apr 2000
#
# $FreeBSD$
#

use Getopt::Long;

#
# Display the usage instructions
#

sub printHelp {
    print<<end;
usage : checkindex -s <sysinstall src dir> <INDEX>

  This program checks the packages INDEX file to verify that the 
index is correct and that every package needed by sysinstall is
included in the index.

  Options

     -help                Display usage instructions
     -s <src dir>         Specify the sysinstall source directory.  Use
	                  this so to make sure every package referenced
                          in the code is in your INDEX
     -newindex            Generate a new index consisting of only those
                          packages that actually exist in pkgdir/All
     -depends <pkg>       Lists all packages in the index that depend
                          on <pkg>.

end
}

##
## Attempts to find the value of a variable in C code by backtracking
## up the source looking for a previous declaration.
## 
## This is a bit overkill for the purpose of this script,
## stick with grepping for likely packages for now.

sub findAssignment($$) {
	    ## This code deals with the small (5%) number of matches
	    ## in which package_add refers to a variable rather than
	    ## a inline string, so we have to find the value of that
	    ## variable so that we can push it onto the list
#	    my($fileName,$code) = split(/:/,$match);
#	    open(FILE,$fileName) || die "Could not open $fileName : $!\n";
#	    my(@lines) = <FILE>;
#	    my($cnt)  = 1;
#	    my($lineMatch) = 0;
#	    chomp(@lines);
#	    foreach $line (@lines) {
#		$lineMatch = $cnt if ($line eq $code);
#		$cnt++;
#	    }
#	    $code =~ /package_add\((\S+)\)/;
#	    my($varName) = $1;
#	    print STDERR "$lineMatch of $fileName is wierd\n";
#	    print STDERR "Trying to find '$varName'\n";
#	    while ($cnt > 0) {
#		$cnt--;
#	    }


}

##
## Returns a list of all the packages referenced in the sysinstall source
## code
##

sub getPackages($) {
    my($srcDir) = $_[0];
    my(@matches) = `grep package_add $opt_s/*.c`;
    my(@packages);
    foreach $match (@matches) {
	chomp $match;
	next if ($match =~ m|$opt_s/package.c|);
	if ($match =~ /package_add\(\"(\S+)\"\)/) {
	    push(@packages,$1);
	} elsif ($match =~ /package_add\(char/) {
	    # function definition or prototype
	    next;
	} else {
	    # package_add(variable or DEFINE)
	    my(@varMatches) = `grep variable_set2 $opt_s/*.c`;
	    chomp @varMatches;
	    foreach $varMatch (@varMatches) {
		if ($varMatch =~ /variable_set2\(\S+_PACKAGE,\s+\"(\S+)\"/) {
		    push(@packages,$1);
		}
	    }
	}
    }
    @packages;
}


&GetOptions("help","s=s","newindex","depends=s");
if ($opt_help) {
    &printHelp;
} else{
    my ($indexName) = $ARGV[0];
    my ($mistakes) = 0;
    my ($counter)  = 0;
    print STDERR "Packages Referenced :\n---------------------\n";
    open(INDEX,$indexName) || die "Could not open $indexName : $!";
    @index = <INDEX>;
    close(INDEX);

    ## Check to ensure that every file in the index exists physically.
    print STDERR "Check to ensure that every file in the index exists physically\n";
    foreach $line (@index) {
	chomp $line;
	($file,$pathto,$prefix,$comment,$descr,$maint,$cats,$junk,$rdeps,$junk) = split(/\|/,$line,10);
	$DEPENDS{$file} = $rdeps if (-e "All/$file.tgz");
    }

    if ($opt_newindex) {
	foreach $pkg (keys %DEPENDS) {
	    $new = quotemeta $pkg;
	    @lines = grep(/^$new\|/,@index);
	    chomp $lines;
	    ($#lines == 0) || die "Multiple lines for '$pkg' in index!";
	    printf "%s\n",$lines[0];
	}
    } elsif ($opt_depends) {
	foreach $key (keys %DEPENDS) {
	    foreach $dependency (split ' ',$DEPENDS{$key}) {
		if ($opt_depends eq $dependency) {
		    print "$opt_depends is needed by $key\n";
		    $counter++;
		}
	    }
	}
	print "$opt_depends is not needed by any packages in the index!\n"
	    unless ($counter);
    } else {

    ## Check to ensure that all the dependencies are there.
    print "Check to make sure that every dependency of every file exists\n",
  	"in the Index and physically.\n";
    foreach $file (keys %DEPENDS) {
#	print "Checking $file\n";
	foreach $depend (split(' ',$DEPENDS{$file})) {
	    unless (-e "All/$depend.tgz") {
		# instead of a hash counter, make it a hash of arrays
		# where the arrays are the files depended on.
		push @{ $MISSING{$depend} }, $file;
		$mistakes++;
	    }
	}
    }

    ## This makes sure that the index file contains everything
    ## that sysinstall uses.
    if ($opt_s) {
	@packages = getPackages($opt_s);
	foreach $pkg (@packages) {
	    unless (grep(/^$pkg/,@index)) {
		push @{ $MISSING{$pkg} }, "sysinstall";
		$mistakes++;
	    }
	}
    }


    ## If there were mistakes, print out the missing packages.
    if ($mistakes) {
	print "--------------------------------------------------------\n",
	      " Packages Missing : \n",
              "--------------------------------------------------------\n";
	foreach $pkg (keys %MISSING) {
	    @files = @{ $MISSING{$pkg} };
	    print "$pkg (@files)\n";
	}
    } else {
	print "Everything looks good!\n";
    }
}
}
OpenPOWER on IntegriCloud