25 ARTIFACT_HASH_PATTERN=re.compile(
r'(?:^|__)([0-9a-f]{64})$')
34 match=ARTIFACT_HASH_PATTERN.search(artifactId)
47 if artifactHash
and artifactId.endswith(artifactHash):
48 displayName=artifactId[:-len(artifactHash)].rstrip(
'_')
49 return displayName
or artifactHash
60 fileName=re.sub(
r'[^0-9A-Za-z_.-]+',
'_',fileName).strip(
'._')
61 length=255-len(extension)
if extension
else 255
62 if len(fileName)<=length:
63 return fileName+extension
66 return fileName[:length]+extension
67 suffix=
'__'+artifactHash
68 prefixLength=max(0,length-len(suffix))
69 prefix=fileName[:prefixLength].rstrip(
'_')
70 return prefix+suffix+extension
88 p_string=p_string.replace(
' ',
'')
104 count_single_yinhao=0
105 count_double_yinhao=0
107 for index,value
in enumerate(p_string):
109 if (value==
"'" or count_single_yinhao)
and not count_double_yinhao:
111 count_single_yinhao+=1
112 if count_single_yinhao&1:
115 elif (value==
'"' or count_double_yinhao)
and not count_single_yinhao:
117 count_double_yinhao+=1
118 if count_double_yinhao&1:
121 count_single_yinhao=0
122 count_double_yinhao=0
133 count_right_middle+=1
147 if count_left_min!=count_right_min:
150 if count_left_middle!=count_right_middle:
153 if count_left_hua!=count_right_hua:
156 if flagMin
and flagMid
and flagHua:
157 parameters.append(
''.join(stack[0:-1]))
160 elif index==len(p_string)-1:
161 parameters.append(
''.join(stack))
175 with open(filePath,
'r',encoding=
'UTF-8')
as f:
177 root=ast.parse(s,filename=
'<unknown>',mode=
'exec')
179 root=ast.parse(filePath,filename=
'<unknown>',mode=
'exec')
191 with open(filePath,
'r',encoding=
'UTF-8')
as f:
195 if (it[0:6]==
'import' or it[0:4]==
'from')
and 'torch' in it
and 'torchvision' not in it
and 'torch_' not in it
and '.torch' not in it:
218 for index,value
in enumerate(s):
226 if left==right
and left>0
and right>0:
228 ans+=
''.join(stack[0:pos])
232 elif index==len(s)-1:
244 if left==right
and left>0
and right>0:
273 if left==right
and left>0
and right>0:
299 for i
in range(len(s)):
312 if leftMid!=rightMid:
316 if leftMin
and rightMin
and leftMin==rightMin
and flagMid:
317 ansLst.append(stack[0:i+1])
320 if leftMid
and rightMid:
325 ansLst.append(stack[0:i+1])
348 for index,value
in enumerate(s):
359 count_right_middle+=1
361 elif value==separator
and ((count_left_min>count_right_min)
or (count_left_middle>count_right_middle)):
369 if count_left_min!=count_right_min:
372 if count_left_middle!=count_right_middle:
374 if flagMin
and flagMid:
375 ansLst.append(
''.join(lst))
377 elif index==len(s)-1:
378 ansLst.append(
''.join(lst))
390 lst=list(dic.values())
392 if isinstance(it,str):
409 p = os.path.basename(p)
411 for i
in range(len(p)):
415 versionLst.append(p[index:])
416 versionLst.sort(key=
lambda it:
cmp(it))
434 for i
in range(len(version)):
435 if version[i].isalpha():
438 if index!=len(version):
439 innerVersion=re.findall(
r'\d+',version[index:])[0]
440 Type=re.findall(
r'[a-zA-Z]+',version[index:])[0]
442 if len(innerVersion)==1:
443 innerVersion=
'0.00000'+innerVersion
445 innerVersion=
'0.0000'+innerVersion
447 if len(innerVersion)==1:
448 innerVersion=
'0.000'+innerVersion
450 innerVersion=
'0.00'+innerVersion
452 if len(innerVersion)==1:
453 innerVersion=
'0.0'+innerVersion
455 innerVersion=
'0.'+innerVersion
458 lst=version[0:index].split(
'.')
469 return float(v)-(1-float(innerVersion))
483 fileName=fileName.replace(
' ',
'')
484 fileName=fileName.replace(
'/',
'')
485 fileName=fileName.replace(
'\\',
'')
486 if len(extension) != 0:
487 length=255-len(extension)
490 fileName=fileName.split(
'(')[0] +
'_' + hashlib.md5(fileName.encode()).hexdigest()[:16]
491 fileName=fileName[0:length]
509 tailSpaceNum=width-2-len(s)-1
510 fw.write(
'| '+s+
' '*tailSpaceNum+
'|'+
'\n')
517 tailSpaceNum=width-2-len(s2)-1
518 s2=
'| '+s2+
' '*tailSpaceNum+
'|'+
'\n'
534 fw=open(savePath,
'w',encoding=
'UTF-8')
539 unknownCompatibleNum=0
546 totalAPINum+=Tuple[2]
548 for callAPI,subDict
in dic.items():
549 if subDict[
'Coverage']==
'No':
553 if subDict[
'Compatible']==
'Yes':
555 elif subDict[
'Compatible']==
'No':
558 unknownCompatibleNum+=1
560 if 'Repair <Successful>' in subDict:
562 elif 'Repair <Failed>' in subDict:
564 elif 'Repair <Unknown>' in subDict:
568 libName=libName.capitalize()
569 fw.write(f
"Run Command: {runCommand}\n")
570 fw.write(f
"Total File Number: {totalFileNum}\n")
571 fw.write(f
"Total {libName} Invoked API Number: {totalAPINum}\n")
572 fw.write(f
"Not Covered {libName} Invoked API Number: {notCoverNum}/{totalAPINum}\n")
573 fw.write(f
"Covered {libName} Invoked API Number: {totalAPINum-notCoverNum}/{totalAPINum}\n\n")
575 fw.write(f
"Compatible {libName} Invoked API Number: {compatibleNum}/{totalAPINum-notCoverNum}\n")
576 fw.write(f
"Unknown Compatible {libName} Invoked API Number: {unknownCompatibleNum}/{totalAPINum-notCoverNum}\n\n")
578 fw.write(f
"Incompatible {libName} Invoked API Number: {incompatibleNum}/{totalAPINum-notCoverNum}\n")
579 fw.write(f
"-> Successfully Repaired {libName} Invoked API number: {successRepairNum}/{incompatibleNum}\n")
580 fw.write(f
"-> Failed to Repair {libName} Invoked API Number: {failedRepairNum}/{incompatibleNum}\n")
581 fw.write(f
"-> Unknown Repair Status {libName} Invoked API Number: {unknownRepairNum}/{incompatibleNum}\n\n")
586 invokedAPINum=Tuple[2]
589 title=f
"File #{fileCount}: {filePath} has {invokedAPINum} {libName}-Invoked API(s)"
591 fontSpaceNum=(width-len(title)-2)//2
592 tailSpaceNum=width-2-fontSpaceNum-len(title)
593 title=
'|'+
' '*fontSpaceNum+title+
' '*tailSpaceNum+
'|'+
'\n'
598 for callAPI,subDict
in dic.items():
599 displayCall=subDict.pop(
'Invoked API',callAPI)
600 tempStr1=f
"Invoked API #{index}: {displayCall}"
602 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
604 for k,v
in subDict.items():
607 if cnt!=len(subDict):
608 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
611 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
612 fw.write(
'|'+
'-'*(width-2)+
'|'+
'\n')
614 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
628 with open(configPath,
'r',encoding=
'UTF-8')
as fr:
631 if isinstance(dic[key], str):
632 dic[key] = dic[key].strip()
633 runCommand=dic[
'runCommand']
634 return dic[
'projPath'],runCommand,dic[
'runFilePath'],dic[
'libName'],dic[
'currentVersion'],dic[
'targetVersion'],dic[
'currentEnv'],dic[
'targetEnv']
644 if os.path.isabs(config):
646 if os.path.exists(config):
647 return os.path.abspath(config)
648 return os.path.join(repoRoot,
'Configure',config)
660 expanded=os.path.expanduser(path)
661 if os.name==
'nt' and expanded.startswith(
'/')
and not expanded.startswith(
'//'):
663 if os.path.isabs(expanded):
664 return os.path.abspath(expanded)
665 return os.path.abspath(os.path.join(repoRoot,expanded))
673 """Raised when PCART cannot safely execute a configured run command."""
682 baseName=os.path.basename(command.replace(
'\\',
'/')).lower()
683 if baseName
in (
'python',
'python.exe'):
685 if re.match(
r'^python3(\.\d+)?(\.exe)?$',baseName):
696 baseName=os.path.basename(command.replace(
'\\',
'/')).lower()
697 return baseName
in (
'py',
'py.exe')
706 if tokens
and re.match(
r'^-\d(\.\d+)?(-\d+)?$',tokens[0]):
719 tokens=shlex.split(runCommand)
721 return runCommand,
'python'
724 simpleTokens=runCommand.split(maxsplit=1)
726 rest=simpleTokens[1].lstrip()
if len(simpleTokens)>1
else ''
729 rest=simpleTokens[1].lstrip()
if len(simpleTokens)>1
else ''
730 restTokens=shlex.split(rest)
734 return shlex.join(tokens[1:]),
'python'
737 if tokens[0]==
'-m' or tokens[0].endswith(
'.py'):
738 return runCommand,
'python'
739 return runCommand,
'console'
749 normalizedEnvPath=os.path.abspath(envPath)
751 if platform.system()==
'Windows':
752 scriptsPath=os.path.join(normalizedEnvPath,
'Scripts')
754 os.path.join(scriptsPath,commandName),
755 os.path.join(scriptsPath,commandName+
'.exe'),
756 os.path.join(scriptsPath,commandName+
'.cmd'),
757 os.path.join(scriptsPath,commandName+
'.bat'),
760 candidates.append(os.path.join(normalizedEnvPath,
'bin',commandName))
762 for candidate
in candidates:
763 if os.path.exists(candidate):
766 candidateStr=
', '.join(candidates)
767 raise FileNotFoundError(
768 f
"Cannot find console command '{commandName}' under virtual "
769 f
"environment root: {normalizedEnvPath}. Tried: {candidateStr}"
781 if commandType==
'python':
784 args=shlex.split(normalizedCommand)
789 except FileNotFoundError
as e:
791 return [commandPath] + args[1:]
800 tokens=shlex.split(runCommand)
802 candidate=token.split(
'::',1)[0]
803 if candidate.endswith(
'.py'):
815 if not os.path.exists(basePath):
816 raise FileNotFoundError(
817 f
"Cannot find Python directory: {basePath} does not exist"
820 for entry
in os.listdir(basePath):
821 full_path = os.path.join(basePath, entry)
822 if os.path.isdir(full_path):
823 if entry.startswith(
"python"):
826 raise FileNotFoundError(
827 f
"Cannot find Python directory under {basePath}/pythonxx.xx"
838 if platform.system() ==
'Windows':
839 sitePackages=os.path.join(envPath,
"Lib",
"site-packages")
841 sitePackages=os.path.join(
findPythonDir(os.path.join(envPath,
"lib")),
"site-packages")
843 if libName==
"tensorflow":
844 tensorflowCore=os.path.join(sitePackages,
"tensorflow_core")
845 if os.path.isdir(tensorflowCore):
846 return tensorflowCore
848 return os.path.join(sitePackages, libName)
857 normalizedEnvPath = os.path.abspath(envPath)
858 if platform.system() ==
'Windows':
860 os.path.join(normalizedEnvPath,
'python.exe'),
861 os.path.join(normalizedEnvPath,
'Scripts',
'python.exe'),
865 os.path.join(normalizedEnvPath,
'bin',
'python'),
868 for candidate
in candidates:
869 if os.path.exists(candidate):
872 candidate_str =
', '.join(candidates)
873 raise FileNotFoundError(
874 f
"Cannot find Python executable under virtual environment root: "
875 f
"{normalizedEnvPath}. Tried: {candidate_str}"
887 with open(f
"Configure/{configPath}",
'r',encoding=
'UTF-8')
as fr:
889 libName=dic[
'libName']
890 currentVersion=dic[
'currentVersion']
891 targetVersion=dic[
'targetVersion']
892 currentEnvPath=dic[
'currentEnv']
893 targetEnvPath=dic[
'targetEnv']
898 return currentVersion, targetVersion, currentSourceCodePath, targetSourceCodePath
911 if isinstance(node, ast.Return)
and isinstance(node.value, ast.IfExp):
915 body=[ast.Return(value=ifExp.body)],
916 orelse=[ast.Return(value=ifExp.orelse)]