1 脚本名称
file_assistant
2 脚本路径
scripts/bin/file_assistant
3 脚本参数说明
次序 | 参数名 | 说明 |
1 | file_type (v;sv;c;all) | 指定脚本要修改的文件类型,目前支持 .v;.sv;.c(后续可以增加其他); |
2 | word_dir | 指定脚本执行路径(可以为脚本所在路径的任意相对路径)。 |
3 | substitude_list | 指定脚本查找替换参考的文件列表,默认为file_assistant_list,遵循perl正则表达式规则 |
示例 | 命令:file_assistant all ./rfdig/rfdig_m33_tc017 file_assistant_list 对当前路径下的rfdig/rfdig_m33_tc017文件夹及其子文件夹下所有的.c/.v/.sv文件进行修改,修改列表为file_assistant_list,遵循perl正则表达式标准; | |
命令:file_assistant v ./rfdig file_assistant_list 对当前路径下的rfdig文件夹及其子文件夹下的所有.v文件进行修改,修改列表为file_assistant_list,遵循perl正则表达式标准; | ||
命令:file_assistant c ../ccom user_define_list 对上层目录的ccom目录下的所有.c文件进行修改,修改列表为user_define_list,遵循perl正则表达式标准; | ||
说明 | 目前该脚本可完成对文件的批量正则匹配替换、删除操作,如有其他需求,可以在此基础上继续开发。 |
3 脚本操作说明
该脚本用于批量替换文件中的指定内容,该脚本执行替换操作时,需要指定替换的文件类型、替换的文件路径、替换的参考列表(参考列表遵循perl正则表达式规则):
在本例中,该脚本用于解决如下问题:在ST仿真中,用于.v和.c交互的iopad信号,将其批量修改为BUCK_BANKUP信号,如下:
具体解决方式为:将rfdig_m33_tc017目录下的.v代码中的iopad_p10/01/02==’d1修改为BUCK_BACKUP==‘d10/1/2,将该目录下的.c代码中的IOMUX_REGS[gpio_ret_pd_aon_p10/01/02_config]修改为PMU_REGS[ana_buck_act_cfg1] = (10/1/2 & ana_buck_act_cfg1_buck_backup_mask) << ana_buck_act_cfg1_buck_backup_shift;
- 使用脚本前,首先完善file_assistant_list,以perl正则表达式为匹配替换规则,将匹配内容置于 ”>>>>” 符号左边,将替换内容置于 “>>>>” 符号右边;
- 根据脚本存放路径,指定脚本工作目录的相对路径,如下例所示,脚本存放于case目录,对rfdig/rfdig_m33_tc017文件夹下的.c/.v/.sv文件执行批量查找替换操作,参考file_assistant_list;执行:file_assistant all ./rfdig/rfdig_m33_tc017 file_assistant_list
-
如下所示,替换成功,仿真也可以正常pass
4 脚本代码
#! /usr/bin/perl -w
#==========================================================
# PERL MODULE
#==========================================================
use Cwd;
use File::Path;
use File::Find;
#==========================================================
# PERL FUNCTION GetOptions (get command parameters)
#==========================================================
$g_fl_type = $ARGV[0];
$g_work_dir = $ARGV[1];
$g_sub_fl = $ARGV[2];
if ($g_sub_fl =~ m/^\s*$/) {
$g_sub_fl = "file_assistant_list";
print "g_sub_fl =$g_sub_fl\n";
}
our $g_help = 0;
#==========================================================
# SCRIPT MAIN
#==========================================================
our @old;
our @new;
our @ref_num;
our @ref_context;
our @AoA = (\@old,\@new,\@ref_num);
Main();
sub SubContent{
if (open(SUBSTITUTE_ID,"file_assistant_list") or die "cannot open file_assistant_list, no such file") {
@substitudecontent = <SUBSTITUTE_ID>;
my $i = 0;
foreach $substitude (@substitudecontent) {
$substitude =~ s/[\n\r]*//g;
if ($substitude =~ m/\s*>>>>\s*/) {
print "before match string: $`\n";
print "match string : $&\n";
print "after match string: $'\n";
$old[$i] = "$`";
$new[$i] = "$'";
#my $ref_pattern_all = ($new[$i] =~ s/(\$\d+)/$1/g);
#my $ref_pattern_nc = ($new[$i] =~ s/(\\\$\d+)/$1/g);
my $ref_pattern_all = ($old[$i] =~ s/(\()/$1/g);
my $ref_pattern_nc = ($old[$i] =~ s/(\\\()/$1/g);
my $ref_pattern_num = $ref_pattern_all - $ref_pattern_nc;
$ref_num[$i] = $ref_pattern_num;
print "ref_pattern_all : $ref_pattern_all\n";
print "ref_pattern_nc : $ref_pattern_nc\n";
print "ref_pattern_num : $ref_num[$i]\n";
for( $j = 0; $j < @AoA; $j=$j+1 ){
print "AoA[$j][$i]: $AoA[$j][$i] \n";
}
$i = $i+1;
}
}
}
}
sub CopyFile{
$fl_name = scalar(@_);
print "source file name is $_[0]\n";
print "orien file name is $_[1]\n";
open(ORIEN_FILE,">$_[1]");
if (open(SOURCE_FILE,"$_[0]")) {
@context = <SOURCE_FILE>;
foreach $line (@context) {
print ORIEN_FILE "$line";
}
close SOURCE_FILE;
}
close ORIEN_FILE
}
sub SubFile{
$fl_name = scalar(@_);
print "source file name is $_[0]\n";
print "orien file name is $_[1]\n";
$sub_ready = 0;
open(ORIEN_FILE,">$_[1]");
if (open(SOURCE_FILE,"$_[0]")) {
@context = <SOURCE_FILE>;
foreach $line (@context) {
for($m = 0; $m < @old; $m=$m+1 ){
if ($line =~ m/$AoA[0][$m]/) {
$pre_match = $`;
$post_match = $';
$sub_pattern = $AoA[1][$m];
print "sub_pattern is $sub_pattern \n";
if ($ref_num[$m] > 0) {
for($k = 0; $k < $ref_num[$m]; $k = $k+1 ){
$j = $k+1;
$ref_context[$k] = $$j;
print "ref pattern is $$j \n";
print "ref_context[$k] is $ref_context[$k]\n";
}
$sub_pattern =~ s/\\\$(\d+)/\[\,\,\,\[\.\.\.\[\!\!\!\[$1/g;
print "111111sub_pattern is $sub_pattern \n";
$sub_pattern =~ s/\$(\d+)/$ref_context[$1-1]/g;
print "222222sub_pattern is $sub_pattern \n";
$sub_pattern =~ s/\[\,\,\,\[\.\.\.\[\!\!\!\[(\d+)/\$$1/g;
print "333333sub_pattern is $sub_pattern \n";
}
print ORIEN_FILE "$pre_match"."$sub_pattern"."$post_match";
$sub_ready = 1;
}
}
if($sub_ready eq 0) {
print ORIEN_FILE "$line";
}
$sub_ready = 0;
}
close SOURCE_FILE;
}
close ORIEN_FILE
}
sub FindWanted{
if(-f $File::Find::name) {
if (($g_fl_type eq "v" ) or ($g_fl_type eq "all") and ($File::Find::name =~ /\.v$/)) {
print "copy .v file begin ...... \n";
CopyFile("$_","$_"."\.old");
print "sub .v file begin ...... \n";
SubFile("$_"."\.old","$_");
print "del .v file begin ...... \n";
}
if (($g_fl_type eq "sv") or ($g_fl_type eq "all") and ($File::Find::name =~ /\.sv$/)) {
print "copy .sv file begin ...... \n";
CopyFile("$_","$_"."\.old");
print "sub .sv file begin ...... \n";
SubFile("$_"."\.old","$_");
print "del .sv file begin ...... \n";
}
if (($g_fl_type eq "c" ) or ($g_fl_type eq "all") and ($File::Find::name =~ /\.c$/)) {
print "copy .c file begin ...... \n";
CopyFile("$_","$_"."\.old");
print "sub .c file begin ...... \n";
SubFile("$_"."\.old","$_");
print "del .c file begin ...... \n";
}
}
}
sub Main{
SubContent();
my $cur_dir = getcwd;
chdir($cur_dir) or die "$cur_dir does not exist!!!";
print "cur_dir is $cur_dir\n";
find(\&FindWanted,"$cur_dir/$g_work_dir");
}