PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
codeUtils.py
Go to the documentation of this file.
1 
8 
9 import hashlib
10 import re
11 
12 
13 ARTIFACT_HASH_PATTERN=re.compile(r'(?:^|__)([0-9a-f]{64})$')
14 
15 
16 
21 def getArtifactHash(artifactId):
22  match=ARTIFACT_HASH_PATTERN.search(artifactId)
23  if match:
24  return match.group(1)
25  return ''
26 
27 
28 
33 def getArtifactDisplayName(artifactId):
34  artifactHash=getArtifactHash(artifactId)
35  if artifactHash and artifactId.endswith(artifactHash):
36  displayName=artifactId[:-len(artifactHash)].rstrip('_')
37  return displayName or artifactHash
38  return artifactId
39 
40 
41 
47 def shortenArtifactFileName(fileName,extension):
48  fileName=re.sub(r'[^0-9A-Za-z_.-]+','_',fileName).strip('._')
49  length=255-len(extension) if extension else 255
50  if len(fileName)<=length:
51  return fileName+extension
52  artifactHash=getArtifactHash(fileName)
53  if not artifactHash:
54  return fileName[:length]+extension
55  suffix='__'+artifactHash
56  prefixLength=max(0,length-len(suffix))
57  prefix=fileName[:prefixLength].rstrip('_')
58  return prefix+suffix+extension
59 
60 
66 def getFileName(fileName,extension):
67  if getArtifactHash(fileName):
68  return shortenArtifactFileName(fileName,extension)
69  #step1:先把fileName中的非法字符去除
70  fileName=fileName.replace(' ','')
71  fileName=fileName.replace('/','')
72  fileName=fileName.replace('\\','')
73  if len(extension) != 0:
74  length=255-len(extension)
75  fileName=fileName.split('(')[0] + '_' + hashlib.md5(fileName.encode()).hexdigest()[:16]
76  fileName=fileName[0:length]
77 
78  fileName+=extension
79  return fileName
80 
81 
90 def removeParameter(s,flag=0):
91  if '->' in s: #若有返回值,则把返回值也去掉
92  s=s.split('->')[0]
93  if flag==0: #去掉API中所有参数
94  stack=[]
95  left=0
96  right=0
97  ans=''
98  for index,value in enumerate(s):
99  #进栈
100  stack.append(value)
101  if value=='(':
102  left+=1
103  if value==')':
104  right+=1
105  #出栈
106  if left==right and left>0 and right>0:
107  pos=stack.index('(')
108  ans+=''.join(stack[0:pos])
109  stack.clear()
110  left=0
111  right=0
112  elif index==len(s)-1:
113  ans+=''.join(stack)
114  else: #只去除最后一个API的参数
115  i=len(s)-1
116  left=0 #记录左括号的个数
117  right=0
118  pos=len(s)
119  while i>=0:
120  if s[i]==')':
121  right+=1
122  if s[i]=='(':
123  left+=1
124  if left==right and left>0 and right>0:
125  pos=i #更新pos
126  break
127  i-=1
128  ans=s[0:pos]
129 
130  return ans
131 
132 
133 #拆分API,比如a.b(x).c(y).d(z)
134 
143 def departAPI(s):
144  ansLst=[]
145  stack=''
146  leftMin=0 #记录左'('的个数
147  rightMin=0
148  leftMid=0
149  rightMid=0
150  for i in range(len(s)):
151  stack+=s[i]
152  if s[i]=='(':
153  leftMin+=1
154  if s[i]==')':
155  rightMin+=1
156  if s[i]=='[':
157  leftMid+=1
158  if s[i]==']':
159  rightMid+=1
160 
161  flagMid=1
162  if '[' in stack:
163  if leftMid!=rightMid:
164  flagMid=0
165 
166  #拆分函数字符串里面必须要出现()
167  if leftMin and rightMin and leftMin==rightMin and flagMid:
168  ansLst.append(stack[0:i+1])
169  leftMin=0
170  rightMin=0
171  if leftMid and rightMid:
172  leftMin=0
173  rightMid=0
174 
175  elif i==len(s)-1:
176  ansLst.append(stack[0:i+1])
177 
178  return ansLst
179 
180 
181 
190 def departAPI2(s,separator='.'):
191  ansLst=[]
192  lst=[]
193  count_left_min=0 #统计左'('的个数
194  count_right_min=0 #统计右')'的个数
195 
196  count_left_middle=0 #统计左'['的个数
197  count_right_middle=0 #统计右']'的个数
198  for index,value in enumerate(s):
199  #入栈,分两种情况
200  if value!=separator:
201  lst.append(value)
202  if value=='(':
203  count_left_min+=1
204  if value==')':
205  count_right_min+=1
206  if value=='[':
207  count_left_middle+=1
208  if value==']':
209  count_right_middle+=1
210 
211  elif value==separator and ((count_left_min>count_right_min) or (count_left_middle>count_right_middle)):
212  lst.append(value)
213 
214  #弹栈,分三种情况
215  if value==separator:
216  flagMin=1 #假设左右括号的个数都是相等的
217  flagMid=1
218  if '(' in lst:
219  if count_left_min!=count_right_min:
220  flagMin=0
221  if '[' in lst:
222  if count_left_middle!=count_right_middle:
223  flagMid=0
224  if flagMin and flagMid:
225  ansLst.append(''.join(lst))
226  lst.clear()
227  elif index==len(s)-1:
228  ansLst.append(''.join(lst))
229 
230  return ansLst
231 
232 
233 
244 def getParameter(p_string,separator=',',space=1):
245  #库定义的参数去空格,项目中的参数不去空格,防止出问题
246  if space: #默认是去空格的
247  p_string=p_string.replace(' ','') #去掉参数中的空格
248 
249  if p_string=='':
250  return []
251 
252  parameters=[]
253  stack=[]
254  count_left_min=0 #统计'('的个数
255  count_right_min=0 #统计')'的个数
256 
257  count_left_middle=0 #统计'['的个数
258  count_right_middle=0 #统计']'的个数
259 
260  count_left_hua=0 #统计'{'的个数
261  count_right_hua=0 #统计'}'的个数
262 
263  count_single_yinhao=0 #统计单引号的引号的个数
264  count_double_yinhao=0 #统计双引号的引号的个数
265 
266  for index,value in enumerate(p_string):
267  stack.append(value)
268  if (value=="'" or count_single_yinhao) and not count_double_yinhao: #若上一步出现了双引号,则说明此处的单引号是在双引号内的,所以不计算单引号的个数
269  if value=="'":
270  count_single_yinhao+=1
271  if count_single_yinhao&1:
272  continue
273 
274  elif (value=='"' or count_double_yinhao) and not count_single_yinhao: #若上一步出现了单引号,则说明此处的双引号是在单引号内的,所以不计算双引号的个数
275  if value=='"':
276  count_double_yinhao+=1
277  if count_double_yinhao&1:
278  continue
279 
280  count_single_yinhao=0 #重置为0
281  count_double_yinhao=0
282 
283  #只计算引号之外的括号是否成对出现
284  if value=='(':
285  count_left_min+=1
286  elif value==')':
287  count_right_min+=1
288 
289  elif value=='[':
290  count_left_middle+=1
291  elif value==']':
292  count_right_middle+=1
293 
294  elif value=='{':
295  count_left_hua+=1
296  elif value=='}':
297  count_right_hua+=1
298 
299 
300  #弹栈,遇到分隔符或达到字符串末尾
301  if value==separator:
302  flagMin=1 #假设左右括号的个数都是相等的
303  flagMid=1
304  flagHua=1
305  if '(' in stack:
306  if count_left_min!=count_right_min:
307  flagMin=0
308  if '[' in stack:
309  if count_left_middle!=count_right_middle:
310  flagMid=0
311  if '{' in stack:
312  if count_left_hua!=count_right_hua:
313  flagHua=0
314 
315  if flagMin and flagMid and flagHua:
316  parameters.append(''.join(stack[0:-1]))
317  stack.clear()
318 
319  elif index==len(p_string)-1:
320  parameters.append(''.join(stack))
321 
322 
323  return parameters
324 
325 
326 
335  ans=''
336  i=len(apiStr)-1
337  left=0 #记录左括号的个数
338  right=0 #记录右括号的个数
339  pos=len(apiStr)
340  while i>=0:
341  if apiStr[i]==')':
342  right+=1
343  if apiStr[i]=='(':
344  left+=1
345  if left==right and left>0 and right>0:
346  pos=i
347  break
348  i-=1
349  if pos!=len(apiStr):
350  ans=apiStr[pos+1:-1]
351  return ans
def getFileName(fileName, extension)
Normalize and sanitize file name 规范化并清理文件名
Definition: codeUtils.py:66
def departAPI2(s, separator='.')
Split API call string based on separator "." 根据"."拆分API调用字符串
Definition: codeUtils.py:190
def shortenArtifactFileName(fileName, extension)
Shorten artifact file name while preserving full hash 缩短运行产物文件名并保留完整hash.
Definition: codeUtils.py:47
def removeParameter(s, flag=0)
Remove parameter(s) from API call string 去掉API中的参数部分
Definition: codeUtils.py:90
def getArtifactHash(artifactId)
Return artifact hash from an artifact id 从运行产物id中提取hash.
Definition: codeUtils.py:21
def getParameter(p_string, separator=',', space=1)
Split parameter string into list of separated parameters 将参数字符串拆分成单个的参数
Definition: codeUtils.py:244
def getArtifactDisplayName(artifactId)
Return readable artifact display name 返回可读运行产物展示名
Definition: codeUtils.py:33
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