Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-release > by-pkgid > a68aa3701b6abd0438ee52ae6e38aaef > files > 15

cvsreport-0.3.5-7mdv2010.0.noarch.rpm

#!/usr/bin/perl -w

# This script is not really functionnal, but it does tickle a CVS
# repository quite a bit.

my @files;
my @folders;


sub rand_str {
    my @blobs = ('bla', 'foo', 'bar', 'moo', 'ta', 'wo', 'wuz', 'plo');
    my $str = '';

    for (my $i = 1 + int(rand(4)); $i; $i--) {
        $str .= $blobs[int(rand(@blobs))];
    }
    return $str;
}

sub rand_msg {
    my @words;

    for (my $i = 3 + int(rand(8)); $i; $i--) {
        push @words, rand_str();
    }
    return join ' ', @words;
}

sub list_remove {
    my $item = shift;
    my @l = ();
    my $x;

    while ($x = shift) {
        push @l, $x if $x ne $item;
    }
    return @l;
}

sub cvs_do {
    my $command = shift;
    $command = "cvs -Q $command >/dev/null";
#    print "$command\n";
    system $command;
}

sub create_file {
    open(FILE, '>'. shift) or return 0;
    print FILE "This is some dummy content\n";
    close FILE;
    return 1;
}

sub modify_file {
    open(FILE, '>>'. shift) or return 0;
    print FILE "This is another dummy content\n";
    close FILE;
    return 1;
}

my $offset = 0;

sub jam {
    # Among known files, elect some to be modified, some to be removed
    my @existing = @files;
    my @removed = ();
    my @modified = ();
    my $todo = 1 + int(rand(9));
    for my $file (@existing) {
        if(int(rand(5)) == 0) {
            push(@removed, $file);
            @files = list_remove $file, @files;
            next;
        }

        if (int(rand(4)) == 0) {
            push(@modified, $file) if modify_file $file;
        }

        last if (@modified + @removed) > $todo;
    }

    # Handle removals
    cvs_do "rm -f @removed" if @removed > 0;

    # Add some files
    my @added = ();
    for (my $i = (@files > 0 ? 0 : 5) + int(rand(5)); $i; $i--) {
        my $folder =  $folders[int(rand(@folders))];
        if (int(rand(5)) == 0) {
            # Create a new folder
            $folder .= '/'. rand_str();
            if (mkdir $folder) {
                push @folders, $folder;
                cvs_do "add $folder";
            }
        }

        my $newfile = $folder .'/'. rand_str();
        if(create_file $newfile) {
            push @added, $newfile;
            push @files, $newfile;
        }
    }
    cvs_do "add @added" if @added > 0;

    my $msg = rand_msg();
    cvs_do "ci -m '$msg'";

    $msg = substr($msg, 0, 20) .'...' if length $msg > 20;
    print "**   cset: u:'$ENV{USER}' b:'' start:$offset m:'$msg'\n";
    for my $file (@modified) { $file =~ s/^\./test/; print "**     M $file\n"; }
    for my $file (@added)    { $file =~ s/^\./test/; print "**     A $file\n"; }
    for my $file (@removed)  { $file =~ s/^\./test/; print "**     R $file\n"; }

    my $total = @modified + @added + @removed;
    $offset += $total;
    return $total;
}

sub init {
    srand 0;
    @files   = split /\n/, `find . -type f -and -not -regex '.*\/CVS\/.*'`;
    @folders = split /\n/, `find . -type d -and -not -regex '.*\/CVS.*'`;
}

if (@ARGV < 1) {
    print STDERR "Usage: cvsjam <changeset count>\n";
    exit;
}

my $cset_nb = $ARGV[0];

init();

my $n = $cset_nb;
while ($n) {
    $n-- if jam();
}
print "**   total: $offset entries, $cset_nb\n";