首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Perl从Accurev导出快照

使用Perl从Accurev导出快照
EN

Stack Overflow用户
提问于 2018-12-11 05:24:33
回答 1查看 69关注 0票数 1

我尝试使用Perl脚本从Accurev中拉出所有快照,但遇到了问题。

我可以单独运行这个命令

代码语言:javascript
复制
accurev show -p myDepot streams

这就是我所拥有的:

代码语言:javascript
复制
#!/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" ;
}
EN

回答 1

Stack Overflow用户

发布于 2018-12-12 02:12:54

问题是AccuRev路径不能正常工作,所以我得不到正确的输出。由于我的环境变量中列出了AccuRev主目录,因此我能够调用accurev并将其保存到一个XMLin文件中,以便在XMLin调用中引用。除此之外,命令必须是"“,而不是'‘或。

代码语言:javascript
复制
#!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" ;
}

下面是调用:

代码语言:javascript
复制
perl -w snapshot.pl <depot> "FromDate" "ToDate" > output.txt 2>&1

输出如下所示:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53713856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档