Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

###############################################################################
# CLASS Clearcase::LsCheckoutCmd
# This class inherits from the CCCmpdProc and executes the cleartool command
# "lscheckout" with args "-me -cview -recurse -fmt 'Element:%n,Comment:%Nc\n'"
# to list all files in cwd.
#
# It overrides the CCProcess member to process the resultant output from the 
# command and store information about all files/dirs in and below the pwd.
# 
# It stores as a result of the command for each file/dir
# NAME      = The full path of the file (as defined by CCCmdProc)
# COMMENT   = The comment, if any, associated with this checked out version
###############################################################################

package Clearcase::LsCheckoutCmd;

use strict;
use Cwd qw/ abs_path /;
use DeployUtils::Logger;
use Clearcase::CmdProc;

use vars qw( @ISA );

@ISA = qw( Clearcase::CmdProc );

#==============================================================================
#   Constructor
#==============================================================================
sub new
{
    my $obclass = shift;
    my $class = ref($obclass) || $obclass;
    my $dir = shift;

    LogDebug("Clearcase::LsCheckoutCmd::new Instantiating new object of class $class");
    
    # if no dir of dir is "." the get full absolute path, abs_path returns patch with "/" regardless of platform
    $dir = abs_path(".") if ( ! defined($dir) || $dir eq "." );    
    
    # Call base class's constructor
    my $self = $class->SUPER::new();

    # Add this class's data to the class object
    $self->{_DIR} = $dir;
    
    bless($self, $class);

    # set Clearcase command and arg
    $self->CCcmd("lscheckout");
    $self->CCargs("-me -cview -fmt Element:%n,Comment:%Nc\\n");

    return $self;
}   # new


#==============================================================================
#   CCexecute
# Runs the command on the directory and then recursively
#==============================================================================
sub CCexecute
{
    my $self = shift;
    my $args = $self->CCargs();

    # search directory first
    $self->CCargs("$args -directory $self->{_DIR}");
    $self->SUPER::CCexecute();
    # now serach the contents
    $self->CCargs("$args -recurse $self->{_DIR}");
    $self->SUPER::CCexecute();

}   # CCexecute


#==============================================================================
#   CCprocess overrides base class's CCProcess to process result
#==============================================================================
sub CCprocess
{
    my $self = shift;
    my $line;
    my ($name, $comment) = ( "", "" );
    my $dirRE = $self->{_DIR};

    # Change the search RE for the findDir so that all occurances of "/" in path are changed to "[/\\]" 
    # to match either type of path separator
    $dirRE =~ s|/|\[/\\\\\]|g;

NEXTLINE:
    while ( $line = $self->getline() )
    {
        chomp $line;
        $line =~ s/\r//g;    # remove carraige returns

        # if line starts with element then its a new file so we increment fileNum and store in array
        if ( $line =~ /^Element/ )
        {
            if ( $line =~ /^Element:(.*),Comment:(.*)$/ )
            {
                $name = $1;
                $comment = $2;

                # Remove Dir from path if dir is same as pwd, makes it less clutered
                $name =~ s|$dirRE|\.| if ( $self->CCcwd() =~ m|^$dirRE| );

                # Add name to file list and set it as current element
                $self->addToFileList($name);
                # update comments
                $self->updateCurrentFile("COMMENT", $comment );
            }
        }
        # its a new line of a comment so we append to comment of current file, name does not change and so 
        # will update same file in call below
        else
        {
            $comment .= "\n$line";
            # update comments to new version
            $self->updateCurrentFile("COMMENT", $comment );
        }

    }
}   # process


#==============================================================================
#   printResult prints out the result of the ls command in a formatted output
#==============================================================================
sub printResult
{
    my $self = shift;

    if ( $self->getNumElements() > 0 )
    {
        my ( $element, $name, $comment, $separator, $count );

        print "--------------------- Checked Out Files ---------------------\n";
        format =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$name,                          $comment
~~                              ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $comment
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$separator
.
    
        $separator = "- - - - - - - - - - - - - - -";
    
        $element = $self->getFirstElement();
        $count = 1;
        while ( defined($element) )
        {
            $name       = $element->{NAME};
            $comment    = $element->{COMMENT};
            $separator  = "-------------------------------------------------------------" if ( $count == $self->getNumElements() );
            write;
        
            $element = $self->getNextElement();
            $count++;
        }
    }
    else
    {
        LogNorm("No Checked out Files Found");
    }
}   # printResult



#==============================================================================
#   getFileList
# Returns a references to an array of files filtered by the arg 1 that determines
# whether all files are returned or ones with or without comments
# Filter Arg can be the strings "ALL", "COMMENT", NOCOMMENT"
#==============================================================================
sub getFileList
{
    my $self = shift;
    my $filter = shift;
    my $element;
    my @fList;

    $filter = "ALL" if ( ! defined($filter) || ( $filter =~ /^COMMENT$/i && $filter =~ /^NOCOMMENT$/i) );

    $element = $self->getFirstElement();
    while ( defined($element) )
    {
        if (   $filter =~ /^ALL/i || 
             ( $filter =~ /^COMMENT$/i   && $element->{COMMENT} ne "" ) || 
             ( $filter =~ /^NOCOMMENT$/i && $element->{COMMENT} eq "" ) )
        {
            push(@fList, $element->{NAME});
        }
        $element = $self->getNextElement();
    }

    return \@fList;
}   # getFileList


#==============================================================================
#   countFiles
# Returns a list of 2 numbers that counts the number of checkedout files that
# have comments and those that dont.
#==============================================================================
sub countFiles
{
    my $self = shift;
    my ($comment, $noComment) = ( 0, 0 );
    my $element;

    $element = $self->getFirstElement();
    while ( defined($element) )
    {
        if ( $element->{COMMENT} eq "" )
        {
            $noComment++;
        }
        else
        {
            $comment++;
        }
        $element = $self->getNextElement();
    }

    return ($comment, $noComment);
}   # countFiles

1;