1#!/usr/bin/env perl -w
2#
3# Copyright (c) 2004 Oliver Eikemeier. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# 1. Redistributions of source code must retain the above copyright notice
10#    this list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# 3. Neither the name of the author nor the names of its contributors may be
17#    used to endorse or promote products derived from this software without
18#    specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31#
32# MAINTAINER=	eik@FreeBSD.org
33#
34# CVSROOT-ports/modules auditing script, based on a shell script by
35# Clement Laforet.
36#
37
38require 5.005;
39use strict;
40use File::Find;
41use Cwd 'abs_path';
42
43my $portsdir = $ENV{PORTSDIR} ? $ENV{PORTSDIR} : '/usr/ports';
44my $cvsroot  = $ENV{CVSROOT}  ? $ENV{CVSROOT}  : '/home/ncvs/CVSROOT-ports';
45my @excludes = $ENV{EXCLUDE}  ? split(' ', $ENV{EXCLUDE}) : ('local', 'rookies');
46
47-d "$portsdir"        or die "Can't find ports tree at $portsdir.\n";
48-f "$cvsroot/modules" or die "Can't read modules file $cvsroot/modules.\n";
49$portsdir = abs_path($portsdir);
50
51my %ports = ('ports' => 1);
52
53my $excludepattern = '(?:'.join('|', 'distfiles', 'packages', @excludes).')';
54
55sub wanted {
56    !-d
57    || (
58      /^CVS$/
59      || $File::Find::name =~ m"^$portsdir/$excludepattern$"os
60      || $File::Find::name =~ m"^$portsdir/[^/]+/pkg$"os
61    )
62      && ($File::Find::prune = 1)
63    || $File::Find::name =~ m"^$portsdir/([^/]+/[^/]+)$"os
64      && ($ports{$1} = undef)
65      && ($File::Find::prune = 1)
66    || $File::Find::name =~ m"^$portsdir/((?:Mk|Templates|Tools)(?:/.+)?)"os
67      && ($ports{$1} = 1)
68    || $File::Find::name =~ m"^$portsdir/([^/]+)$"os
69      && ($ports{$1} = undef);
70}
71
72find(\&wanted, $portsdir);
73
74my %extraneous;
75my %missorted;
76my %illegal;
77
78my $lastmodule = '';
79
80open(MODULES, "$cvsroot/modules");
81while (<MODULES>) {
82    chomp;
83    next if 1 .. /^# !!MERGE!!/;
84    next if /^(#|$)/;
85    my ($module, $portsdir) = split;
86    if ($lastmodule gt $module) {
87        $missorted{$.} = $_;
88    }
89    $lastmodule = $module;
90    next if $portsdir eq 'ports';
91    if ($module !~ /^[a-z\d]/i || $portsdir !~ m"^ports/([^/]+(?:/[^/]+)?)") {
92        $illegal{$.} = $_;
93        next;
94    }
95    if (exists $ports{$1}) {
96        $ports{$1} = $.;
97    }
98    else {
99        $extraneous{$.} = $_;
100    }
101}
102close(MODULES);
103
104my @missing = grep(!defined $ports{$_}, keys %ports);
105
106print "** chkmodules report: **\n"
107  if %extraneous || %illegal || %missorted || @missing;
108
109print join("\n ",
110    "\nModules with illegal names:",
111    map("$_: $illegal{$_}", sort { $a <=> $b } keys %illegal)), "\n"
112  if %illegal;
113
114print join("\n ",
115    "\nModules that are not sorted correctly:",
116    map("$_: $missorted{$_}", sort { $a <=> $b } keys %missorted)), "\n"
117  if %missorted;
118
119print join("\n ",
120    "\nOrphaned entries:",
121    map("$_: $extraneous{$_}", sort { $a <=> $b } keys %extraneous)), "\n"
122  if %extraneous;
123
124print join("\n - ",
125    "\nMissing entries in modules:",
126    sort @missing), "\n"
127  if @missing;
128
129exit %extraneous || %illegal || %missorted || @missing ? 1 : 0;
130