PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
Classes | Functions | Variables
tool Namespace Reference

Provide utility functions for processing API calls and source files. More...

Classes

class  ConditionalReturnTransformer
 Convert conditional return statement to multi-line if-else structure 展开单行条件返回语句为多行if-else结构 More...
 
class  UnsupportedRunCommand
 Unsupported run command exception 不支持的运行命令异常 More...
 

Functions

def buildRunCommand (runCommand, envPath)
 Build subprocess argv for project run command 为被测项目运行命令构造subprocess argv. More...
 
def cmp (version)
 Normalize and compare version strings 归一化并比较版本号字符串 More...
 
def departAPI (s)
 Split API call string based on parameter passing 根据参数传递拆分API调用字符串 More...
 
def departAPI2 (s, separator='.')
 Split API call string based on separator "." 根据"."拆分API调用字符串 More...
 
def findPythonDir (basePath)
 Get Python interpreter path 获取Python解释器路径 More...
 
def getArtifactDisplayName (artifactId)
 Return readable artifact display name 返回可读运行产物展示名 More...
 
def getArtifactHash (artifactId)
 Return artifact hash from an artifact id 从运行产物id中提取hash. More...
 
def getAst (filePath, strFlag=0)
 Get AST for code 将代码转化为Ast树 More...
 
def getFileName (fileName, extension)
 Normalize file name 给文件取名字 More...
 
def getImportLst (filePath)
 Extract import statements from a source file (currently torch-specific) 从源码文件中提取import语句(目前仅筛选torch相关) More...
 
def getLastAPIParameter (apiStr)
 Get parameter(s) of the last API from the API call string 获取最后一个API参数 More...
 
def getParameter (p_string, separator=',', space=1)
 Split parameter string into list of separated parameters 将参数字符串拆分成单个的参数 More...
 
def getRunFile (runCommand)
 Extract the .py script filename from a run command 从运行命令中提取.py脚本文件名 More...
 
def getSourceCodePath (configPath)
 Get source code path of the lib 获取库源码路径 More...
 
def getVersionLst (libPath)
 Get all version numbers from a library package directory 给定库的路径,获得该库的所有版本号 More...
 
def isDynamic (dic)
 Check if a match dictionary contains dynamic (string) match results 检查匹配字典是否包含动态(字符串)匹配结果 More...
 
def isPyLauncher (command)
 Check whether command is Windows py launcher 判断命令是否为Windows py启动器 More...
 
def isPythonExecutable (command)
 Check whether command is a Python executable name/path 判断命令是否为Python解释器名称或路径 More...
 
def loadConfig (configPath)
 Load PCART's configuration file 加载PCART配置文件 More...
 
def normalizeRunCommand (runCommand)
 Normalize project run command 归一化被测项目运行命令 More...
 
def removeParameter (s, flag=0)
 Remove parameter(s) from API call string 去掉API中的参数部分 More...
 
def resolveConfigFilePath (config, repoRoot)
 Resolve PCART config file path 解析PCART配置文件路径 More...
 
def resolveConfigValuePath (repoRoot, path)
 Resolve path value loaded from PCART config 解析PCART配置字段中的路径值 More...
 
def resolveConsoleExecutable (envPath, commandName)
 Resolve console script executable from a virtual environment 从虚拟环境中解析console script可执行文件 More...
 
def resolveLibSourceCodePath (envPath, libName)
 Resolve the source package directory for a library in a virtual environment 从虚拟环境中解析库源码目录 More...
 
def resolvePythonExecutable (envPath)
 Resolve Python executable from a virtual environment root 从虚拟环境根目录解析 Python 解释器路径 More...
 
def save2txt (lst, libName, runCommand, savePath)
 Save PCART report 保存PCART报告 More...
 
def shortenArtifactFileName (fileName, extension)
 Shorten artifact file name while preserving full hash 缩短运行产物文件名并保留完整hash. More...
 
def stripPyLauncherOption (tokens)
 Remove version option after py launcher 去除py启动器后的版本选项 More...
 
def writeLine (width, s, fw)
 Format output 格式化输出 More...
 

Variables

 ARTIFACT_HASH_PATTERN = re.compile(r'(?:^|__)([0-9a-f]{64})$')
 

