1

I am trying to use a Perl script to pull out all snapshots from Accurev but I'm having issues.

I can run this command fine on it's own

accurev show -p myDepot streams

This will get all the streams for me, but when I go to put it into my Perl script, I come up empty and can't pass in the argument to a for each loop.

Here's what I have:

#!/usr/bin/perl
#only tested on Windows - not supported by AccuRev

use XML::Simple ;
use Data::Dumper ;
use strict ;
use Time::Piece;

### Modify to reflect your local AccuRev client path
$::AccuRev = "/cygdrive/c/\"Program Files (x86)\"/AccuRev/bin/accurev.exe" ;


my ($myDepot, $myDate, $stream_raw, $stream_xml, $streamNumber,   $streamName, $counter, $snapTime) ;

### With AccuRev 4.5+ security, if you want to ensure you are authenticated before executing the script,
### uncomment the following line and use a valid username and password.

system "$::AccuRev login -n username password" ;

chomp($myDepot = $ARGV[0]);
chomp($myDate = $ARGV[1]);

if ($myDepot eq "") {
  print "\nUsage: perl snapshot_streams.pl <depot_name>\n" ;
  print "This script will return the name of the snapshot streams for the depot passed in...\n" ;
  exit(1) ;
}


$stream_raw = `$::AccuRev show -p $myDepot -fx streams`;
$stream_xml = XMLin($stream_raw, forcearray => 1, suppressempty => '', KeyAttr => 'stream') ;
if ($stream_xml eq "") {
  print "\nDepot $myDepot doesn't exist...\n" ;
  exit(1) ;
}

print "List of snapshots in depot $myDepot:\n";
$counter = 0 ;

foreach $stream_xml (@{$stream_xml->{stream}})
{
    if ($stream_xml->{type} eq "snapshot") {
    $streamName =  $stream_xml->{name};
    $snapTime = scalar localtime($stream_xml->{time});
    my $datecheck = $snapTime->strftime('%Y%m%d');
    if ($datecheck >= $myDate){
    print "Snapshot Name: $streamName \t\t\t Time: $snapTime\n" ;
    }
    $counter = $counter + 1 ;
    }       
}

if ( $counter == 0 ) {
     print "\nNo snapshots found in depot $myDepot...\n" ;
}
Stacker
  • 137
  • 3
  • 12
  • Do you use the perl debugger? It seems like you could step through this program yourself and see at what part the program results deviate from your expectations. Then you could narrow the question down. – mob Dec 10 '18 at 22:53
  • What output do you actually get from running the script? You probably want to either use the debugger to look at each line of execution or to add some additional debugging output to see the contents of variables. – Mike Abusheery Dec 11 '18 at 14:30
  • I was getting an output of 0, so it was failing but I solved it, I will post below – Stacker Dec 11 '18 at 18:06

1 Answers1

0

The problem was that the AccuRev path was not working correctly so I was not getting the correct output. Since I have the AccuRev home directory listed in my envrionment variables I was able to call accurev and save it to an XML file to be referenced in the XMLin call. In addition to this, the command had to be in "" not '' or ``.

Below is the end result with an additional argument to specify the date range of snapshots:

#!C:\Strawberry\perl\bin
#only tested on Windows - not supported by AccuRev

use XML::Simple qw(:strict);
use English qw( -no_match_vars );
use Data::Dumper ;
use strict ;
use Time::Piece;

my ( $login, $xml, $command, $myDepot, $myDateStart, $myDateEnd, $stream_xml, $streamNumber, $streamName, $counter, $snapTime) ;

###If Accurev is already in your environment variables, you can call it without setting the path 
###otherwise uncomment and update script 
###$accurev = "/cygdrive/c/\"Program Files (x86)\"/AccuRev/bin/accurev.exe";
### With AccuRev 4.5+ security, if you want to ensure you are authenticated before executing the script,
### uncomment the following line and use a valid username and password.

###$login = "accurev login -n username password" ;
###system($login);

chomp($myDepot = $ARGV[0]);
chomp($myDateStart = $ARGV[1]);
chomp($myDateEnd = $ARGV[2]);

if ($myDepot eq "") {
  print "\nUsage: perl snapshot_streams.pl <depot_name>\n" ;
  print "This script will return the name of the snapshot streams for the depot passed in...\n" ;
  exit(1) ;
}

$command = "accurev show -p $myDepot -fx streams > snapshot_streams.xml";
system($command);

$stream_xml = XMLin("snapshot_streams.xml", ForceArray => 1, SuppressEmpty => '', KeyAttr => 'stream') ;
if ($stream_xml eq "") {
  print "\nDepot $myDepot doesn't exist...\n" ;
  exit(1) ;
}

print "List of snapshots in depot $myDepot:\n";
$counter = 0 ;

foreach $stream_xml (@{$stream_xml->{stream}})
{
    if ($stream_xml->{type} eq "snapshot") {
        $streamName =  $stream_xml->{name};
        $snapTime = scalar localtime($stream_xml->{time});
        my $datecheck = $snapTime->strftime('%Y%m%d');
        if ($datecheck >= $myDateStart && $datecheck <= $myDateEnd){
        print "Snapshot Name: $streamName \t\t\t Time: $snapTime\n" ;
        }
        $counter = $counter + 1 ;
    }       
}

if ( $counter == 0 ) {
    print "\nNo snapshots found in depot $myDepot...\n" ;
}

Here is the call:

perl -w snapshot.pl <depot> "FromDate" "ToDate" > output.txt 2>&1

The output looks something like this:

List of snapshots in depot <Depot_Name>:

Snapshot Name: Product_1_SS                  Time: Tue Jul 04 10:00:05 2018
Snapshot Name: Product_2_SS                  Time: Tue Jul 07 11:00:15 2018
Snapshot Name: Product_3_SS                  Time: Tue Jul 15 12:30:30 2018
Stacker
  • 137
  • 3
  • 12