Экспорт снимков из Accurev с использованием Perl - PullRequest
0 голосов
/ 11 декабря 2018

Я пытаюсь использовать сценарий Perl для извлечения всех снимков из Accurev, но у меня возникают проблемы.

Я могу нормально выполнить эту команду самостоятельно

accurev show -p myDepot streams

Этополучит все потоки для меня, но когда я добавлю его в свой Perl-скрипт, я получаю пустой и не могу передать аргумент для каждого цикла.

Вот что у меня есть:

#!/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 голосов
/ 11 декабря 2018

Проблема заключалась в том, что путь AccuRev не работал правильно, поэтому я не получал правильный вывод.Так как у меня есть домашний каталог AccuRev, перечисленный в моих переменных окружения, я смог вызвать accurev и сохранить его в файл XML, который будет ссылаться в вызове XMLin.В дополнение к этому, команда должна быть в "" не "или" ".

Ниже приведен конечный результат с дополнительным аргументом для указания диапазона дат снимков:

#!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
...