Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

###############################################################################
# CLASS ClearCase::IgnoreList
# Class holding files to ignore for CC,  reads file from GBE_TOOLS, HOME & pwd
# This is a singleton class that stores data in an Eponymous hash and as the 
# constructor does not bless self it always refers to the classname which is 
# the same name as the hash ( which is why its an Eponymous Hash)
###############################################################################

package Clearcase::IgnoreList;

use strict;
use DeployUtils::Logger;

our %IgnoreList = ( );

#==============================================================================
#   new
#==============================================================================
sub new
{
    my $self = shift;
    my $ignoreFile = ".ccignore";

    #
    #   This routine assumes that there exists a file called .ccignore
    #   in the same directory as this package. The trick is to determine
    #   that directory. This can be discovered from __FILE__.
    #   __FILE__ should be an an absolute path, but it may not be.
    #   It depends on the PERL5LIB element used to locate the module
    #
    #
   (my $this_dir = __FILE__ ) =~ s~/[^/]*$~~;
    LogDebug("ClearCase::IgnoreList::new Use .ccignore in $this_dir");
    unless ( -f "$this_dir/$ignoreFile" )
    {
        LogError('-x',"ClearCase::IgnoreList::new Local $ignoreFile not found");
        LogError("ClearCase::IgnoreList::new FNF: $this_dir/$ignoreFile");
    }

    my @lookupDirs  = ( $this_dir, $ENV{HOME}, "." );
    my $classobj = $self->_classobj();

    $classobj->{FILELIST} = [ ];

    #always ignore .ccignore file if found
    push(@{$classobj->{FILELIST}}, qr|/\.ccignore$|);

    foreach my $dir ( @lookupDirs )
    {
        if ( -f "$dir/$ignoreFile" )
        {
            LogDebug("ClearCase::IgnoreList::new Processing $dir/$ignoreFile");
            $self->_readFile("$dir/$ignoreFile");
        }
    }
}   # new


#==============================================================================
#   _classobj returns a hard reference to class object ( The Eponymous Hash )
#==============================================================================
sub _classobj
{
    my $self = shift;
    no strict "refs";
    return \%$self;
}   # _classobj
                                

#==============================================================================
#   _readFile
#==============================================================================
sub _readFile
{
    my $self = shift;
    my $file = shift;
    my $classobj = $self->_classobj();

    open(IGFILE, "<$file") || LogError("ClearCase::IgnoreList::_readFile Cannot open $file for reading");

    while ( <IGFILE> )
    {
        chomp;
        
        # skip empty lines or comments
        next if ( /^ *$/ || /^#/ );

        push( @{$classobj->{FILELIST}}, qr|$_| );
        LogDebug("ClearCase::IgnoreList::_readFile Added item RE $classobj->{FILELIST}[$#{$classobj->{FILELIST}}] to ignore list");
    }
}   # _readFile


#==============================================================================
#   ignoreFile returns 1(TRUE) if file is to be ignored
#==============================================================================
sub ignoreFile
{
    my $self = shift;
    my $fullPath = shift;
    my $fixedPath = $fullPath;
    my $classobj = $self->_classobj();

    $fixedPath =~ s|\\|/|g;

    for ( my $i = 0; $i <= $#{$classobj->{FILELIST}}; $i++ )
    {
        if ( $fixedPath =~ m|$classobj->{FILELIST}[$i]| )
        {
            LogDebug("ClearCase::IgnoreList::ignoreFile Ignoring $fixedPath");
            return (1);
        }
    }

    return (0);
}   # ignoreFile


#==============================================================================
#   printIgnoreList prints the list of files to ignore, used for debugging
#==============================================================================
sub printIgnoreList
{
    my $self = shift;
    my $classobj = $self->_classobj();

    # lets dump hash if logging is debug
    if ( getLogLevel() == $LOG_LEVEL_DEBUG )
    {
        for my $file ( sort keys(%{$classobj->{FILELIST}}) )
        {
            LogDebug("ClearCase::IgnoreList::printIgnoreList contains $file");
        }
    }
}   # printIgnoreList


# now we create the class singleton before we do anything else
__PACKAGE__->new();

1;