23 from Tool.codeUtils
import getArtifactHash, getArtifactDisplayName, shortenArtifactFileName, getFileName, getParameter, removeParameter, getLastAPIParameter, departAPI, departAPI2
35 with tokenize.open(filePath)
as f:
37 root=ast.parse(s,filename=
'<unknown>',mode=
'exec')
39 root=ast.parse(filePath,filename=
'<unknown>',mode=
'exec')
51 with open(filePath,
'r',encoding=
'UTF-8')
as f:
55 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:
67 lst=list(dic.values())
69 if isinstance(it,str):
86 p = os.path.basename(p)
88 for i
in range(len(p)):
92 versionLst.append(p[index:])
93 versionLst.sort(key=
lambda it:
cmp(it))
111 for i
in range(len(version)):
112 if version[i].isalpha():
115 if index!=len(version):
116 innerVersion=re.findall(
r'\d+',version[index:])[0]
117 Type=re.findall(
r'[a-zA-Z]+',version[index:])[0]
119 if len(innerVersion)==1:
120 innerVersion=
'0.00000'+innerVersion
122 innerVersion=
'0.0000'+innerVersion
124 if len(innerVersion)==1:
125 innerVersion=
'0.000'+innerVersion
127 innerVersion=
'0.00'+innerVersion
129 if len(innerVersion)==1:
130 innerVersion=
'0.0'+innerVersion
132 innerVersion=
'0.'+innerVersion
135 lst=version[0:index].split(
'.')
146 return float(v)-(1-float(innerVersion))
161 for start
in range(0,max(len(s),1),contentWidth):
162 content=s[start:start+contentWidth]
163 tailSpaceNum=width-2-len(content)-1
164 fw.write(
'| '+content+
' '*tailSpaceNum+
'|'+
'\n')
176 fw=open(savePath,
'w',encoding=
'UTF-8')
181 unknownCompatibleNum=0
188 totalAPINum+=Tuple[2]
190 for callAPI,subDict
in dic.items():
191 if subDict[
'Coverage']==
'No':
195 if subDict[
'Compatible']==
'Yes':
197 elif subDict[
'Compatible']==
'No':
200 unknownCompatibleNum+=1
202 if 'Repair <Successful>' in subDict:
204 elif 'Repair <Failed>' in subDict:
206 elif 'Repair <Unknown>' in subDict:
210 libName=libName.capitalize()
211 fw.write(f
"Run Command: {runCommand}\n")
212 fw.write(f
"Total File Number: {totalFileNum}\n")
213 fw.write(f
"Total {libName} Invoked API Number: {totalAPINum}\n")
214 fw.write(f
"Not Covered {libName} Invoked API Number: {notCoverNum}/{totalAPINum}\n")
215 fw.write(f
"Covered {libName} Invoked API Number: {totalAPINum-notCoverNum}/{totalAPINum}\n\n")
217 fw.write(f
"Compatible {libName} Invoked API Number: {compatibleNum}/{totalAPINum-notCoverNum}\n")
218 fw.write(f
"Unknown Compatible {libName} Invoked API Number: {unknownCompatibleNum}/{totalAPINum-notCoverNum}\n\n")
220 fw.write(f
"Incompatible {libName} Invoked API Number: {incompatibleNum}/{totalAPINum-notCoverNum}\n")
221 fw.write(f
"-> Successfully Repaired {libName} Invoked API number: {successRepairNum}/{incompatibleNum}\n")
222 fw.write(f
"-> Failed to Repair {libName} Invoked API Number: {failedRepairNum}/{incompatibleNum}\n")
223 fw.write(f
"-> Unknown Repair Status {libName} Invoked API Number: {unknownRepairNum}/{incompatibleNum}\n\n")
228 invokedAPINum=Tuple[2]
231 title=f
"File #{fileCount}: {filePath} has {invokedAPINum} {libName}-Invoked API(s)"
233 fontSpaceNum=(width-len(title)-2)//2
234 tailSpaceNum=width-2-fontSpaceNum-len(title)
235 title=
'|'+
' '*fontSpaceNum+title+
' '*tailSpaceNum+
'|'+
'\n'
240 for callAPI,subDict
in dic.items():
241 displayCall=subDict.pop(
'Invoked API',callAPI)
242 tempStr1=f
"Invoked API #{index}: {displayCall}"
244 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
246 for k,v
in subDict.items():
249 if cnt!=len(subDict):
250 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
253 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
254 fw.write(
'|'+
'-'*(width-2)+
'|'+
'\n')
256 fw.write(
'|'+
' '*(width-2)+
'|'+
'\n')
270 with open(configPath,
'r',encoding=
'UTF-8')
as fr:
273 if isinstance(dic[key], str):
274 dic[key] = dic[key].strip()
275 runCommand=dic[
'runCommand']
276 return dic[
'projPath'],runCommand,dic[
'runFilePath'],dic[
'libName'],dic[
'currentVersion'],dic[
'targetVersion'],dic[
'currentEnv'],dic[
'targetEnv']
286 if os.path.isabs(config):
288 if os.path.exists(config):
289 return os.path.abspath(config)
290 return os.path.join(repoRoot,
'Configure',config)
302 expanded=os.path.expanduser(path)
303 if os.name==
'nt' and expanded.startswith(
'/')
and not expanded.startswith(
'//'):
305 if os.path.isabs(expanded):
306 return os.path.abspath(expanded)
307 return os.path.abspath(os.path.join(repoRoot,expanded))
315 """Raised when PCART cannot safely execute a configured run command."""
324 baseName=os.path.basename(command.replace(
'\\',
'/')).lower()
325 if baseName
in (
'python',
'python.exe'):
327 if re.match(
r'^python3(\.\d+)?(\.exe)?$',baseName):
338 baseName=os.path.basename(command.replace(
'\\',
'/')).lower()
339 return baseName
in (
'py',
'py.exe')
348 if tokens
and re.match(
r'^-\d(\.\d+)?(-\d+)?$',tokens[0]):
361 tokens=shlex.split(runCommand)
363 return runCommand,
'python'
366 simpleTokens=runCommand.split(maxsplit=1)
368 rest=simpleTokens[1].lstrip()
if len(simpleTokens)>1
else ''
371 rest=simpleTokens[1].lstrip()
if len(simpleTokens)>1
else ''
372 restTokens=shlex.split(rest)
376 return shlex.join(tokens[1:]),
'python'
379 if tokens[0]==
'-m' or tokens[0].endswith(
'.py'):
380 return runCommand,
'python'
381 return runCommand,
'console'
391 normalizedEnvPath=os.path.abspath(envPath)
393 if platform.system()==
'Windows':
394 scriptsPath=os.path.join(normalizedEnvPath,
'Scripts')
396 os.path.join(scriptsPath,commandName),
397 os.path.join(scriptsPath,commandName+
'.exe'),
398 os.path.join(scriptsPath,commandName+
'.cmd'),
399 os.path.join(scriptsPath,commandName+
'.bat'),
402 candidates.append(os.path.join(normalizedEnvPath,
'bin',commandName))
404 for candidate
in candidates:
405 if os.path.exists(candidate):
408 candidateStr=
', '.join(candidates)
409 raise FileNotFoundError(f
"Cannot find console command '{commandName}' under virtual " f
"environment root: {normalizedEnvPath}. Tried: {candidateStr}")
420 if commandType==
'python':
423 args=shlex.split(normalizedCommand)
428 except FileNotFoundError
as e:
430 return [commandPath] + args[1:]
439 tokens=shlex.split(runCommand)
441 candidate=token.split(
'::',1)[0]
442 if candidate.endswith(
'.py'):
454 if not os.path.exists(basePath):
455 raise FileNotFoundError(f
"Cannot find Python directory: {basePath} does not exist")
457 for entry
in os.listdir(basePath):
458 full_path = os.path.join(basePath, entry)
459 if os.path.isdir(full_path):
460 if entry.startswith(
"python"):
463 raise FileNotFoundError(f
"Cannot find Python directory under {basePath}/pythonxx.xx")
473 if platform.system() ==
'Windows':
474 sitePackages=os.path.join(envPath,
"Lib",
"site-packages")
476 sitePackages=os.path.join(
findPythonDir(os.path.join(envPath,
"lib")),
"site-packages")
478 if libName==
"tensorflow":
479 tensorflowCore=os.path.join(sitePackages,
"tensorflow_core")
480 if os.path.isdir(tensorflowCore):
481 return tensorflowCore
483 return os.path.join(sitePackages, libName)
492 normalizedEnvPath = os.path.abspath(envPath)
493 if platform.system() ==
'Windows':
495 os.path.join(normalizedEnvPath,
'python.exe'),
496 os.path.join(normalizedEnvPath,
'Scripts',
'python.exe'),
500 os.path.join(normalizedEnvPath,
'bin',
'python'),
503 for candidate
in candidates:
504 if os.path.exists(candidate):
507 candidate_str =
', '.join(candidates)
508 raise FileNotFoundError(f
"Cannot find Python executable under virtual environment root: " f
"{normalizedEnvPath}. Tried: {candidate_str}")
521 repoRoot=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
523 with open(resolvedConfigPath,
'r',encoding=
'UTF-8')
as fr:
525 libName=dic[
'libName']
526 currentVersion=dic[
'currentVersion']
527 targetVersion=dic[
'targetVersion']
534 return currentVersion, targetVersion, currentSourceCodePath, targetSourceCodePath
547 if isinstance(node, ast.Return)
and isinstance(node.value, ast.IfExp):
549 newIf = ast.If(test=ifExp.test,body=[ast.Return(value=ifExp.body)],orelse=[ast.Return(value=ifExp.orelse)])