Detailed Description

Provide utility functions for processing API calls and source files.

Collection of shared utility functions: AST parsing, parameter string splitting/parsing (getParameter, departAPI, departAPI2), configuration loading, run command normalization and executable resolution, report formatting (save2txt), and AST transformation (ConditionalReturnTransformer). 共享工具函数集合:AST解析、参数字符串拆分/解析(getParameter、departAPI、 departAPI2)、配置加载、运行命令归一化和可执行文件解析、报告格式化 (save2txt)、AST转换(ConditionalReturnTransformer)。

Function Documentation

◆ buildRunCommand()

def tool.buildRunCommand (   runCommand,
  envPath 
)

Build subprocess argv for project run command 为被测项目运行命令构造subprocess argv.

Parameters
runCommandThe project run command
envPathThe virtual environment root path
Returns
Command argv list for subprocess.run

Definition at line 779 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmp()

def tool.cmp (   version)

Normalize and compare version strings 归一化并比较版本号字符串

Converts version strings like "3.10.12", "4.5.0rc1" into a single float for comparison. Alpha/beta/rc suffixes are mapped to fractional parts (a < b < rc). 将版本号"3.10.12"、"4.5.0rc1"等转化为浮点数进行比较, alpha/beta/rc后缀映射为小数部分(a < b < rc)。

Parameters
versionThe version string to normalize
Returns
A float representing the normalized version

Definition at line 431 of file tool.py.

Here is the caller graph for this function:

◆ departAPI()

def tool.departAPI (   s)

Split API call string based on parameter passing 根据参数传递拆分API调用字符串

For example, a.b(x).c(y).d(z) --> ['a.b(x)', 'a.b(x).c(y)', 'a.b(x).c(y).d(z)'] 比如a.b(x).c(y).d(z)拆成3个API,分别是['a.b(x)', 'a.b(x).c(y)', 'a.b(x).c(y).d(z)'] 还有特殊的调用形式a.b['x'](y)

Parameters
sThe API call string
Returns
ansLst The split result

Definition at line 292 of file tool.py.

Here is the caller graph for this function:

◆ departAPI2()

def tool.departAPI2 (   s,
  separator = '.' 
)

Split API call string based on separator "." 根据"."拆分API调用字符串

For example, a.b(x).c(y).d(z) --> ['a', 'b(x)', 'c(y)', 'd(z)'] 比如a.b(x).c(y).d(z)拆成4个API,分别是['a', 'b(x)', 'c(y)', 'd(z)']

Parameters
sThe API call string
separatorThe separator symbol: .
Returns
ansLst The split result

Definition at line 340 of file tool.py.

Here is the caller graph for this function:

◆ findPythonDir()

def tool.findPythonDir (   basePath)

Get Python interpreter path 获取Python解释器路径

Parameters
basePathThe virtual environment path
Returns
full_path The path of Python interpreter

Definition at line 814 of file tool.py.

Here is the caller graph for this function:

◆ getArtifactDisplayName()

def tool.getArtifactDisplayName (   artifactId)

Return readable artifact display name 返回可读运行产物展示名

Parameters
artifactIdThe artifact id
Returns
display name

Definition at line 45 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getArtifactHash()

def tool.getArtifactHash (   artifactId)

Return artifact hash from an artifact id 从运行产物id中提取hash.

Parameters
artifactIdThe artifact id
Returns
artifact hash or empty string

Definition at line 33 of file tool.py.

Here is the caller graph for this function:

◆ getAst()

def tool.getAst (   filePath,
  strFlag = 0 
)

Get AST for code 将代码转化为Ast树

Parameters
filePathThe code file path
strFlagDetermine the code from file or API: 0 for file; 1 for API call string
Returns
root The parsed AST root

Definition at line 173 of file tool.py.

Here is the caller graph for this function:

◆ getFileName()

def tool.getFileName (   fileName,
  extension 
)

Normalize file name 给文件取名字

Parameters
fileNameTypically, the API call string or callsite artifact id is input as the file name
extensionThe extension of the file
Returns
fileName The normalized file name

Definition at line 479 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getImportLst()

def tool.getImportLst (   filePath)

Extract import statements from a source file (currently torch-specific) 从源码文件中提取import语句(目前仅筛选torch相关)

