前言
之前的时候当我需要filelist时候,选择的是借助vcs:
【python脚本】当我需要一个完整的rtl list时,还是选择求助vcs吧
可是呢,我又熬走了一个vcs,悲桑:
Retrying request....
Failed to obtain license...
Note: Use +vcs+lic+wait ( or -licwait <minute> or -licqueue in Unified Use Model ) to queue for license
没办法,只好手动来解析了。
关键函数
环境变量替换函数
我感觉把这两个函数我应该单独水两个csdn才是啊;
路径中经常会有很多环境变量比如$WORK_PATH、$PROJ_PATH这类的,因此需要首先把路径中的环境变量替换掉:
def replace_env_var(path):
def get_env(match):
return os.getenv(match.group(1))
path = re.sub(r"\$(\w+)", get_env, path)
return path
经过该函数后,路径中环境变量就会被替换掉了。
相对路径替换函数
之后要把相对路径转换为绝对路径,使用的是下一个函数:
def replace_relative_path(base, offset=""):
if offset != "":
path = os.path.join(base, offset)
else:
path = base
path = re.sub(r"\/.\/\/", "//", path) # /path/./aaa -> /path//a
path = re.sub(r"\/+", "/", path) #/path///aa//b -> /path/a/b
while re.search(r"\w+\/\.\.\/", path):
path = re.sub(r"\w+\/\.\.\/", "",path) #/path/aa/../b -> /path/b
return path
不过不得不承认,这个函数还是不完美的,之后有时间把他补充完备一些,不过目前是够用的了。
解析filelist
在filelist中我们一般会见到以下几种吃入文件的方式:
+incdir+path:这样的方式是这定一个全局路径,路径中的全部文件都是可以吃到的;
-f:最常见的引用绝对路径子filelist的方式,我个人只接受这种方式,拒绝-f/-F混用;
-F:一般是引用相对路径子filelist,项目中一般不这样使用,函数中也不解析了;
-y/-v:用来引用库和库文件的,大部分脚本使用场景下是不需要关心的,也不仅行解析;
$PROJ_PATH/aaa/bb.v:直接文件路径,索引文件;
因此解析函数的思路就是从root filelist开始递归解析以上几种索引方式:
def process_list(lst):
file_list = []
lst = lst.strip()
lst = replace_relative_path(replace_env_var(lst))
file_list.append(lst)
(filepath, filename) = os.path.split(lst)
with open (lst, "r") as handle:
for line in handle:
line = re.sub(r"\/\/.*", "", line)
if line != "":
re0 = re.match(r"\+incdir\+(.*)", line)
re1 = re.match(r"\-f\s+(.*)", line)
re2 = re.match(r"-v", line)
re3 = re.match(r"-y", line)
if re0:
path = replace_relative_path(replace_env_var(re0.group(1)))
file_list.append(path)
elif re1:
path = replace_relative_path(replace_env_var(re1.group(1)))
file_list.extend(process_list(path))
elif not re2 and not re3:
path = line.strip()
path = replace_relative_path(replace_env_var(path))
file_list.append(path)
return file_list