20 from Extract.getCall
import getCallFunction, modifyWithName
22 from Tool.tool
import getAst,getParameter,getLastAPIParameter,departAPI,departAPI2,ConditionalReturnTransformer, getFileName, getRunFile
23 from Tool
import codeUtils
24 from Tool.workspace
import getRuntimePaths
60 return minL,minR,midL,midR,huaL,huaR
71 with open(filePath,
'w',encoding=
'UTF-8')
as fw:
72 newCode=ast.unparse(root)
74 fw.write(f
"{newCode}\n")
75 except Exception
as e:
76 print(f
"oneLine --> {filePath} parse to ast failed: {e}")
86 transformer = ConditionalReturnTransformer()
87 new_root = transformer.visit(root)
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}")
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)
118 s1=f
"{var}=[{temp} {s}][0]"
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)
145 s1=f
"{var}=[{temp} {s}][0]"
158 with open(filePath,
'r',encoding=
'UTF-8')
as fr:
159 codeLst=fr.readlines()
161 for i
in range(len(codeLst)):
162 s=codeLst[i].lstrip(
' ').rstrip(
' ')
164 root=ast.parse(s,filename=
'<unknown>',mode=
'exec')
165 except Exception
as e:
171 for node
in ast.walk(root):
172 if isinstance(node,ast.ListComp):
174 if isinstance(node,ast.DictComp):
177 if not listComp
and not dictComp:
183 callLst=[record[
'call_text']
for record
in callDict.values()]
185 for node
in ast.walk(root):
186 if isinstance(node, ast.Call):
187 callState=ast.unparse(node)
188 callState=callState.replace(
' ',
'').replace(
'"',
'').replace(
"'",
'')
190 if callState==it.replace(
' ',
'').replace(
'"',
'').replace(
"'",
''):
206 exceptStr=
"except:\n"
209 tryStr=
' '*spaceNum*1+tryStr
210 it=
' '*spaceNum*1+
' '*4+it+
'\n'
211 exceptStr=
' '*spaceNum*1+exceptStr
212 passStr=
' '*spaceNum*1+
' '*4+passStr
215 it=
' '*spaceNum*1+it+
'\n'
216 passStr=
' '*spaceNum*1+passStr
219 s=tryStr+it+exceptStr+passStr
230 exceptStr=
"except:\n"
233 tryStr=
' '*spaceNum*1+tryStr
234 it=
' '*spaceNum*1+
' '*4+it+
'\n'
235 exceptStr=
' '*spaceNum*1+exceptStr
236 passStr=
' '*spaceNum*1+
' '*4+passStr
239 it=
' '*spaceNum*1+it+
'\n'
240 passStr=
' '*spaceNum*1+passStr
243 s=tryStr+it+exceptStr+passStr
249 with open(filePath,
'w',encoding=
'UTF-8')
as fw:
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)
278 aliasName=receiverName.split(
'.')[0]
279 receiverSuffix=receiverName[len(aliasName):]
284 scopeBodies=[root.body]
285 for node
in ast.walk(root):
286 if isinstance(node,(ast.FunctionDef,ast.AsyncFunctionDef,ast.ClassDef)):
288 end=getattr(node,
'end_lineno',start)
289 if start<lineno<=end:
290 scopeBodies.append(node.body)
292 for body
in scopeBodies:
294 if not hasattr(stmt,
'lineno')
or stmt.lineno>=lineno:
296 if isinstance(stmt,(ast.If,ast.For,ast.AsyncFor,ast.While,ast.Try,ast.With,ast.AsyncWith)):
300 if isinstance(stmt,ast.Assign):
303 elif isinstance(stmt,ast.AnnAssign):
305 targets=[stmt.target]
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)
312 expr=sourceExpr.strip()
320 for body
in scopeBodies:
322 if not hasattr(stmt,
'lineno')
or stmt.lineno>=lineno:
325 if isinstance(stmt,ast.Assign):
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)
334 for node
in ast.walk(root):
335 if isinstance(node,(ast.FunctionDef,ast.AsyncFunctionDef)):
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)
342 localNames.add(node.args.vararg.arg)
344 localNames.add(node.args.kwarg.arg)
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:
351 return f
'({expr}){receiverSuffix}'
363 for node
in ast.walk(root):
364 if not isinstance(node,ast.ClassDef):
367 end=getattr(node,
'end_lineno',start)
368 if not (start<lineno<=end)
or len(node.bases)==0:
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:
376 return ast.unparse(node.bases[0])
408 for i
in range(len(codeLst)):
409 if 'import' in codeLst[i]
and '__future__' in codeLst[i]:
414 if index==-1
and '"""' in codeLst[0]:
415 for i
in range(0,len(codeLst)):
416 if '"""' in codeLst[i]:
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):
439 decoratorLst.append(ast.unparse(decorator.func))
456 if not codeLst[index].lstrip().startswith(
'@'):
462 if codeLst[j].lstrip().startswith(
'@')
and countSpace(codeLst[j])==spaceNum:
479 with open(filePath,
'r',encoding=
'UTF-8')
as fr:
480 codeLst=fr.readlines()
481 source=
''.join(codeLst)
484 importDict=
'from recordValue import paraValueDict\nfrom recordValue import callsiteInfoDict\n'
485 codeLst.insert(lineno,importDict)
493 withitem_visitor = WithVisitor()
494 withitem_visitor.visit(root)
495 withitem_call_names = withitem_visitor.get_withitem_call()
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]:
507 firstPart=firstPart.rstrip(
'.')
510 'call_text': callAPI,
511 'format_api': callAPI,
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:
520 if firstPart.split(
'.')[0]
in targetLst:
522 elif firstPart.split(
'.')[0]==
'self':
523 methodName=callAPI.split(
'(')[0].split(
'.')[-1]
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'
529 dicString1=f
'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={firstPart}\n'
531 dicString1=f
'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={l[-2]}\n'
534 if firstPart
and firstPart.split(
'.')[0]
in withitem_call_names:
536 initialCallName =
modifyWithName(firstPart, withitem_call_names, lineNo).rstrip(
'.')
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'
542 dicString2=
'paraValueDict[__pcart_runtime_callsite_key__]=['
543 for para
in parameterLst:
544 if '=' in para
and "'='" not in para
and '"="' not in para:
546 if '(' not in para[0:pos]
and "'" not in para[0:pos]
and '"' not in para[0:pos]
and para[pos+1]!=
'=':
549 para=para.lstrip(
'*')
550 dicString2=dicString2+para+
','
551 dicString2=dicString2.rstrip(
',')+
']\n'
554 dicStringComment=
' '+dicStringComment
555 dicStringKey=
' '+dicStringKey
556 dicString0=
' '+dicString0
558 dicString1=
' '+dicString1
559 dicString2=
' '+dicString2
563 if 'elif' not in codeLst[i]:
565 codeLst.insert(insertIndex,dicString2)
567 codeLst.insert(insertIndex,dicString1)
568 codeLst.insert(insertIndex,dicString0)
569 codeLst.insert(insertIndex,dicStringKey)
570 codeLst.insert(insertIndex,dicStringComment)
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]:
582 dicStringComment=
' '+dicStringComment
583 dicStringKey=
' '+dicStringKey
584 dicString0=
' '+dicString0
586 dicString1=
' '+dicString1
587 dicString2=
' '+dicString2
590 codeLst.insert(j,dicString2)
592 codeLst.insert(j,dicString1)
593 codeLst.insert(j,dicString0)
594 codeLst.insert(j,dicStringKey)
595 codeLst.insert(j,dicStringComment)
601 if codeLst[0]==
'pass\n':
603 with open(filePath,
'w',encoding=
'UTF-8')
as fw:
621 def addDictAll(projPath,projName,filePath,copyRoot,runFileLst,libName,runPath,runCommand,pcresolveLookup=None):
622 with open(filePath,
'r',encoding=
'UTF-8')
as fr:
625 codeLst=fr.readlines()
627 root=ast.parse(code,filename=
'<unknown>',mode=
'exec')
628 except Exception
as e:
629 print(f
"addDictAll --> ast.parse failed, {filePath}: {e}")
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)
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:
643 _,callDict=
getCallFunction(fileAbsolutePath,libName,projPath,pcresolveLookup=pcresolveLookup)
649 withitem_root =
getAst(fileAbsolutePath)
652 withitem_visitor = WithVisitor()
653 withitem_visitor.visit(withitem_root)
654 withitem_call_names = withitem_visitor.get_withitem_call()
659 for artifactId,record
in callDict.items():
661 lineno=int(record[
'lineno'])
662 callState=record[
'call_text']
663 paraStr=record[
'parameters']
664 callAPI=callState.replace(
' ',
'')
665 if callAPI==preInsertAPI:
667 if preInsertAPICount<=0:
671 while i<len(codeLst):
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:
675 preInsertAPICount=codeLst[i].replace(
' ',
'').count(callAPI)
679 callSiteKey=artifactId
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',
''),
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'
704 firstPart=firstPart.rstrip(
'.')
712 if firstPart
and (firstPart.split(
'.')[0]
in targetLst
or firstPart.split(
'.')[0]==
'self')
and len(l)==1:
714 if firstPart.split(
'.')[0]
in targetLst:
716 elif firstPart.split(
'.')[0]==
'self':
717 methodName=callState.split(
'(')[0].split(
'.')[-1]
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'
723 dicString1=f
'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={firstPart}\n'
725 dicString1=f
'paraValueDict[\"@\"+__pcart_runtime_callsite_key__]={l[-2]}\n'
728 if firstPart
and firstPart.split(
'.')[0]
in withitem_call_names:
729 initialCallName =
modifyWithName(firstPart, withitem_call_names, lineno).rstrip(
'.')
730 initialCallName = initialCallName.rstrip(
'.')
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'
735 dicString2=
'paraValueDict[__pcart_runtime_callsite_key__]=['
738 if '=' in para
and "'='" not in para
and '"="' not in para:
740 if '(' not in para[0:pos]
and "'" not in para[0:pos]
and '"' not in para[0:pos]
and para[pos+1]!=
'=':
743 para=para.lstrip(
'*')
744 dicString2=dicString2+para+
','
745 dicString2=dicString2.rstrip(
',')+
']\n'
747 dicString3=
'apiCoveredSet.add(__pcart_runtime_callsite_key__)\n'
749 dicStringComment=
' '+dicStringComment
750 dicStringKey=
' '+dicStringKey
751 dicString0=
' '+dicString0
753 dicString1=
' '+dicString1
754 dicString2=
' '+dicString2
755 dicString3=
' '+dicString3
760 if 'elif' not in codeLst[i]:
762 codeLst.insert(insertIndex,dicString3)
763 codeLst.insert(insertIndex,dicString2)
765 codeLst.insert(insertIndex,dicString1)
766 codeLst.insert(insertIndex,dicString0)
767 codeLst.insert(insertIndex,dicStringKey)
768 codeLst.insert(insertIndex,dicStringComment)
771 insertStartLine=insertIndex+6
773 insertStartLine=insertIndex+5
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]:
786 dicStringComment=
' '+dicStringComment
787 dicStringKey=
' '+dicStringKey
788 dicString0=
' '+dicString0
790 dicString1=
' '+dicString1
791 dicString2=
' '+dicString2
792 dicString3=
' '+dicString3
794 codeLst.insert(j,dicString3)
795 codeLst.insert(j,dicString2)
797 codeLst.insert(j,dicString1)
798 codeLst.insert(j,dicString0)
799 codeLst.insert(j,dicStringKey)
800 codeLst.insert(j,dicStringComment)
815 print(f
"{fileName}#{lineno}-->{callState}\n")
817 if codeLst[0]==
'pass\n':
819 with open(filePath,
'w',encoding=
'UTF-8')
as fw:
838 with open(file,
'r',encoding=
'UTF-8')
as fr:
839 codeLst=fr.readlines()
841 codeLst.insert(lineno,f
"from recordValue import paraValueDict\n")
843 if codeLst[0]==
'pass\n':
845 with open(file,
'w',encoding=
'UTF-8')
as fw:
854 with open(sourcePath,
'r',encoding=
'UTF-8')
as fr:
857 root=ast.parse(code,filename=
'<unknown>',mode=
'exec')
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):
876 with open(filePath,
'r', encoding=
'UTF-8')
as fr:
877 codeLst=fr.readlines()
879 s=
'\n'.join(importStatement)+
'\n'
882 with open(filePath,
'w', encoding=
'UTF-8')
as fw:
911 for n
in ast.walk(astNode):
912 if isinstance(n,ast.Assign):
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
919 if any(varName
in decorator.split(
'.')
for decorator
in decoratorLst):
922 if isinstance(value, ast.Constant):
923 if varName
in nonConstantVar:
930 if isinstance(target, ast.Name):
931 constantVar.append(target.id)
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:
942 for match
in valueMatches:
943 if match
not in constantVar:
944 nonConstantVar.append(targetMatches[0])
953 astBody.append(astNode)
965 pathObj.getPath(projPath)
966 filePath=[it
for it
in pathObj.path
if it.endswith(
'py')]
967 for file
in filePath:
969 callLst=[record[
'call_text']
for record
in callDict.values()]
971 with open(file,
'r', encoding=
'UTF-8')
as fr:
974 root=ast.parse(code,filename=
'<unknown>',mode=
'exec')
975 except Exception
as e:
976 print(f
"saveStructure --> ast parse failed in {file}: {e}")
983 for node
in root.body:
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):
999 if isinstance(node,ast.Assign):
1007 for node
in ast.walk(root):
1008 if isinstance(node,ast.Assign):
1014 if ast.dump(item)
not in [ast.dump(i)
for i
in newBody]:
1015 newBody.append(item)
1018 newFile=ast.unparse(root)
1019 with open(file,
'w',encoding=
'utf-8')
as fw:
1020 fw.write(f
"{newFile}\n")
1031 return [f
for f
in files
if os.path.islink(os.path.join(directory, f))]
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:
1049 with open(file,
'r', encoding=
'UTF-8')
as fr:
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):
1056 if bool(re.search(pattern,s)):
1058 except Exception
as e:
1059 print(f
"getLibImportLst: ast parse failed, {file}, {e}")
1062 ansLst=list(set(lst))
1063 ansLst.sort(key=lst.index)
1073 for root, dirs, files
in os.walk(directory):
1075 if file.endswith(
'.py'):
1076 file_path = os.path.join(root, file)
1078 with open(file_path,
'r', encoding=
'utf-8')
as f:
1081 content = content.expandtabs(4)
1082 with open(file_path,
'w', encoding=
'utf-8')
as f:
1084 except Exception
as e:
1085 print(f
"Error converting file {file_path}: {e}")
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:
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:
1111 return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'Script',scriptName)
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}'))
1151 def codeProcess(projPath,runCommand,runPath,libName,workspace,pcresolveLookup=None):
1153 copyRoot=runtimePaths[
'copy_root']
1154 dynamicRoot=runtimePaths[
'dynamic_root']
1155 dataDir=runtimePaths[
'data_dir']
1161 if runFile
and (
'/' in runFile
or '\\' in runFile):
1163 prefix = os.path.dirname(runFile)
1165 runFile = os.path.basename(runFile)
1167 runFileLst.append(runFile)
1170 if runPath!=
'' and prefix!=runPath:
1173 projName = os.path.basename(projPath)
1174 copyProjPath=os.path.join(copyRoot,projName)
1176 for it
in runFileLst:
1179 importStatement+=f
"from {it} import *\n"
1184 libImportLst.append(importStatement)
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)
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)
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)
1212 if os.path.isdir(dataDir):
1213 shutil.rmtree(dataDir)
1214 os.makedirs(dataDir)
1222 pathObj.getPath(copyProjPath)
1223 filePath=[file
for file
in pathObj.path
if file.endswith(
'.py')]
1224 for file
in filePath:
1228 for file
in filePath:
1232 for file
in filePath:
1236 shutil.copytree(os.path.join(copyRoot,projName),os.path.join(copyRoot,f
'bak_{projName}'))
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)
1246 for file
in filePath:
1247 if pcresolveLookup
is None:
1248 addDictAll(projPath,projName,file,copyRoot,runFileLst,libName,runPath,runCommand)
1250 addDictAll(projPath,projName,file,copyRoot,runFileLst,libName,runPath,runCommand,pcresolveLookup=pcresolveLookup)
1253 for file
in runFileLst:
1254 file=os.path.join(copyRoot,f
'bak_{projName}',prefix,file)
1257 shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,f
'bak_{projName}',prefix))
1260 writeRecordValue(os.path.join(copyRoot,projName,prefix,
'recordValue.py'),pklRelPath,1)
1262 shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,projName,runPath))
1264 shutil.copy2(codeUtils.__file__,os.path.join(copyRoot,projName,prefix))
def departAPI2(s, separator='.')
Split API call string based on separator "." 根据"."拆分API调用字符串
def getParameter(p_string, separator=',', space=1)
Split parameter string into list of separated parameters 将参数字符串拆分成单个的参数
def getLastAPIParameter(apiStr)
Get parameter(s) of the last API from the API call string 获取最后一个API参数
def departAPI(s)
Split API call string based on parameter passing 根据参数传递拆分API调用字符串
def getCallFunction(filePath, libName, projPath=None, pcresolveLookup=None)
Extract all API calls from a given .py file 每次传进来一个.py文件,抽取所有的调用API.
def modifyWithName(callName, withitemCallName, lineno=None)
Restore the conventional API call path in the withitem (including AsyncWith) call form 还原withitemAPI调...
def convertLocalVar(filePath, libName)
Convert the ListComp and DictComp statements to variable assignment statements 将列表推导式listComp和字典推导式Di...
def codeProcess(projPath, runCommand, runPath, libName, workspace, pcresolveLookup=None)
Code processing 代码预处理
def convertTabsToSpaces(directory)
Convert tabs to spaces in all Python files within a directory 将目录下所有Python文件中的制表符转换为空格
def getDictVar(root, ansLst)
Get the local variable in the dictionary comprehension 获取字典推导式中的局部变量
def oneLine(filePath)
Convert the multi-line parameter calls in the code into a single line to facilitate insertion into di...
def expandConditionalReturn(filePath)
Expand the single-line conditional return statement into a multi-line if-else structure 展开单行条件retur...
def obtainDef(sourcePath)
Extract class and function definitions from a source file 从源文件中提取类和函数定义
def addDictSingle(callAPI, filePath, callKey)
Code instrumentation for a single API call within a source file 源文件单个API调用代码插桩
def getImportLine(codeLst)
Determine the instrumentation line for import statements 确定import语句插桩行
def writeRecordValue(filePath, pklRelPath, useCallsiteName)
Write recordValue.py used by instrumented project files 写入插桩文件共享的recordValue.py.
def getLibImportLst(projPath, libName)
Get all lib-related import statements 获取所有第三方库相关的import语句
def restoreProjectCopy(projName, copyRoot, tempDir)
Restore the project copy prepared for single-API instrumentation 恢复用于单个API插桩的项目副本
def getListVar(root, ansLst)
Get the local variable in the list comprehension 获取列表推导式中的局部变量
def countBracket(s)
Count the number of different types of brackets ((), [], {}) in a string 计算字符串中各类括号((),...
def getSelfReceiverExpr(root, lineno, methodName)
Resolve the visible base-class expression for an inherited self receiver 还原继承场景中self调用者可见的基类表达式
def scriptPath(scriptName)
Get source script path from PCART repository 获取PCART仓库中的辅助脚本路径
def countSpace(s)
Count the number of spaces at the beginning of a string 计算字符串的前面有多少个空格
def saveConstantAssign(astNode, constantVar, nonConstantVar, decoratorLst)
Save assignment statement with constant values 保存常量赋值语句
def addDictAll(projPath, projName, filePath, copyRoot, runFileLst, libName, runPath, runCommand, pcresolveLookup=None)
Code instrumentation for all API calls within a project source file 项目源文件所有API调用代码插桩
def modifyFromImport(filePath, importStatement)
Save import statement to source file 将import语句保存到源码中
def getAssignReceiverExpr(root, source, receiverName, lineno)
Resolve the source expression assigned to a receiver alias 还原调用者别名在当前作用域内的赋值来源表达式
def ignore_sym_links(directory, files)
Determine the soft link files in a directory 确定文件夹中的软链接文件
def handleRunFile(file, runPath, runCommand)
Code instrumentation for project run file 项目运行文件代码插桩
def findAssignCall(root)
Find assignment call statements 获取赋值调用语句
def extractDecorator(root)
Extract decorator API calls 抽取项目中第三方库装饰器调用
def saveStructure(projPath, libName)
Save structure information of the project 保存项目的结构信息
def getDecoratorInsertIndex(codeLst, index)
Get decorator block start index 获取装饰器块起始行号
def getRuntimePaths(workspace)
Return runtime artifact paths for the current execution 返回当前执行使用的运行产物路径