Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477808
  • 博文数量: 122
  • 博客积分: 1403
  • 博客等级: 中尉
  • 技术积分: 1668
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-11 13:31
文章分类

全部博文(122)

文章存档

2018年(5)

2017年(12)

2014年(15)

2013年(33)

2012年(4)

2011年(53)

分类: Python/Ruby

2011-02-25 15:34:31

跟上一篇
 
#! /usr/bin/perl
####################################################
#Program Name: svn_ac.pl
#Program Version: V1.0
#Release Notes:
#V1.0 2009-6-4 Release by leijianfeng
#
#Program Description:
#This file is used to generate svn authority control
#format file from a certain file extracted from
#authority control excel file.
#
###################################################
use Getopt::Long;
######################################################################
#Parse all the command line option and make sure options are all valid.
$optionParse = &GetOptions("h:s" =>\$help,
                           "file_list:s"  =>\$file_list_file,
                           "o:s" =>\$output_file);
#####################################################################
############################################################################
# Check for various errors in command line options.
if(!$optionParse)
{
        print "Invalid Options provided. Please check the options.\n";
        &printHelp();
        exit;
}
###########################################################################
############################################################################
# Process the -h option and print usage information for user.
if(defined($help)) {&printHelp();exit;}
############################################################################
###########################################################################
#Main body
if (defined($file_list_file)){
 if (defined($output_file)) {} # If there are filelist file and output file,then do nothing.
 else { # If there are filelist file but there is'nt output file,then prompt the user to set output file or to see the help information.
  print "Error: Output file name has not been set, please set file name by option -o, or find help by option -h. \n";
  exit;
 }
 my @file_list = &get_file_list; contain that  will be processed file's path
 my $num_of_list_file = @file_list;
 if ($num_of_list_file == 0) {
  print "Error: There is no file in the file_list. Please check file list. \n";
  exit;
 } else {
  my @ac_file_format_all;
  push (@ac_file_format_all, "\#This file is generated by svn_ac.pl\n");
  foreach (@file_list) {
   $ac_file_excel=$_;#one of the file's path
   my @ac_file_format_one = &ac_content_gen($ac_file_excel);
   push(@ac_file_format_all,@ac_file_format_one);
  }
  &writefile(@ac_file_format_all,$output_file);#Let then content of @ac_file_fomat_all ouput to $output_file.
 exit;
  }
} else {
 print "Error: File list has not been set, please set file list by option -file_list, or find help by option -h. \n";
 exit;
} #end Main body
###########################################################################
############################################################################
sub printHelp
{
    print "perl svn_ac.pl [ -h] –file_list -o \n";
    print " -h         This help message.\n";
    print " -file_list Name of the file list file.\n";
    print " -o         Output file name\n";
    print "\nExample:\n  perl svn_ac.pl  -file_list filelist.txt -o ac.txt\n";
} # end printHelp
############################################################################

#############################################################################
# generate svn authority control format file
sub ac_content_gen
{
 my $file_name=pop @_; # $file_name's value is one of the will be processed file's path
 my @file_content = &readfile($file_name); contain all of the will be processed file's content.
 my @output_content;
 print "Operating on $file_name...!\n";
 push (@output_content,"\#$file_name\n");
 foreach (@file_content) {
  s/^\s+|\s+$//g; #remove all blanks before and after this line
  if (/^\s*$/) {}
  else {
   my @file_line = split /\t/, $_;
   my $none_auth = pop @file_line;
   my $read_auth = pop @file_line;
   my $write_auth = pop @file_line;
   my @path_content;
   foreach (@file_line) {
    push (@path_content, "$_\/");
   } 
   
   push (@output_content, "\n\[nationz:\/");
   push (@output_content, @path_content);
   push (@output_content, "\]\n");
   foreach(@output_content){
    s/\-\///g;  
    
    }
   
   if ($read_auth ne "-") {
    my @read_auth_tmp = split /,/ , $read_auth;
    foreach (@read_auth_tmp) {
     s/^\"|\"$//g;
     push (@output_content, "\@$_ = r\n");
    }
   }
   if ($write_auth ne "-") {
    my @write_auth_tmp = split /,/ , $write_auth;
    foreach (@write_auth_tmp) {
     s/^\"|\"$//g;
     push (@output_content, "\@$_ = rw\n");
    }
   }
   push (@output_content, "* = \n");
  }
 }
 @output_content;
}
#############################################################################
############################################################################
sub get_file_list
{
 my @file_list = &readfile($file_list_file);
 my @tmp_list;
 foreach (@file_list) {
  s/^\s+|\s+$//g; #remove all blanks before and after this line
  if (!/^\#/) {
    push (@tmp_list, $_) if -e; #keep this line if no "#" at the begining and exist
    print "Warning: The file $_ is not exist, and will be ignored.\n" if !(-e);
  }
 }
 @file_list = @tmp_list;
}
############################################################################
############################################################################
sub writefile
{
 my $filename = pop @_;
        unlink $filename;
        open POSTFILE, ">$filename" or die "Can't write $filename";
        select POSTFILE;
        $|=1;
        select STDOUT;
        foreach (@_) {
                print POSTFILE;
        }
        close POSTFILE;
}
############################################################################
############################################################################
sub readfile
{
        my $filename = pop @_;
        my @file_content;
        open INFILE, "$filename" or die "Can't open file $filename";
        @file_content=;
        close INFILE;
        @file_content;
}
############################################################################
阅读(914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~