使用Perl从Accurev导出快照

时间:2018-12-10 21:24:33

标签: perl accurev

我正在尝试使用Perl脚本从Accurev中提取所有快照,但是出现问题。

我可以自行运行此命令

accurev show -p myDepot streams

这将为我获取所有流,但是当我将其放入我的Perl脚本时,我空了,无法将参数传递给for每个循环。

这就是我所拥有的:

#!/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" ;
}

1 个答案:

答案 0 :(得分:0)

问题是AccuRev路径无法正常工作,所以我没有得到正确的输出。由于我的环境变量中列出了AccuRev主目录,因此我可以调用accurev并将其保存到XML文件中,以便在XMLin调用中进行引用。 除此之外,该命令必须位于“” not“或”中。

下面是最终结果,带有附加参数来指定快照的日期范围:

#!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" ;
}

这是电话:

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

输出看起来像这样:

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