[interchange-cvs] interchange - heins modified scripts/findtags.PL

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sat Aug 10 13:35:00 2002


User:      heins
Date:      2002-08-10 17:34:03 GMT
Added:     scripts  findtags.PL
Log:
* New findtags.PL that finds tags used in catalogs (and the UI). Help is
  in usage() message.

Revision  Changes    Path
1.1                  interchange/scripts/findtags.PL


rev 1.1, prev_rev 1.0
Index: findtags.PL
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
#!/usr/bin/perl
##!~_~perlpath~_~
#
# Interchange session expiration for all catalogs
#
# $Id: findtags.PL,v 1.1 2002/08/10 17:34:03 mheins Exp $
#
# Copyright (C) 1996-2001 Red Hat, Inc. <interchange@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

use lib '/work/interchange/lib';
#use lib '~_~INSTALLPRIVLIB~_~';
use lib '/work/interchange';
#use lib '~_~INSTALLARCHLIB~_~';

use strict;


BEGIN {
	($Global::VendRoot =3D $ENV{MINIVEND_ROOT})
		if defined $ENV{MINIVEND_ROOT};

	$Global::VendRoot =3D $Global::VendRoot || '/work/interchange';
#	$Global::VendRoot =3D $Global::VendRoot || '~_~INSTALLARCHLIB~_~';

	if(-f "$Global::VendRoot/interchange.cfg") {
		$Global::ExeName =3D 'interchange';
		$Global::ConfigFile =3D 'interchange.cfg';
	}
	elsif(-f "$Global::VendRoot/minivend.cfg") {
		$Global::ExeName =3D 'minivend';
		$Global::ConfigFile =3D 'minivend.cfg';
	}
	elsif(-f "$Global::VendRoot/interchange.cfg.dist") {
		$Global::ExeName =3D 'interchange';
		$Global::ConfigFile =3D 'interchange.cfg';
	}
}

### END CONFIGURATION VARIABLES

my $prog =3D $0;
$prog =3D~ s:.*/::;

my $USAGE =3D <<EOF;
usage: $prog [-r] [-f file]

    -a                  Look in all catalogs
    -c CAT              Only look in catalog CAT
    -d "dir1 dir2 .."   Look in dirs
    -f                  Alternate interchange.cfg file
    -h                  Print this message and exit
    -n                  Don't report SystemTag
    -t                  "TagInclude" directive output
    -u                  Report unseen tags
    -v                  Slightly verbose, report directories scanned
    -x "dirs"           Don't look for tags in dir (default "session tmp")

Find tags in Interchange catalogs and directories -- intended to develop
a list for TagInclude.

WARNING: This is not 100%, for developing tag names from Variable definitio=
ns
and other sources can fool it. If you include all directories, make sure you
don't include documentation files or the usertags themselves.

With the standard distribution, this should find just about all
tags needed for foundation:

	findtags -a -d lib/UI

To develop a TagInclude statement which excludes unused tags, try:

	findtags -a -d lib/UI -t -u

If you don't want to use the UI, then do:

	findtags -a -u -t=20

EOF

use Vend::Config;

$Vend::ExternalProgram =3D 1;
$Vend::Quiet =3D 1;

use Getopt::Std;
use vars qw/
	$opt_a
	$opt_c
	$opt_d
	$opt_f
	$opt_h
	$opt_n
	$opt_t
	$opt_u
	$opt_v
	$opt_x
/;=20

if($ARGV[0] eq '--help') {
	print $USAGE;
	exit 2;
}

getopts('ac:d:f:hntuvx') or die "$@\n$USAGE\n";

if($opt_h) {
	print $USAGE;
	exit 2;
}

sub logGlobal { shift(@_) if ref $_[0]; printf @_; print "\n" }
sub logError { }
sub logDebug { }

my $flag =3D '';

if($opt_f) {
	$Global::ConfigFile =3D $opt_f;
	$flag .=3D qq{ -f "$opt_f"};
}
else {
	$Global::ConfigFile =3D "$Global::VendRoot/$Global::ConfigFile";
}

