说明:虽然是自己写的东西,也一直在用,但这两天在整理自己的工作时发现读懂它还是花了不少时间,
故现在加上详细的注释,同时放在博客上,供有需要的朋友参考。
#! /usr/bin/perl
####################################################
#Program Name: svn_group.pl
#Program Version: V1.0
#Release Notes:
#V1.0 2009-06-01 Release by leijianfeng
#
#Program Description:
#This file is used to generate svn group infromation
#format file from a certain file extracted from
#authority control excel file.
#
####################################################
#The Getopt::Long module implements an extended getopt function
# called GetOptions().
use Getopt::Long;
######################################################################
#Parse all the command line option and make sure options are all valid.
#The argument must be string format
$optionParse = &GetOptions("h:s" =>\$help,
"file_list:s" =>\$file_list_file, #suppose it is filelist.txt
"o:s" =>\$output_file);
#####################################################################
############################################################################
# Check for various errors in command line options.
#If there is something wrong with the various in command line options,
#exec the follow code
if(!$optionParse)
{
print "Invalid Options provided. Please check the options.\n";
&printHelp(); #invoke function printHelp,and print usage information for usr
exit;
}
###########################################################################
############################################################################
# Process the -h option and print usage information for user.
if(defined($help))
{
&printHelp(); #Type "perl svn_group.pl -h" on the command line ,then will invoke 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;# the number of files will be processed
if ($num_of_list_file == 0) # no file path in filelist.txt
{
print "Error: There is no file in the file_list. Please check file list. \n";
exit;
}
else {
my @group_file_format_all;
push (@group_file_format_all, "\n#This file is generated by svn_group.pl\n");
foreach (@file_list) {
$group_file_excel=$_; #one of the file's path
my @group_file_format_one = &group_content_gen($group_file_excel);
push(@group_file_format_all,@group_file_format_one);
}
&writefile(@group_file_format_all,$output_file); #Let then content of @group_file_fomat_all ouput to $output_file.
exit;
}
} else {#If there is no filelist file have been set
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_group [ -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_group.pl -file_list xxx.txt -o xxx.txt\n";
} # end printHelp
############################################################################
#############################################################################
#process the .txt file,and generate the svn group informnation
sub group_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); # @file_name contain all of the will be processed file's content.
shift @file_content; #remove fisrt element beacause it is just contain the filed description.
my @output_content;
my @temp;
print "Operating on $file_name...!\n";
push (@output_content,"\n\n\#$file_name\n");
foreach (@file_content) {
if (/^\s*$/) {} # if it's a bank line, do nothing.
else { # process the content
my $group_name;
my $user_id;
my @file_line = split /\t/, $_; #split the line content into a array ,the separator is tab.
$group_name=$file_line[0];
$user_id = $file_line[3];
if ($file_line[0]=~/\w/){ # the first element of @file_line is not null
push @temp,$file_line[0]; #group name
push @output_content,"\n$group_name = $user_id";
}else{
push @output_content,",$user_id";
}
}
}
@output_content;
}
#############################################################################
#############################################################################
#process the filellist file,and gain the right content
sub get_file_list
{
my @file_list = &readfile($file_list_file); #now,@file_list contains the content of filelist.txt
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 #&writefile(@group_file_format_all,$output_file);
{
my $filename = pop @_;
unlink $filename;
open POSTFILE, ">$filename" or die "Can't write $filename";
select POSTFILE; # set POSTFILE as the default output filehandle
$|=1; #the effect is that clear the buffer immediately after the default filehandle outputfunction.
select STDOUT;
foreach (@_) {
print POSTFILE;
}
close POSTFILE;
}
############################################################################
############################################################################
#readout file's content,and return to the parent program
sub readfile
{
my $filename = pop @_; #the subroutine readfile's parameter,namely filelist.txt
my @file_content;
open INFILE, "$filename" or die "Can't open file $filename";
@file_content=;
close INFILE;
@file_content;
}
############################################################################
阅读(1246) | 评论(0) | 转发(0) |