[p4] Old checkpoints and journal

Ken.Williams@thomson.com Ken.Williams at thomson.com
Thu May 4 12:19:37 PDT 2006


Hmm, that attachment didn't seem to work.  Maybe this list strips
attachments or something, or maybe outlook is being ridiculous again.

Here's the script just pasted.  Line breaks are probably all wonky this
time...

 -Ken
--------------------------
#!/usr/bin/perl

use strict;

use Getopt::Long;
use File::Spec;

my ($dir, $test);
my ($daily, $weekly, $monthly) = (5, 5, 5);
GetOptions('dir=s'  => \$dir,
	   'daily=i' => \$daily,
	   'weekly=i' => \$weekly,
	   'monthly=i' => \$monthly,
	   'test' => \$test);

die "Usage: $0 --dir <directory> [--daily <n>] [--weekly <n>] [--monthly
<n>] [--test]\n"
  unless $dir;

my $counter = 0+`p4 counter journal`
  or die "Couldn't get the current 'journal' counter\n";

# Get the current list of checkpoint & journal files
my (@checkpoints, @journals);
opendir my($dh), $dir or die "Can't read directory $dir: $!";
while (defined(my $file = readdir $dh)) {
  push @checkpoints, $file if $file =~ /^checkpoint\.\d/;
  push @journals,    $file if $file =~ /^journal\.\d/;
}
@checkpoints = sort {name_to_num($a)<=>name_to_num($b)} @checkpoints;
@journals    = sort {name_to_num($a)<=>name_to_num($b)} @journals;


# Just test out our algorithm for culling files - user should hit
# control-c to abort the script
if ($test) {
  while (1) {
    @checkpoints = cull($counter, \@checkpoints, [$daily, $weekly,
$monthly]);
    print "Stage $counter: @checkpoints\n";
    <STDIN>;

    # A little hack to add another file
    push @checkpoints, "checkpoint." . (1+name_to_num($checkpoints[-1]))
. ".gz";
    $counter++;
  }
  exit 0;
}

# Perform the actual rotation
my $kept = rotate($counter, \@checkpoints, $dir, [$daily, $weekly,
$monthly]);
warn "Kept $kept checkpoint files\n";
$kept =    rotate($counter, \@journals,    $dir, [$daily, $weekly,
$monthly]);
warn "Kept $kept journal files\n";

##############################################
sub name_to_num {
  my $name = shift;
  $name =~ /^\w+\.(\d+)/
    or die "Can't parse filename $name";
  return $1;
}

sub rotate {
  my ($counter, $files, $dir, $schedule) = @_;
  my %keep = map {$_, 1} cull($counter, $files, $schedule);
  foreach my $file (@$files) {
    next if $keep{$file};
    my $fullpath = File::Spec->catfile($dir, $file);
    warn "Removing $fullpath\n";
    unlink $fullpath or die "Can't remove $fullpath: $!";
  }
  return keys(%keep);
}

sub cull {
  my ($counter, $files, $schedule) = @_;

  my @keep;
  foreach my $file (@$files) {
    my $num = name_to_num($file);

    # Dailys
    my $max = $schedule->[0];
    if ($counter-$num < $max) {
      push @keep, $file;
      next;
    }

    # Weeklys
    $max += 7 * $schedule->[1];
    if ($counter-$num < $max and not $num %  7) {
      push @keep, $file;
      next;
    }

    # Monthlys
    $max += 28 * $schedule->[2];
    if ($counter-$num < $max and not $num % 28) {
      push @keep, $file;
      next;
    }
  }

  return @keep;
}
-------------------------- 

> -----Original Message-----
> From: perforce-user-bounces at perforce.com 
> [mailto:perforce-user-bounces at perforce.com] On Behalf Of 
> Williams, Ken (TLR Corp)
> Sent: Thursday, May 04, 2006 9:57 AM
> To: philippep at collectivestudios.com; perforce-user at perforce.com
> Subject: Re: [p4] Old checkpoints and journal
> 
> Hi Philippe,
> 
> I use the attached script to rotate my checkpoints & journals.
> 
>  -Ken 
> 
> > -----Original Message-----
> > From: perforce-user-bounces at perforce.com 
> > [mailto:perforce-user-bounces at perforce.com] On Behalf Of 
> > Philippe Paquet
> > Sent: Wednesday, May 03, 2006 4:37 PM
> > To: perforce-user at perforce.com
> > Subject: [p4] Old checkpoints and journal
> > 
> > Hi Guys,
> > 
> > We are currently doing a checkpoint every day on a pretty large and
> > active depot which generate a fair amount of data (150Mb+). I 
> > would like
> > to setup an automatic process to remove old checkpoints and 
> journals.
> > How long do you keep your old checkpoints and journal? What kind of
> > process to you use to remove old checkpoints and journal?
> > 
> > Cheers,
> > Philippe
> > --- 
> > Philippe Paquet
> > Technical Director
> > The Collective Inc, a Foundation 9 Entertainment company
> > Phone: (949) 255 1900 x 870
> > Fax: (949) 724 9667
> > Email: philippep at collectivestudios.com
> > www.f9e.com
> > www.collectivestudios.com
> > 
> > _______________________________________________
> > perforce-user mailing list  -  perforce-user at perforce.com
> > http://maillist.perforce.com/mailman/listinfo/perforce-user
> > 
> 



More information about the perforce-user mailing list