PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
preprocess.py
Go to the documentation of this file.
1 
13 
14 
15 
16 import re
17 import ast
18 import shutil
19 from Path.getPath import *
20 from Extract.getCall import getCallFunction, modifyWithName
21 from Extract.extractCall import WithVisitor
22 from Tool.tool import getAst,getParameter,getLastAPIParameter,departAPI,departAPI2,ConditionalReturnTransformer, getFileName, getRunFile
23 from Tool import codeUtils
24 from Tool.workspace import getRuntimePaths
25 
26 
27 
28 
32 def countBracket(s):
33  minL=0
34  minR=0
35  midL=0
36  midR=0
37  huaL=0
38  huaR=0
39  flag=1
40  cnt=0 #计算引号的个数
41  for it in s:
42  if it=='\'': #引号内的括号不计数
43  flag=0
44  cnt+=1
45  if cnt%2==0:
46  flag=1
47  elif flag==1:
48  if it=='(':
49  minL+=1
50  elif it==')':
51  minR+=1
52  elif it=='[':
53  midL+=1
54  elif it==']':
55  midR+=1
56  elif it=='{':
57  huaL+=1
58  elif it=='}':
59  huaR+=1
60  return minL,minR,midL,midR,huaL,huaR
61 
62 
63 
64 
67 def oneLine(filePath):
68  try:
69  root=getAst(filePath)
70  #重写回文件
71  with open(filePath,'w',encoding='UTF-8') as fw:
72  newCode=ast.unparse(root)
73  # newCode=re.sub(r'""".*?"""','pass',newCode,flags=re.DOTALL) #去掉代码中的注释
74  fw.write(f"{newCode}\n")
75  except Exception as e:
76  print(f"oneLine --> {filePath} parse to ast failed: {e}")
77 
78 
79 
80 
84  try:
85  root=getAst(filePath)
86  transformer = ConditionalReturnTransformer()
87  new_root = transformer.visit(root)
88  #重写回文件
89  with open(filePath,'w',encoding='UTF-8') as fw:
90  newCode=ast.unparse(root)
91  fw.write(f"{newCode}\n")
92  except Exception as e:
93  print(f"expandConditionalReturn --> {filePath} parse to ast failed: {e}")
94 
95 
96 
97 
106 def getListVar(root,ansLst):
107  for node in ast.iter_child_nodes(root):
108  if isinstance(node,ast.ListComp):
109  s=ast.unparse(node.generators).lstrip(' ')
110  pattern="for (.*?) in"
111  lst=re.findall(pattern,s)
112  if len(lst)==1:
113  temp=lst[0]
114  if temp[0]=='(':
115  var=temp[1:-1]
116  else:
117  var=temp
118  s1=f"{var}=[{temp} {s}][0]"
119  ansLst.append(s1)
120 
121  getListVar(node,ansLst)
122 
123 
124 
125 
133 def getDictVar(root,ansLst):
134  for node in ast.iter_child_nodes(root):
135  if isinstance(node,ast.DictComp):
136  s=ast.unparse(node.generators).lstrip(' ')
137  pattern="for (.*?) in"
138  lst=re.findall(pattern,s)
139  if len(lst)==1:
140  temp=lst[0]
141  if temp[0]=='(':
142  var=temp[1:-1]
143  else:
144  var=temp
145  s1=f"{var}=[{temp} {s}][0]"
146  ansLst.append(s1)
147 
148  getDictVar(node,ansLst)
149 
150 
151 
152 
157 def convertLocalVar(filePath,libName):
158  with open(filePath,'r',encoding='UTF-8') as fr:
159  codeLst=fr.readlines()
160 
161  for i in range(len(codeLst)):
162  s=codeLst[i].lstrip(' ').rstrip(' ')
163  try:
164  root=ast.parse(s,filename='<unknown>',mode='exec')
165  except Exception as e:
166  continue
167 
168  #step1:判断代码语句中是否含有列表推导式或字典推导式
169  listComp=0
170  dictComp=0
171  for node in ast.walk(root):
172  if isinstance(node,ast.ListComp):
173  listComp=1
174  if isinstance(node,ast.DictComp):
175  dictComp=1
176 
177  if not listComp and not dictComp:
178  continue
179 
180  #step2:判断列表推导式中是否含有第三方库调用的API
181  flag=0
182  _,callDict=getCallFunction(filePath,libName)
183  callLst=[record['call_text'] for record in callDict.values()]
184  flag=0
185  for node in ast.walk(root):
186  if isinstance(node, ast.Call):
187  callState=ast.unparse(node)
188  callState=callState.replace(' ','').replace('"','').replace("'",'')
189  for it in callLst:
190  if callState==it.replace(' ','').replace('"','').replace("'",''):
191  flag=1
192  break
193  if flag==1:
194  break
195  if flag==0:
196  continue
197 
198  #step3:提取出列表推导式中的变量
199  ansLst=[]
200  if listComp:
201  getListVar(root,ansLst)
202  spaceNum=countSpace(codeLst[i])
203  temp=''
204  for it in ansLst:
205  tryStr="try:\n"
206  exceptStr="except:\n"
207  passStr="pass\n"
208  if spaceNum:
209  tryStr=' '*spaceNum*1+tryStr
210  it=' '*spaceNum*1+' '*4+it+'\n'
211  exceptStr=' '*spaceNum*1+exceptStr
212  passStr=' '*spaceNum*1+' '*4+passStr
213  else:
214  spaceNum=4
215  it=' '*spaceNum*1+it+'\n'
216  passStr=' '*spaceNum*1+passStr
217  spaceNum=0 #用完之后就置为0
218 
219  s=tryStr+it+exceptStr+passStr
220  temp=temp+s
221  temp+=codeLst[i]
222  codeLst[i]=temp
223 
224  if dictComp:
225  getDictVar(root,ansLst)
226  spaceNum=countSpace(codeLst[i])
227  temp=''
228  for it in ansLst:
229  tryStr="try:\n"
230  exceptStr="except:\n"
231  passStr="pass\n"
232  if spaceNum:
233  tryStr=' '*spaceNum*1+tryStr
234  it=' '*spaceNum*1+' '*4+it+'\n'
235  exceptStr=' '*spaceNum*1+exceptStr
236  passStr=' '*spaceNum*1+' '*4+passStr
237  else:
238  spaceNum=4
239  it=' '*spaceNum*1+it+'\n'
240  passStr=' '*spaceNum*1+passStr
241  spaceNum=0 #用完之后就置为0
242 
243  s=tryStr+it+exceptStr+passStr
244  temp=temp+s
245  temp+=codeLst[i]
246  codeLst[i]=temp
247 
248 
249  with open(filePath,'w',encoding='UTF-8') as fw:
250  for it in codeLst:
251  fw.write(it)
252 
253 
254 
255 
260 def findAssignCall(root):
261  assignLst=[]
262  for node in ast.walk(root):
263  if isinstance(node,ast.Assign) and isinstance(node.value,ast.Call):
264  target=ast.unparse(node.targets)
265  assignLst.append(target)
266  return assignLst
267 
268 
269 
277 def getAssignReceiverExpr(root,source,receiverName,lineno):
278  aliasName=receiverName.split('.')[0]
279  receiverSuffix=receiverName[len(aliasName):]
280  expr=None
281  exprNode=None
282  bestLine=-1
283 
284  scopeBodies=[root.body]
285  for node in ast.walk(root):
286  if isinstance(node,(ast.FunctionDef,ast.AsyncFunctionDef,ast.ClassDef)):
287  start=node.lineno
288  end=getattr(node,'end_lineno',start)
289  if start<lineno<=end:
290  scopeBodies.append(node.body)
291 
292  for body in scopeBodies:
293  for stmt in body:
294  if not hasattr(stmt,'lineno') or stmt.lineno>=lineno:
295  continue
296  if isinstance(stmt,(ast.If,ast.For,ast.AsyncFor,ast.While,ast.Try,ast.With,ast.AsyncWith)):
297  continue
298  value=None
299  targets=[]
300  if isinstance(stmt,ast.Assign):
301  value=stmt.value
302  targets=stmt.targets
303  elif isinstance(stmt,ast.AnnAssign):
304  value=stmt.value
305  targets=[stmt.target]
306  if value is None:
307  continue
308  for target in targets:
309  if isinstance(target,ast.Name) and target.id==aliasName and stmt.lineno>bestLine:
310  sourceExpr=ast.get_source_segment(source,value)
311  if sourceExpr:
312  expr=sourceExpr.strip()
313  exprNode=value
314  bestLine=stmt.lineno
315 
316  if exprNode is None:
317  return None
318 
319  localNames=set()
320  for body in scopeBodies:
321  for stmt in body:
322  if not hasattr(stmt,'lineno') or stmt.lineno>=lineno:
323  continue
324  targets=[]
325  if isinstance(stmt,ast.Assign):
326  targets=stmt.targets
327  elif isinstance(stmt,(ast.AnnAssign,ast.AugAssign)):
328  targets=[stmt.target]
329  for target in targets:
330  for node in ast.walk(target):
331  if isinstance(node,ast.Name):
332  localNames.add(node.id)
333 
334  for node in ast.walk(root):
335  if isinstance(node,(ast.FunctionDef,ast.AsyncFunctionDef)):
336  start=node.lineno
337  end=getattr(node,'end_lineno',start)
338  if start<lineno<=end:
339  for arg in node.args.posonlyargs+node.args.args+node.args.kwonlyargs:
340  localNames.add(arg.arg)
341  if node.args.vararg:
342  localNames.add(node.args.vararg.arg)
343  if node.args.kwarg:
344  localNames.add(node.args.kwarg.arg)
345 
346  loadedNames={node.id for node in ast.walk(exprNode) if isinstance(node,ast.Name) and isinstance(node.ctx,ast.Load)}
347  if loadedNames & localNames:
348  return None
349 
350  if receiverSuffix:
351  return f'({expr}){receiverSuffix}'
352  return expr
353 
354 
355 
362 def getSelfReceiverExpr(root,lineno,methodName):
363  for node in ast.walk(root):
364  if not isinstance(node,ast.ClassDef):
365  continue
366  start=node.lineno
367  end=getattr(node,'end_lineno',start)
368  if not (start<lineno<=end) or len(node.bases)==0:
369  continue
370  defNames=set()
371  for item in ast.iter_child_nodes(node):
372  if isinstance(item,(ast.FunctionDef,ast.AsyncFunctionDef)):
373  defNames.add(item.name)
374  if methodName in defNames:
375  return None
376  return ast.unparse(node.bases[0])
377  return None
378 
379 
380 
381 
386 def countSpace(s):
387  cntSpace=0
388  for it in s:
389  if it==' ':
390  cntSpace+=1
391  else:
392  break
393  return cntSpace
394 
395 
396 
397 
405 def getImportLine(codeLst):
406  #首先判断from import语句中是否含有特殊的__future__
407  index=-1
408  for i in range(len(codeLst)):
409  if 'import' in codeLst[i] and '__future__' in codeLst[i]:
410  index=i
411 
412  #若没有future,再判断开头是否存在'"""'注释
413  count=0
414  if index==-1 and '"""' in codeLst[0]:
415  for i in range(0,len(codeLst)):
416  if '"""' in codeLst[i]:
417  index=i
418  count+=1
419  if count==2:
420  break
421 
422  index+=1
423 
424  return index
425 
426 
427 
428 
434  decoratorLst = []
435  for n in ast.walk(root):
436  if isinstance(n, (ast.FunctionDef, ast.ClassDef)) and n.decorator_list:
437  for decorator in n.decorator_list:
438  if isinstance(decorator, ast.Call): # and isinstance(decorator.func.value, ast.Name):
439  decoratorLst.append(ast.unparse(decorator.func))
440 
441  return decoratorLst
442 
443 
444 
454 def getDecoratorInsertIndex(codeLst,index):
455  insertIndex=index
456  if not codeLst[index].lstrip().startswith('@'):
457  return insertIndex
458 
459  spaceNum=countSpace(codeLst[index])
460  j=index-1
461  while j>=0:
462  if codeLst[j].lstrip().startswith('@') and countSpace(codeLst[j])==spaceNum:
463  insertIndex=j
464  j-=1
465  continue
466  break
467 
468  return insertIndex
469 
470 
471 
472 
478 def addDictSingle(callAPI,filePath,callKey):
479  with open(filePath,'r',encoding='UTF-8') as fr:
480  codeLst=fr.readlines()
481  source=''.join(codeLst)
482 
483  lineno=getImportLine(codeLst)
484  importDict='from recordValue import paraValueDict\nfrom recordValue import callsiteInfoDict\n'
485  codeLst.insert(lineno,importDict)
486 
487  paraStr=getLastAPIParameter(callAPI) #获取最后一个API的参数
488  parameterLst=getParameter(paraStr,space=0) #项目参数不去空格
489  root=getAst(filePath)
490  targetLst=findAssignCall(root)
491 
492  #找出树中所有withitem call节点 -- 2025/5/19
493  withitem_visitor = WithVisitor()
494  withitem_visitor.visit(root)
495  withitem_call_names = withitem_visitor.get_withitem_call() #dict
496 
497  for i in range(len(codeLst)): #每次只会往列表中插入一个元素
498  if callAPI.replace(' ','') in codeLst[i].replace(' ','') and 'def ' not in codeLst[i] and 'paraValueDict' not in codeLst[i] and 'callsiteInfoDict' not in codeLst[i]:
499  spaceNum=countSpace(codeLst[i])
500  dicString1=''
501  l=departAPI(callAPI)
502  l2=departAPI2(callAPI)
503  firstPart=''
504  for it in l2:
505  if '(' not in it:
506  firstPart+=it+'.'
507  firstPart=firstPart.rstrip('.')
508 
509  callsiteInfo={
510  'call_text': callAPI,
511  'format_api': callAPI,
512  }
513  displayCall=callAPI.replace('\n',' ')
514  dicStringComment=f'# PCART callsite: {displayCall}\n'
515  dicStringKey=f'__pcart_runtime_callsite_key__={repr(callKey)}\n'
516  dicString0=f'callsiteInfoDict[__pcart_runtime_callsite_key__]={repr(callsiteInfo)}\n'
517  if firstPart and (firstPart.split('.')[0] in targetLst or firstPart.split('.')[0]=='self') and len(l)==1:
518  lineNo = i + 1
519  receiverExpr=None
520  if firstPart.split('.')[0] in targetLst:
521  receiverExpr=getAssignReceiverExpr(root,source,firstPart,lineNo)
522  elif firstPart.split('.')[0]=='self':
523  methodName=callAPI.split('(')[0].split('.')[-1]
524  receiverExpr=getSelfReceiverExpr(root,lineNo,methodName)
525  if receiverExpr:
526  receiverExpr=receiverExpr.replace('\\','\\\\').replace('"','\\"')
527  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={{}}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"object\"]={firstPart}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"expr\"]=\"{receiverExpr}\"\n'
528  else:
529  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={firstPart}\n'
530  elif len(l)>1: #df.a(x).b(y), np.max(...), torch.nn.Sequential(...)
531  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={l[-2]}\n'
532 
533  #判断API是否为withitem中的别名调用 -- 2025/5/19
534  if firstPart and firstPart.split('.')[0] in withitem_call_names:
535  lineNo = i + 1
536  initialCallName = modifyWithName(firstPart, withitem_call_names, lineNo).rstrip('.')
537  # withitem接收者同时保存运行时对象和还原表达式,动态阶段按可用候选依次尝试
538  initialCallName=initialCallName.replace('\\','\\\\').replace('"','\\"')
539  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={{}}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"object\"]={firstPart}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"expr\"]=\"{initialCallName}\"\n'
540 
541  #再保存API的参数值
542  dicString2='paraValueDict[__pcart_runtime_callsite_key__]=['
543  for para in parameterLst: #
544  if '=' in para and "'='" not in para and '"="' not in para: #若参数的形式为key=f(x=1),只要确保=的前面不含括号即可
545  pos=para.find('=') #找到第一个=的位置
546  if '(' not in para[0:pos] and "'" not in para[0:pos] and '"' not in para[0:pos] and para[pos+1]!='=': #等号前面也不能出现引号,比如f('x= ',y=1)
547  para=para[pos+1:]
548 
549  para=para.lstrip('*') #有的参数会带*,2023-12-20
550  dicString2=dicString2+para+','
551  dicString2=dicString2.rstrip(',')+']\n'
552 
553  while spaceNum>0:
554  dicStringComment=' '+dicStringComment
555  dicStringKey=' '+dicStringKey
556  dicString0=' '+dicString0
557  if dicString1:
558  dicString1=' '+dicString1
559  dicString2=' '+dicString2
560  spaceNum-=1
561 
562  #插入的时候要考虑是否含有elif,如果有elif要把它插在elif后面
563  if 'elif' not in codeLst[i]:
564  insertIndex=getDecoratorInsertIndex(codeLst,i)
565  codeLst.insert(insertIndex,dicString2)
566  if dicString1:
567  codeLst.insert(insertIndex,dicString1)
568  codeLst.insert(insertIndex,dicString0)
569  codeLst.insert(insertIndex,dicStringKey)
570  codeLst.insert(insertIndex,dicStringComment)
571  else:
572  if dicString1:
573  dicString1=dicString1.lstrip(' ')#去掉之前添加的空格,重新计算开头的空格数
574  dicStringComment=dicStringComment.lstrip(' ')
575  dicStringKey=dicStringKey.lstrip(' ')
576  dicString0=dicString0.lstrip(' ')
577  dicString2=dicString2.lstrip(' ')
578  for j in range(i+1,len(codeLst)):
579  if codeLst[j]!='\n' and '#' not in codeLst[j]:
580  spaceNum=countSpace(codeLst[j])
581  while spaceNum>0:
582  dicStringComment=' '+dicStringComment
583  dicStringKey=' '+dicStringKey
584  dicString0=' '+dicString0
585  if dicString1:
586  dicString1=' '+dicString1
587  dicString2=' '+dicString2
588  spaceNum-=1
589 
590  codeLst.insert(j,dicString2)
591  if dicString1:
592  codeLst.insert(j,dicString1)
593  codeLst.insert(j,dicString0)
594  codeLst.insert(j,dicStringKey)
595  codeLst.insert(j,dicStringComment)
596  break
597 
598  break
599 
600 
601  if codeLst[0]=='pass\n':
602  codeLst=codeLst[1:]
603  with open(filePath,'w',encoding='UTF-8') as fw:
604  for it in codeLst:
605  fw.write(it)
606 
607 
608 
609 
621 def addDictAll(projPath,projName,filePath,copyRoot,runFileLst,libName,runPath,runCommand,pcresolveLookup=None):
622  with open(filePath,'r',encoding='UTF-8') as fr:
623  code=fr.read()
624  fr.seek(0) #将文件指针重新定位到文件的开头
625  codeLst=fr.readlines()
626  try:
627  root=ast.parse(code,filename='<unknown>',mode='exec')
628  except Exception as e:
629  print(f"addDictAll --> ast.parse failed, {filePath}: {e}")
630  return
631 
632  #处理相关路径
633  fileName = os.path.basename(filePath)[0:-3]
634  fileRelativePath=os.path.relpath(filePath,os.path.join(copyRoot,projName))
635  fileAbsolutePath=os.path.join(projPath,fileRelativePath)
636 
637  lineno=getImportLine(codeLst)
638  importDict='from recordValue import paraValueDict\nfrom recordValue import apiCoveredSet\nfrom recordValue import callsiteInfoDict\n'
639  codeLst.insert(lineno,importDict)
640  if pcresolveLookup is None:
641  _,callDict=getCallFunction(fileAbsolutePath,libName,projPath)
642  else:
643  _,callDict=getCallFunction(fileAbsolutePath,libName,projPath,pcresolveLookup=pcresolveLookup)
644 
645  targetLst=findAssignCall(root) #用来区分调用者是否来自赋值语句,比如a.f(), tf.f(), or self.f()
646 
647  #找出树中所有withitem call节点 -- 2025/5/19
648  try:
649  withitem_root = getAst(fileAbsolutePath)
650  except Exception:
651  withitem_root = root
652  withitem_visitor = WithVisitor()
653  withitem_visitor.visit(withitem_root)
654  withitem_call_names = withitem_visitor.get_withitem_call() #dict
655 
656  insertStartLine=0 #记录每次插桩的行
657  preInsertAPI='' #记录上一个插桩的API是哪个
658  preInsertAPICount=0 #记录上一个插桩行中出现了几次被插的API
659  for artifactId,record in callDict.items(): #key是调用点artifact id,value是结构化调用点记录
660  flag=0 #标记API是否找到了插桩的位置
661  lineno=int(record['lineno']) #这个lineno是原项目中的行数
662  callState=record['call_text']
663  paraStr=record['parameters']
664  callAPI=callState.replace(' ','')
665  if callAPI==preInsertAPI: #判断当前要处理的API与上一个API是否相同
666  preInsertAPICount-=1
667  if preInsertAPICount<=0:
668  insertStartLine+=1
669 
670  i=insertStartLine #从第i行开始向后找
671  while i<len(codeLst):
672  #API调用在i行代码中,且i行代码不是函数定义语句、插桩语句和运行覆盖检查语句
673  if callAPI in codeLst[i].replace(' ','') and 'def ' not in codeLst[i] and 'paraValueDict' not in codeLst[i] and 'apiCoveredSet' not in codeLst[i] and 'callsiteInfoDict' not in codeLst[i]:
674  if callAPI!=preInsertAPI:#只有当前API不等于上一个被插API时,才需要重新计算preAPICount
675  preInsertAPICount=codeLst[i].replace(' ','').count(callAPI)
676  preInsertAPI=callAPI
677  flag=1
678  spaceNum=countSpace(codeLst[i])
679  callSiteKey=artifactId
680  callsiteInfo={
681  'artifact_hash': record.get('artifact_hash',''),
682  'rel_path': record.get('rel_path',''),
683  'lineno': record.get('lineno'),
684  'col_offset': record.get('col_offset'),
685  'end_lineno': record.get('end_lineno'),
686  'end_col_offset': record.get('end_col_offset'),
687  'call_text': record.get('call_text',''),
688  'format_api': record.get('format_api',''),
689  }
690  displayPath=record.get('rel_path','')
691  displayLine=record.get('lineno')
692  displayColumn=record.get('col_offset')
693  displayCall=str(record.get('call_text','')).replace('\n',' ')
694  displayPrefix=f'{displayPath}:{displayLine}:{displayColumn} ' if displayPath else ''
695  dicStringComment=f'# PCART callsite: {displayPrefix}{displayCall}\n'
696  dicStringKey=f'__pcart_runtime_callsite_key__={repr(callSiteKey)}\n'
697  dicString0=f'callsiteInfoDict[__pcart_runtime_callsite_key__]={repr(callsiteInfo)}\n'
698  l=departAPI(callState)
699  l2=departAPI2(callState)
700  firstPart=''
701  for it in l2:
702  if '(' not in it:
703  firstPart+=it+'.'
704  firstPart=firstPart.rstrip('.')
705  dicString1=''
706 
707  #判断API是否具有上文依赖,比如self.f(x), a(x).b(y)中的a(x),或者a.f(x)中的a
708  #df.a(x).b(y)这种情况如何解决
709  #a.b.c(x)
710  # if '(' not in firstPart and (firstPart in targetLst or firstPart=='self'):
711  #self.f(x), a.f(x), a.b.c(x)
712  if firstPart and (firstPart.split('.')[0] in targetLst or firstPart.split('.')[0]=='self') and len(l)==1:
713  receiverExpr=None
714  if firstPart.split('.')[0] in targetLst:
715  receiverExpr=getAssignReceiverExpr(root,code,firstPart,lineno)
716  elif firstPart.split('.')[0]=='self':
717  methodName=callState.split('(')[0].split('.')[-1]
718  receiverExpr=getSelfReceiverExpr(root,lineno,methodName)
719  if receiverExpr:
720  receiverExpr=receiverExpr.replace('\\','\\\\').replace('"','\\"')
721  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={{}}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"object\"]={firstPart}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"expr\"]=\"{receiverExpr}\"\n'
722  else:
723  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={firstPart}\n'
724  elif len(l)>1: #df.a(x).b(y), np.max(...), torch.nn.Sequential(...)
725  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={l[-2]}\n'
726 
727  #判断API是否为withitem中的别名调用 -- 2025/5/19
728  if firstPart and firstPart.split('.')[0] in withitem_call_names:
729  initialCallName = modifyWithName(firstPart, withitem_call_names, lineno).rstrip('.')
730  initialCallName = initialCallName.rstrip('.')
731  # withitem接收者同时保存运行时对象和还原表达式,非withitem调用仍保持原有单值保存
732  initialCallName=initialCallName.replace('\\','\\\\').replace('"','\\"')
733  dicString1=f'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={{}}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"object\"]={firstPart}; paraValueDict[\"@\"+__pcart_runtime_callsite_key__][\"expr\"]=\"{initialCallName}\"\n'
734  #再保存API的参数值
735  dicString2='paraValueDict[__pcart_runtime_callsite_key__]=['
736  paraLst=getParameter(paraStr,space=0) #项目参数不去空格2023-12-14
737  for para in paraLst:
738  if '=' in para and "'='" not in para and '"="' not in para: #若参数的形式为key=f(x=1),只要确保=的前面不含括号即可
739  pos=para.find('=') #找到第一个=的位置,存在x=(a==b)和a==b形式
740  if '(' not in para[0:pos] and "'" not in para[0:pos] and '"' not in para[0:pos] and para[pos+1]!='=': #等号前面也不能出现引号,比如f('x= ',y=1)
741  para=para[pos+1:] #把参数的值保存下来
742 
743  para=para.lstrip('*') #有的参数会带*号,2023-12-20
744  dicString2=dicString2+para+','
745  dicString2=dicString2.rstrip(',')+']\n'
746 
747  dicString3='apiCoveredSet.add(__pcart_runtime_callsite_key__)\n'
748  while spaceNum>0:
749  dicStringComment=' '+dicStringComment
750  dicStringKey=' '+dicStringKey
751  dicString0=' '+dicString0
752  if dicString1:
753  dicString1=' '+dicString1
754  dicString2=' '+dicString2
755  dicString3=' '+dicString3
756  spaceNum-=1
757 
758  #插入的时候要考虑是否含有elif,如果有elif要把它插在elif后面
759  #不能将字典插入到if和elif之间,所以需要找到elif后的下一个非空行再插入
760  if 'elif' not in codeLst[i]:
761  insertIndex=getDecoratorInsertIndex(codeLst,i)
762  codeLst.insert(insertIndex,dicString3)
763  codeLst.insert(insertIndex,dicString2)
764  if dicString1:
765  codeLst.insert(insertIndex,dicString1)
766  codeLst.insert(insertIndex,dicString0)
767  codeLst.insert(insertIndex,dicStringKey)
768  codeLst.insert(insertIndex,dicStringComment)
769  #记录当前插在了哪一行
770  if dicString1:
771  insertStartLine=insertIndex+6
772  else:
773  insertStartLine=insertIndex+5
774  else:
775  if dicString1:
776  dicString1=dicString1.lstrip(' ') #去掉之前添加的空格,重新计算开头的空格数
777  dicStringComment=dicStringComment.lstrip(' ')
778  dicStringKey=dicStringKey.lstrip(' ')
779  dicString0=dicString0.lstrip(' ')
780  dicString2=dicString2.lstrip(' ')
781  dicString3=dicString3.lstrip(' ')
782  for j in range(i+1,len(codeLst)):
783  if codeLst[j]!='\n' and '#' not in codeLst[j]:
784  spaceNum=countSpace(codeLst[j])
785  while spaceNum>0:
786  dicStringComment=' '+dicStringComment
787  dicStringKey=' '+dicStringKey
788  dicString0=' '+dicString0
789  if dicString1:
790  dicString1=' '+dicString1
791  dicString2=' '+dicString2
792  dicString3=' '+dicString3
793  spaceNum-=1
794  codeLst.insert(j,dicString3)
795  codeLst.insert(j,dicString2)
796  if dicString1:
797  codeLst.insert(j,dicString1)
798  codeLst.insert(j,dicString0)
799  codeLst.insert(j,dicStringKey)
800  codeLst.insert(j,dicStringComment)
801  #记录当前插在了哪一行
802  if dicString1:
803  insertStartLine=j+6
804  else:
805  insertStartLine=j+5
806  break
807 
808  break
809 
810  i+=1
811 
812  if flag==0:
813  # Debug aid: log uninstrumentable callsites for diagnosis
814  # 调试辅助:记录无法插桩的调用点以便诊断
815  print(f"{fileName}#{lineno}-->{callState}\n")
816 
817  if codeLst[0]=='pass\n':
818  codeLst=codeLst[1:]
819  with open(filePath,'w',encoding='UTF-8') as fw:
820  for it in codeLst:
821  fw.write(it)
822 
823 
824 
825 
837 def handleRunFile(file,runPath,runCommand):
838  with open(file,'r',encoding='UTF-8') as fr:
839  codeLst=fr.readlines()
840  lineno=getImportLine(codeLst)
841  codeLst.insert(lineno,f"from recordValue import paraValueDict\n")
842 
843  if codeLst[0]=='pass\n':
844  codeLst=codeLst[1:]
845  with open(file,'w',encoding='UTF-8') as fw:
846  for it in codeLst:
847  fw.write(it)
848 
849 
853 def obtainDef(sourcePath):
854  with open(sourcePath,'r',encoding='UTF-8') as fr:
855  code=fr.read()
856  try:
857  root=ast.parse(code,filename='<unknown>',mode='exec') #将源码解析成AST语法树
858  except:
859  print(sourcePath)
860  return
861  fw=open('Copy/defFile.py','a',encoding='UTF-8')
862  for node in ast.iter_child_nodes(root):
863  if isinstance(node,ast.ClassDef) or isinstance(node,ast.FunctionDef):
864  s=ast.unparse(node)
865  fw.write(f"{s}\n")
866  fw.close()
867 
868 
869 
874 def modifyFromImport(filePath,importStatement):
875  # with open(filePath,'r') as fr:
876  with open(filePath, 'r', encoding='UTF-8') as fr:
877  codeLst=fr.readlines()
878 
879  s='\n'.join(importStatement)+'\n'
880  codeLst.insert(0,s)
881  # with open(filePath,'w') as fw:
882  with open(filePath, 'w', encoding='UTF-8') as fw:
883  for it in codeLst:
884  fw.write(it)
885 
886 
887 
888 
908 def saveConstantAssign(astNode, constantVar, nonConstantVar, decoratorLst):
909  flag = 0
910  astBody = []
911  for n in ast.walk(astNode):
912  if isinstance(n,ast.Assign):
913  value = n.value
914  valueAstContent = ast.dump(n.value)
915  targetAstContent = ast.dump(n.targets[0])
916  if isinstance(n.targets[0], ast.Name):
917  varName = n.targets[0].id
918  #如果赋值语句的变量名在装饰器列表中出现过,则保留该赋值语句 2025/5/13
919  if any(varName in decorator.split('.') for decorator in decoratorLst):
920  continue
921  #如果赋值语句的变量名是常量变量名,则保留该赋值语句 2025/5/31
922  if isinstance(value, ast.Constant):
923  if varName in nonConstantVar:
924  flag=1
925  break
926  else:
927  targets = n.targets
928  if targets:
929  target = targets[0]
930  if isinstance(target, ast.Name):
931  constantVar.append(target.id)
932  else:
933  # 使用正则表达式匹配单引号包围的内容
934  pattern = r"id='([^']*)'"
935  valueMatches = re.findall(pattern, valueAstContent)
936  targetMatches = re.findall(pattern, targetAstContent)
937  if not len(valueMatches):
938  if targetMatches[0] in nonConstantVar:
939  flag=1
940  break
941  else:
942  for match in valueMatches:
943  if match not in constantVar:
944  nonConstantVar.append(targetMatches[0])
945  flag=1
946  break
947  else:
948  flag=1
949  break
950  if flag==1:
951  return flag
952  else:
953  astBody.append(astNode)
954  return astBody
955 
956 
957 
958 
963 def saveStructure(projPath,libName):
964  pathObj=Path('DF')
965  pathObj.getPath(projPath)
966  filePath=[it for it in pathObj.path if it.endswith('py')]
967  for file in filePath:
968  _,callDict=getCallFunction(file,libName)
969  callLst=[record['call_text'] for record in callDict.values()]
970  # with open(file,'r') as fr:
971  with open(file, 'r', encoding='UTF-8') as fr:
972  code=fr.read()
973  try:
974  root=ast.parse(code,filename='<unknown>',mode='exec')
975  except Exception as e:
976  print(f"saveStructure --> ast parse failed in {file}: {e}")
977  continue
978  newBody=[]
979  constantVar = []
980  nonConstantVar = []
981  decoratorLst = extractDecorator(root)
982  #保留Import语句、函数和类定义
983  for node in root.body:
984  #增加AsyncFunctionDef节点信息保存 -- 2025/5/19
985  if isinstance(node,ast.Import) or isinstance(node,ast.ImportFrom) or isinstance(node,ast.ClassDef) or isinstance(node,ast.FunctionDef) or isinstance(node,ast.AsyncFunctionDef):
986  newBody.append(node)
987 
988  #保留包含常量的全局赋值语句和装饰器调用相关的赋值语句,例如:
989  # Case 1:
990  #1. a = 1
991  #2. b = 1
992  #3. c = a + b
993  #4. c = func(a,b)
994  #仅保留1,2,3行代码
995  # Case 2:
996  #1. app = Flask(__name__)
997  #2. @app.route("/")
998  #保留 app = Flask(__name__)以解决NameError
999  if isinstance(node,ast.Assign):
1000  result = saveConstantAssign(node,constantVar,nonConstantVar,decoratorLst)
1001  if result==1:
1002  continue
1003  else:
1004  newBody+=result
1005 
1006  #保留包含常量的局部赋值语句
1007  for node in ast.walk(root):
1008  if isinstance(node,ast.Assign):
1009  result = saveConstantAssign(node,constantVar,nonConstantVar,decoratorLst)
1010  if result==1:
1011  continue
1012  else:
1013  for item in result:
1014  if ast.dump(item) not in [ast.dump(i) for i in newBody]:
1015  newBody.append(item)
1016 
1017  root.body=newBody
1018  newFile=ast.unparse(root)
1019  with open(file,'w',encoding='utf-8') as fw:
1020  fw.write(f"{newFile}\n")
1021 
1022 
1023 
1024 
1030 def ignore_sym_links(directory, files):
1031  return [f for f in files if os.path.islink(os.path.join(directory, f))]
1032 
1033 
1034 
1035 
1041 def getLibImportLst(projPath,libName):
1042  lst=[]
1043  pathObj=Path('DF')
1044  pathObj.getPath(projPath)
1045  filePath=[file for file in pathObj.path if file.endswith('.py')]
1046  pattern=rf"(from {libName}|import {libName})" #确保库的前面不会出现其它字符
1047  for file in filePath:#下面的所有操作都是对项目副本进行的
1048  # with open(file,'r') as fr:
1049  with open(file, 'r', encoding='UTF-8') as fr:
1050  code=fr.read()
1051  try:
1052  root=ast.parse(code,filename='<unknown>',mode='exec')
1053  for node in ast.walk(root):
1054  if isinstance(node,ast.Import) or isinstance(node,ast.ImportFrom):
1055  s=ast.unparse(node)
1056  if bool(re.search(pattern,s)):
1057  lst.append(s)
1058  except Exception as e:
1059  print(f"getLibImportLst: ast parse failed, {file}, {e}")
1060 
1061 
1062  ansLst=list(set(lst))
1063  ansLst.sort(key=lst.index)
1064  return ansLst
1065 
1066 
1067 
1068 
1072 def convertTabsToSpaces(directory):
1073  for root, dirs, files in os.walk(directory):
1074  for file in files:
1075  if file.endswith('.py'):
1076  file_path = os.path.join(root, file)
1077  try:
1078  with open(file_path, 'r', encoding='utf-8') as f:
1079  content = f.read()
1080  # 将制表符转换为4个空格
1081  content = content.expandtabs(4)
1082  with open(file_path, 'w', encoding='utf-8') as f:
1083  f.write(content)
1084  except Exception as e:
1085  print(f"Error converting file {file_path}: {e}")
1086 
1087 
1088 
1089 
1095 def writeRecordValue(filePath,pklRelPath,useCallsiteName):
1096  scriptPath=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'Script','recordValue.py')
1097  with open(scriptPath,'r',encoding='UTF-8') as fr:
1098  content=fr.read()
1099  content=content.replace("'__PCART_PKL_REL_PATH__'",repr(pklRelPath))
1100  content=content.replace('__PCART_USE_CALLSITE_NAME__',str(bool(useCallsiteName)))
1101  with open(filePath,'w',encoding='UTF-8') as fw:
1102  fw.write(content)
1103 
1104 
1105 
1110 def scriptPath(scriptName):
1111  return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'Script',scriptName)
1112 
1113 
1114 
1121 def restoreProjectCopy(projName,copyRoot,tempDir):
1122  os.makedirs(tempDir,exist_ok=True)
1123  tempProjPath=os.path.join(tempDir,projName)
1124  if os.path.exists(tempProjPath):
1125  shutil.rmtree(tempProjPath)
1126  shutil.move(os.path.join(copyRoot, projName), tempProjPath)
1127  shutil.move(os.path.join(copyRoot, f'bak_{projName}'), os.path.join(copyRoot, projName))
1128  shutil.move(tempProjPath, os.path.join(copyRoot, f'bak_{projName}'))
1129 
1130 
1131 
1151 def codeProcess(projPath,runCommand,runPath,libName,workspace,pcresolveLookup=None):
1152  runtimePaths=getRuntimePaths(workspace)
1153  copyRoot=runtimePaths['copy_root']
1154  dynamicRoot=runtimePaths['dynamic_root']
1155  dataDir=runtimePaths['data_dir']
1156  #提取运行的文件
1157  runFileLst=[]
1158  runFile=getRunFile(runCommand)
1159  prefix='' #运行文件所在的目录,默认是在项目的一级子目录下
1160  # if '/' in runFile:
1161  if runFile and ('/' in runFile or '\\' in runFile):
1162  # prefix=runFile.rsplit('/',1)[0]
1163  prefix = os.path.dirname(runFile)
1164  # runFile=runFile.rsplit('/',1)[1] #去掉路径前缀,只保留文件名即run.py
1165  runFile = os.path.basename(runFile)
1166  if runFile:
1167  runFileLst.append(runFile)
1168  #这种情况针对于python run.py, run.py在其它目录中比如src,则prefix就是src
1169  #若是python src/run.py, 则prexfix和runPath是一致的
1170  if runPath!='' and prefix!=runPath:
1171  prefix=runPath
1172  # projName=projPath.split('/')[-1]
1173  projName = os.path.basename(projPath)
1174  copyProjPath=os.path.join(copyRoot,projName)
1175  importStatement=''
1176  for it in runFileLst:
1177  # it=it.rstrip('.py') #把文件名中的后缀去掉,但遇到display.py会变成displa
1178  it=it[0:-3]
1179  importStatement+=f"from {it} import *\n"
1180 
1181 
1182  #找出项目中所有和第三方库相关的import语句
1183  libImportLst=getLibImportLst(projPath,libName)
1184  libImportLst.append(importStatement)
1185 
1186  #清除Copy和Dynamic中遗留的项目信息,然后把新项目的信息拷贝进去
1187  if os.path.isdir(copyRoot):
1188  shutil.rmtree(copyRoot)
1189  os.makedirs(os.path.dirname(copyRoot) or '.',exist_ok=True)
1190  shutil.copytree(projPath,os.path.join(copyRoot,projName),ignore=ignore_sym_links)
1191  os.mkdir(os.path.join(copyRoot,'pkl'))
1192  if os.path.isdir(dynamicRoot):
1193  shutil.rmtree(dynamicRoot)
1194  os.makedirs(os.path.dirname(dynamicRoot) or '.',exist_ok=True)
1195  shutil.copytree(projPath,os.path.join(dynamicRoot,projName),ignore=ignore_sym_links)
1196 
1197  #去掉项目代码中的冗余信息,仅保存项目代码的结构信息(import,functionDef, classDef)
1198  saveStructure(os.path.join(dynamicRoot,projName),libName)
1199 
1200  dynamicScriptDir=os.path.join(dynamicRoot,projName,prefix)
1201  shutil.copy2(scriptPath('addValueForAPI.py'),dynamicScriptDir)
1202  shutil.copy2(codeUtils.__file__,dynamicScriptDir)
1203  shutil.copy2(scriptPath('dynamicMatch.py'),dynamicScriptDir)
1204  shutil.copy2(scriptPath('verifySingle.py'),dynamicScriptDir)
1205 
1206  #更新脚本中的from ... import ...语句,因为加载pkl的时候需要依赖于项目的结构信息
1207  modifyFromImport(os.path.join(dynamicScriptDir,'addValueForAPI.py'),libImportLst)
1208  modifyFromImport(os.path.join(dynamicScriptDir,'dynamicMatch.py'),libImportLst)
1209  modifyFromImport(os.path.join(dynamicScriptDir,'verifySingle.py'),libImportLst)
1210 
1211  #清除data中的数据
1212  if os.path.isdir(dataDir):
1213  shutil.rmtree(dataDir)
1214  os.makedirs(dataDir)
1215 
1216 
1217  #然后再把Copy中的项目制表符统一转化为空格,目的是为了插入字典的时候计算空格缩进
1218  convertTabsToSpaces(copyRoot)
1219 
1220  #把代码换行写的合成一行,并添加字典
1221  pathObj=Path('DF')
1222  pathObj.getPath(copyProjPath)
1223  filePath=[file for file in pathObj.path if file.endswith('.py')]
1224  for file in filePath:#下面的所有操作都是对项目副本进行的
1225  oneLine(file)
1226 
1227  #处理单行条件返回语句
1228  for file in filePath:
1230 
1231  #处理局部变量
1232  for file in filePath:
1233  convertLocalVar(file,libName)
1234 
1235 
1236  shutil.copytree(os.path.join(copyRoot,projName),os.path.join(copyRoot,f'bak_{projName}'))
1237 
1238  # 计算recordValue.py到Copy/pkl的相对路径
1239  pklDepth=1
1240  if prefix:
1241  pklDepth+=len([s for s in prefix.replace('\\','/').strip('/').split('/') if s])
1242  pklRelPath='/'.join(['..']*pklDepth+['pkl'])
1243  writeRecordValue(os.path.join(copyRoot,f'bak_{projName}',prefix,'recordValue.py'),pklRelPath,0)
1244 
1245 
1246  for file in filePath:
1247  if pcresolveLookup is None:
1248  addDictAll(projPath,projName,file,copyRoot,runFileLst,libName,runPath,runCommand)
1249  else:
1250  addDictAll(projPath,projName,file,copyRoot,runFileLst,libName,runPath,runCommand,pcresolveLookup=pcresolveLookup)
1251 
1252  #再对bak_proj中的运行文件进行插桩
1253  for file in runFileLst:
1254  file=os.path.join(copyRoot,f'bak_{projName}',prefix,file)
1255  handleRunFile(file,runPath,runCommand)
1256  # bak项目会在current pkl生成后换回Copy/{projName},其运行文件也依赖codeUtils
1257  shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,f'bak_{projName}',prefix))
1258 
1259  #处理完项目所有文件后,再给项目添加一个新的文件
1260  writeRecordValue(os.path.join(copyRoot,projName,prefix,'recordValue.py'),pklRelPath,1)
1261 
1262  shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,projName,runPath))
1263  if prefix!=runPath:
1264  shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,projName,prefix))
def departAPI2(s, separator='.')
Split API call string based on separator "." 根据"."拆分API调用字符串
Definition: codeUtils.py:190
def getParameter(p_string, separator=',', space=1)
Split parameter string into list of separated parameters 将参数字符串拆分成单个的参数
Definition: codeUtils.py:244
def getLastAPIParameter(apiStr)
Get parameter(s) of the last API from the API call string 获取最后一个API参数
Definition: codeUtils.py:334
def departAPI(s)
Split API call string based on parameter passing 根据参数传递拆分API调用字符串
Definition: codeUtils.py:143
def getCallFunction(filePath, libName, projPath=None, pcresolveLookup=None)
Extract all API calls from a given .py file 每次传进来一个.py文件,抽取所有的调用API.
Definition: getCall.py:205
def modifyWithName(callName, withitemCallName, lineno=None)
Restore the conventional API call path in the withitem (including AsyncWith) call form 还原withitemAPI调...
Definition: getCall.py:157
def convertLocalVar(filePath, libName)
Convert the ListComp and DictComp statements to variable assignment statements 将列表推导式listComp和字典推导式Di...
Definition: preprocess.py:157
def codeProcess(projPath, runCommand, runPath, libName, workspace, pcresolveLookup=None)
Code processing 代码预处理
Definition: preprocess.py:1151
def convertTabsToSpaces(directory)
Convert tabs to spaces in all Python files within a directory 将目录下所有Python文件中的制表符转换为空格
Definition: preprocess.py:1072
def getDictVar(root, ansLst)
Get the local variable in the dictionary comprehension 获取字典推导式中的局部变量
Definition: preprocess.py:133
def oneLine(filePath)
Convert the multi-line parameter calls in the code into a single line to facilitate insertion into di...
Definition: preprocess.py:67
def expandConditionalReturn(filePath)
Expand the single-line conditional return statement into a multi-line if-else structure 展开单行条件retur...
Definition: preprocess.py:83
def obtainDef(sourcePath)
Extract class and function definitions from a source file 从源文件中提取类和函数定义
Definition: preprocess.py:853
def addDictSingle(callAPI, filePath, callKey)
Code instrumentation for a single API call within a source file 源文件单个API调用代码插桩
Definition: preprocess.py:478
def getImportLine(codeLst)
Determine the instrumentation line for import statements 确定import语句插桩行
Definition: preprocess.py:405
def writeRecordValue(filePath, pklRelPath, useCallsiteName)
Write recordValue.py used by instrumented project files 写入插桩文件共享的recordValue.py.
Definition: preprocess.py:1095
def getLibImportLst(projPath, libName)
Get all lib-related import statements 获取所有第三方库相关的import语句
Definition: preprocess.py:1041
def restoreProjectCopy(projName, copyRoot, tempDir)
Restore the project copy prepared for single-API instrumentation 恢复用于单个API插桩的项目副本
Definition: preprocess.py:1121
def getListVar(root, ansLst)
Get the local variable in the list comprehension 获取列表推导式中的局部变量
Definition: preprocess.py:106
def countBracket(s)
Count the number of different types of brackets ((), [], {}) in a string 计算字符串中各类括号((),...
Definition: preprocess.py:32
def getSelfReceiverExpr(root, lineno, methodName)
Resolve the visible base-class expression for an inherited self receiver 还原继承场景中self调用者可见的基类表达式
Definition: preprocess.py:362
def scriptPath(scriptName)
Get source script path from PCART repository 获取PCART仓库中的辅助脚本路径
Definition: preprocess.py:1110
def countSpace(s)
Count the number of spaces at the beginning of a string 计算字符串的前面有多少个空格
Definition: preprocess.py:386
def saveConstantAssign(astNode, constantVar, nonConstantVar, decoratorLst)
Save assignment statement with constant values 保存常量赋值语句
Definition: preprocess.py:908
def addDictAll(projPath, projName, filePath, copyRoot, runFileLst, libName, runPath, runCommand, pcresolveLookup=None)
Code instrumentation for all API calls within a project source file 项目源文件所有API调用代码插桩
Definition: preprocess.py:621
def modifyFromImport(filePath, importStatement)
Save import statement to source file 将import语句保存到源码中
Definition: preprocess.py:874
def getAssignReceiverExpr(root, source, receiverName, lineno)
Resolve the source expression assigned to a receiver alias 还原调用者别名在当前作用域内的赋值来源表达式
Definition: preprocess.py:277
def ignore_sym_links(directory, files)
Determine the soft link files in a directory 确定文件夹中的软链接文件
Definition: preprocess.py:1030
def handleRunFile(file, runPath, runCommand)
Code instrumentation for project run file 项目运行文件代码插桩
Definition: preprocess.py:837
def findAssignCall(root)
Find assignment call statements 获取赋值调用语句
Definition: preprocess.py:260
def extractDecorator(root)
Extract decorator API calls 抽取项目中第三方库装饰器调用
Definition: preprocess.py:433
def saveStructure(projPath, libName)
Save structure information of the project 保存项目的结构信息
Definition: preprocess.py:963
def getDecoratorInsertIndex(codeLst, index)
Get decorator block start index 获取装饰器块起始行号
Definition: preprocess.py:454
def getAst(filePath, strFlag=0)
Get AST for code 将代码转化为Ast树
Definition: tool.py:33
def getRunFile(runCommand)
Extract the .py script filename from a run command 从运行命令中提取.py脚本文件名
Definition: tool.py:438
def getRuntimePaths(workspace)
Return runtime artifact paths for the current execution 返回当前执行使用的运行产物路径
Definition: workspace.py:197