Parameters
filePathThe source file path
Returns
List of matching import statement strings

Definition at line 189 of file tool.py.

◆ getLastAPIParameter()

def tool.getLastAPIParameter (   apiStr)

Get parameter(s) of the last API from the API call string 获取最后一个API参数

For example, a(x).b(x=c.d(1),y=b((1,2),5),w).c(1,2,3,4) --> 1,2,3,4 比如,a(x).b(x=c.d(1),y=b((1,2),5),w).c(1,2,3,4),获取c的参数1,2,3,4

Parameters
apiStrThe API call string
Returns
ans The parameter(s) of the last API

Definition at line 262 of file tool.py.

Here is the caller graph for this function:

◆ getParameter()

def tool.getParameter (   p_string,
  separator = ',',
  space = 1 
)

Split parameter string into list of separated parameters 将参数字符串拆分成单个的参数

apiName(x,y="<bold>Hello, World!</bold>",z:int,w=(p1,p2={1,(1m,23)}),device: Union[Device, int] = None, abbreviated: bool ={'a','b'}) -> str 默认按逗号进行拆分,也可按'.'进行拆分,比如a.b.c 拆分参数的时候没有考虑到x="hello,wolrd"带冒号的情况,会错误拆成两个

Parameters
p_stringParameter string
separatorThe separator, ',' is the default value
spaceDetermine whether to remove the space in the parameter string
Returns
parameters List of separated parameters

Definition at line 85 of file tool.py.

Here is the caller graph for this function:

◆ getRunFile()

def tool.getRunFile (   runCommand)

Extract the .py script filename from a run command 从运行命令中提取.py脚本文件名

Parameters
runCommandThe project run command
Returns
The .py filename, or ''

Definition at line 799 of file tool.py.

Here is the caller graph for this function:

◆ getSourceCodePath()

def tool.getSourceCodePath (   configPath)

Get source code path of the lib 获取库源码路径

f"/dataset/zhang/anaconda3/envs/3d/lib/{pythonxx.xx}/" + f"site-packages/{torch}"

Parameters
configPathPCART's configuration file
Returns
(currentVersion, targetVersion, currentSourceCodePath, targetSourceCodePath) Tuple of current version, target version, virtual environment paths of current version and target version

Definition at line 886 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getVersionLst()

def tool.getVersionLst (   libPath)

Get all version numbers from a library package directory 给定库的路径,获得该库的所有版本号

Parameters
libPathThe library package directory path
Returns
List of version strings sorted in ascending order

Definition at line 403 of file tool.py.

Here is the call graph for this function:

◆ isDynamic()

def tool.isDynamic (   dic)

Check if a match dictionary contains dynamic (string) match results 检查匹配字典是否包含动态(字符串)匹配结果

Parameters
dicThe match result dictionary
Returns
1 if any value is a string (dynamic match), 0 otherwise (static match or empty)

Definition at line 389 of file tool.py.

◆ isPyLauncher()

def tool.isPyLauncher (   command)

Check whether command is Windows py launcher 判断命令是否为Windows py启动器

Parameters
commandCommand name or executable path
Returns
True if command points to py launcher

Definition at line 695 of file tool.py.

Here is the caller graph for this function:

◆ isPythonExecutable()

def tool.isPythonExecutable (   command)

Check whether command is a Python executable name/path 判断命令是否为Python解释器名称或路径

Parameters
commandCommand name or executable path
Returns
True if command points to Python executable

Definition at line 681 of file tool.py.

Here is the caller graph for this function:

◆ loadConfig()

def tool.loadConfig (   configPath)

Load PCART's configuration file 加载PCART配置文件

Parameters
configPathThe path of configuration file
Returns
(dic['projPath'],runCommand,dic['runFilePath'],dic['libName'],dic['currentVersion'],dic['targetVersion'],dic['currentEnv'],dic['targetEnv']) tuple of configuration entries

Definition at line 627 of file tool.py.

Here is the caller graph for this function:

◆ normalizeRunCommand()

def tool.normalizeRunCommand (   runCommand)

Normalize project run command 归一化被测项目运行命令

只识别Python解释器结构,其余命令在执行阶段从虚拟环境解析

