以前在做CRS(客户需求设置)时由于要对应多国多运营商多客户的各种需求,常常要手动地去寻找修改,
当然,对于那时刚刚进入MTK手机开发的我来说还是花了不少时间的,受MTK工程管理与方法论的启发和MS自动化生成代码的理念影响,我决定自己写一段代码来试试,并顺带实践下刚学的perl脚本,经过几天的摸索,终于小成,虽然还有很多可以改进的方面,但也是一个自己比较满意的东东...
等项目组有需求的时候我会再进行对应以便工程实际使用.
思路:
一般来说,CRS都会在一个excel里按大项小项与值记录下来,我们从中读取原始数据,并根据脚本中的提示进行简单的选择,然后查找相应字串并进行原文件修改以完成既定任务.
#!/usr/local/bin/perl
#use strict;
use warnings;
use Win32::OLE; #导入win32 OLE包
use Cwd;
my $DEBUG;##macro switch for debug
undef $DEBUG;
$dir = getcwd();##为该excel所在的目录,即工程目录,如e:\cne210,该目录下是plutommi等目录
print("*********************************\n");
print "now entering the work directory\n";
print($dir);
print "\n";
print("*********************************\n");
##相关目录或文件定义
##不同工程等需要对应修改
##nvram_cust_pack.c等文件所在的目录
##E:\cn081027\custom\app\KAKA25_07B_CMCC_BB
$DIR_CUSTOM_APP_PROJECT_BB = $dir."";
##此处可不精确定义大小,但考虑到效率问题,尽量精确
##这里仅考虑简单实现查找,并未使用二分法等来改善效率,留待以后改进
$maxrow = 490 ; ##excel最大行数
$maxcol = 3 ; ##excel最大列数
#line21
##此函数用于在excel sheet表单中搜索指定字符串位置
##return::该字符串所在的行和列
sub seach_str_in_sheet
{
my($sheet,$str) = @_;
my @array;
foreach $row (1..$maxrow)
{
foreach $col(1..$maxcol)
{
next unless defined $sheet->Cells($row,$col)->{'Value'};
if($str eq $sheet->Cells($row,$col)->{'Value'})
{
@array = ($row,$col);
return (@array);
}
#print out the contents of a cell
# print("At ($row, $col) the value is %s \n\",$Sheet->Cells($row,$col)->{'Value'} );
}
}
}
#数组输出子程序
sub print_array{
my(@printarray)=@_;
my $count =0 ;
while($count < @printarray)
{
print("the $count number to output is $printarray[$count]\n");
$count ++;
}
}
##此函数用于在excel sheet表单中搜索指定字符串位置
##return::该字符串所在的行和列
sub modify_value_in_file
{
my($file,$str1,$str2,$newval) = @_;
my $flg_first = 0 ;
print("$destfile written begin.....\n");
open (DESTFILE, ">>$file") || die ("Could not open file $file");
print("file open ok!!\n");
my $line = 0 ;
##mode1,mode2用于数字判断
my $mode1 = "^-?\\d+\$";
my $mode2 = "^-?0[xX][\\da-fA-F]+\$";#注意式子中的转义符
while($line = <DESTFILE>)
{
print("file modify finished and successfully\n");
if ($line =~ /$str1/)
{
$flg_first = 1;
next;
}
if($flg_first)
{
if($line=~s/$mode2.".*".$str2/0D:11:3C:3D:FF/)
{
print "00:FF:00:FF:00:FF in the file!\n";
}
last;
}
}
}
##此函数用于换行
##只是为了书写方便
sub newline
{
print "\n";
}
##进行某项设置时的提示
##只是为了书写方便
sub prompt_setting
{
my($item)=@_;
newline();
print("************************************************************");
newline();
print"now let's begin to set $item:\n";
print("************************************************************");
newline();
}
##usage: 本例程作用在于输出设置语言相关选项的提示信息
##author: sf.kaka
##mofifications:created 08122703
##all rights reserved by sf.kaka
sub prt_lang{
warn << "__END_OF_USAGE";
only thing to do is to input the indexno of default language in language packs
Example:
Language pack include languages in order as follows:
english
traditional chinese
simple chinese
if we want to set simple chinese as the default language;
we should then input the number: 2
then press the ENTER key
when you see this ptompttion,please input right num: 0 start
it's better to input in hex format,eg:0x00 :::::::
here not strong enough to check the input error!!!!!!!!!!!!
the index i want to choose is:
__END_OF_USAGE
}
my $crs_excel = $dir.'\crsauto'."\.xls";
my $nowstr;
#新建一个EXCEL应用对象,然后我们就可以对excel进行操作。
my $app_xls = Win32::OLE->new('Excel.Application', sub{$_[0]->Quit})
or die"Can't install Excel01!";#打开一个EXCEL文件,'True' 表示是只读
my $crs_book = $app_xls->WorkBooks->Open($crs_excel, 0, 'True');
my $crs_sheet = $crs_book->Worksheets('crsauto'); #选中一工作表
$nowstr = $crs_sheet->Cells(18,3)->{Value}; #取得一单元格中数据
print($nowstr);
print("\n");
@crs_array = seach_str_in_sheet($crs_sheet,"Default Language");
$crs_row = $crs_array[0];
$crs_col = $crs_array[1];
##print ("%d,%s",$crs_row,$crs_col);
##for debug and test
if(defined $DEBUG)
{
print $crs_row;
print $crs_col;
print $crs_sheet->Cells($crs_row,$crs_col + 1)->{'Value'};
}
my $lang_default = $crs_sheet->Cells($crs_row,$crs_col + 1)->{'Value'};
prompt_setting("LANGUAGE");
print ("the crs requirement:: $lang_default \n");
prt_lang();
$index_lang = <STDIN>;
chomp($index_lang);
newline();
$FILE_NVRAM_CUST_PACK_C = $DIR_CUSTOM_APP_PROJECT_BB."nvram_cust_pack\.c";
print $FILE_NVRAM_CUST_PACK_C;
##modify_value_in_file($FILE_NVRAM_CUST_PACK_C,"NVRAM_CACHE_BYTE_DEFAULT","NVRAM_SETTING_LANG",$lang_index);
my $flg_first = 0 ;
open (DESTFILE, "+<$FILE_NVRAM_CUST_PACK_C") || die ("Could not open file $FILE_NVRAM_CUST_PACK_C");
$str1 = ".*NVRAM_CACHE_BYTE_DEFAULT.*";
my $line = 0 ;
##mode1,mode2用于数字判断
##my $mode1 = "^-?\\d+\$";
$mode2 = "^-?0[xX][\\da-fA-F]+\$";#注意式子中的转义符
$file_temp = "temp.txt";#用于暂存文件的临时文件
open (TEMPFILE, ">$file_temp") || die ("Could not open file $file_temp");
print "\n";
print "please input your comment about this modification,eg:sf.kaka 09012001 for lang crs\n ";
$comment = <STDIN>;
chomp($comment);
print("\n");
//此处在查找相应的项目仅采用简单的匹配,可改进
while($line = <DESTFILE>)
{
if ($line =~ /$str1/)
{
print( "match $str1 ok\n");
$flg_first = 1;
}
if($flg_first)
{
if($line=~ /NVRAM_SETTING_LANG/)
{
$line = "\t$index_lang, \t\t \/\*NVRAM_SETTING_LANG\*\/" . "\/\/$comment\n";
$flg_first = 0;
print("NVRAM_SETTING_LANG match\n");
}
}
print TEMPFILE ("$line");
}
close(TEMPFILE);
close(DESTFILE);
unlink $FILE_NVRAM_CUST_PACK_C;
rename("temp.txt",$FILE_NVRAM_CUST_PACK_C);
undef $crs_excel;
undef $app_xls; #关掉所打开的excel应用