# Parse the interchange.cfg file to look for script/catalog info
# but don't read in the core tags
$Vend::ControllingInterchange =3D 1;
chdir $Global::VendRoot or die "Couldn't change to $Global::VendRoot: $!\n";
global_config();

my @cats;

if($opt_a) {
	while( my($name, $cat) =3D each %Global::Catalog ) {
		next if $cat->{base};
		push @cats, $name;
	}
}
elsif($opt_c) {
	@cats =3D split /[\s,]+/, $opt_c;
}

my @exclude =3D qw/session tmp/;
my %exclude;

if($opt_x) {
	@exclude =3D split /[\s,]+/, $opt_x;
}

@exclude{@exclude} =3D @exclude;

my @dirs;

for(@$Global::TagDir) {
	push @dirs, glob("$_/*");
}

#warn "Dirs are " . join (" ", @dirs) . "\n";

if ($opt_n) {
	@dirs =3D grep $_ !~ '/SystemTag$', @dirs;
}

my %tag;

use File::Find;

GETTAGS: {
	my @tags;
	my $wanted =3D sub {
		return unless -f $_;
		return unless /^(\w[-\w]*)\.[a-z_]*tag$/;
		my $tname =3D $1;
		$tname =3D lc $tname;
		$tname =3D~ tr/-/_/;
		push @tags, $tname;
	};
	File::Find::find($wanted, @dirs);
	my %seen;
	@tags =3D grep !$seen{$_}++, @tags;
	@tag{@tags} =3D @tags;
}

my @targdirs;

if($opt_d) {
	my @d =3D split /[\s,]+/, $opt_d;
	for(@d) {
warn "Doing directory $_\n" if $opt_v;
		push @targdirs, $_;
	}
}

my @tags;

foreach my $catname (@cats) {
warn "Doing catalog $catname\n" if $opt_v;
	my $dir =3D $Global::Catalog{$catname}->{dir};
	if(! $dir) {
		warn errmsg("Unknown catalog '%s', skipping.\n", $catname);
	}
	push @targdirs, $dir;
}

if(! @targdirs) {
	die "no directories to scan!\n";
}

foreach my $dir (@targdirs) {
	chdir $dir
		or die errmsg("chdir to directory %s: $!\n", $dir);
	my @files;
	my @binaries;
	my $wanted =3D sub {
		return unless -f $_;
		if (-B $_) {
			push @binaries, $File::Find::name;
		}
		push @files, $File::Find::name;
	};

	my @d =3D glob('*');
	@d =3D grep !$exclude{$_}, @d;
	File::Find::find($wanted, @d);

	undef $/;
	foreach my $f (@files) {
#print "Checking file $f\n";
		open IT, "< $f"			or die errmsg("open %s: %s", $f, $!);
		$_ =3D <IT>;
		close IT				or die errmsg("close %s: %s", $f, $!);
		while (m{(?:\[(\w[-\w]*)[\s\]]|\$Tag->(\w+))}g) {
			my $tmp =3D $1 || $2;
			next if $tmp =3D~ /^\d+$/;
			$tmp =3D~ s/-/_/g;
			$tmp =3D lc $tmp;
			next unless $tag{$tmp};
			if($opt_u) {
				delete $tag{$tmp};
			}
			else {
				push @tags, $tmp;
			}
		}
	}
}

my %seen;

if($opt_u) {
	@tags =3D keys %tag;
	if($opt_t) {
		@tags =3D map { "!$_" } @tags;
	}
}
else {
	@tags =3D grep !$seen{$_}++, @tags;
}

@tags =3D grep /\w/, @tags;
@tags =3D sort @tags;

if($opt_t) {
	print "TagInclude <<EOTI\n\t";
	print join("\n\t", sort @tags);
	print "\nEOTI\n";
}
else {
	print join("\n", sort @tags);
}
__END__