Parameters
runCommandThe project run command
Returns
(normalizedCommand, commandType)

Definition at line 718 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeParameter()

def tool.removeParameter (   s,
  flag = 0 
)

Remove parameter(s) from API call string 去掉API中的参数部分

For example, a.b(x,y(2)).c(z=1).d(w=[(1,2)]) --> a.b.c.d 比如a.b(x,y(2)).c(z=1).d(w=[(1,2)])变成a.b.c.d

Parameters
sThe API call string
flagDetermine remove all parameters or the last parameter for the input API call: 0 for all parameters; 1 for the last parameters
Returns
ans The API call string without parameter(s)

Definition at line 210 of file tool.py.

Here is the caller graph for this function:

◆ resolveConfigFilePath()

def tool.resolveConfigFilePath (   config,
  repoRoot 
)

Resolve PCART config file path 解析PCART配置文件路径

Parameters
configThe config file path or config file name under Configure
repoRootThe PCART repository root path
Returns
Absolute config file path

Definition at line 643 of file tool.py.

Here is the caller graph for this function:

◆ resolveConfigValuePath()

def tool.resolveConfigValuePath (   repoRoot,
  path 
)

Resolve path value loaded from PCART config 解析PCART配置字段中的路径值

Parameters
repoRootThe PCART repository root path
pathThe path value read from config
Returns
Absolute path for relative values, original empty value otherwise

Definition at line 657 of file tool.py.

Here is the caller graph for this function:

◆ resolveConsoleExecutable()

def tool.resolveConsoleExecutable (   envPath,
  commandName 
)

Resolve console script executable from a virtual environment 从虚拟环境中解析console script可执行文件

Parameters
envPathThe virtual environment root path
commandNameConsole command name
Returns
resolvedPath Path of the console executable

Definition at line 748 of file tool.py.

Here is the caller graph for this function:

◆ resolveLibSourceCodePath()

def tool.resolveLibSourceCodePath (   envPath,
  libName 
)

Resolve the source package directory for a library in a virtual environment 从虚拟环境中解析库源码目录

Parameters
envPathThe virtual environment root path
libNameThe configured public library name
Returns
sourceCodePath Library source code path

Definition at line 837 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resolvePythonExecutable()

def tool.resolvePythonExecutable (   envPath)

Resolve Python executable from a virtual environment root 从虚拟环境根目录解析 Python 解释器路径

Parameters
envPathThe virtual environment root path
Returns
resolvedPath Absolute path of the Python executable

Definition at line 856 of file tool.py.

Here is the caller graph for this function:

◆ save2txt()

def tool.save2txt (   lst,
  libName,
  runCommand,
  savePath 
)

Save PCART report 保存PCART报告

Parameters
lstPCART's result
libNameThe lib name
runCommandThe run command of the project
savePathThe save path

Definition at line 533 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shortenArtifactFileName()

def tool.shortenArtifactFileName (   fileName,
  extension 
)

Shorten artifact file name while preserving full hash 缩短运行产物文件名并保留完整hash.

Parameters
fileNameThe artifact file stem
extensionThe extension of the file
Returns
fileName The shortened file name

Definition at line 59 of file tool.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stripPyLauncherOption()

def tool.stripPyLauncherOption (   tokens)

Remove version option after py launcher 去除py启动器后的版本选项

Parameters
tokensCommand tokens after py launcher
Returns
Tokens without version selector

Definition at line 705 of file tool.py.

Here is the caller graph for this function:

◆ writeLine()

def tool.writeLine (   width,
  s,
  fw 
)

Format output 格式化输出

Specifies the maximum width for each line of string in the file. If a line exceeds this width, it is wrapped. If the wrapped line still exceeds the maximum width, the wrapping process continues. 规定文件中每行的字符串的宽度,如果超出宽度就换行写,如果换行之后还超过了最大宽度,则继续向下换行

Parameters
widthThe width of each line of string
sThe output string
fwFile writer

Definition at line 507 of file tool.py.

Here is the caller graph for this function:

Variable Documentation

◆ ARTIFACT_HASH_PATTERN

ARTIFACT_HASH_PATTERN = re.compile(r'(?:^|__)([0-9a-f]{64})$')

Definition at line 25 of file tool.py.