generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 7
/
migrate.py
385 lines (353 loc) · 13.9 KB
/
migrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/python
# Copyright 2022 CloudWeGo Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import sys
from map import package_map
class Transfer(object):
# The hertz package has been imported. The format is [{name: ${package name}, alias: ${alias name}}]
import_hertz_library = []
# The other package has been imported
import_other_library = ""
# hertz package no need to convert
import_unuse_hertz_library = {}
import_new_hertz_library = {}
# backup variables name for context.Context
backup_var = ["c", "ctx", "context_Context"]
route_handler_name = []
before_import = ""
body = ""
package_map = {}
other_package_alias = []
def __init__(self, rootname):
self.rootname = rootname
def format_code(self):
# print("cd " + self.rootname + " && go fmt ./...")
os.system("cd " + self.rootname + " && go fmt ./...")
os.system("cd " + self.rootname + " && go get -t github.com/cloudwego/hertz")
os.system("cd " + self.rootname + " && go mod tidy && go mod verify")
def items_dir(self):
"""find all go files in rootname"""
for main_dir, _, file_name_list in os.walk(self.rootname):
for file in file_name_list:
if file.endswith(".go"):
yield os.path.join(main_dir, file)
def handle_alias(self, line, alias) -> None:
"""get a alias from a import line"""
pattern = re.compile('"(.*)"')
ele = line.split(" ")
if len(ele) == 1:
str_re1 = pattern.findall(line)
if len(str_re1) == 0:
return
package_name_list = str_re1[0].split("/")
alias.append(
{
"name": str_re1[0],
"alias": package_name_list[len(package_name_list) - 1],
}
)
self.import_unuse_hertz_library[
package_name_list[len(package_name_list) - 1]
] = str_re1[0]
else:
str_re1 = pattern.findall(line)
alias.append({"name": str_re1[0], "alias": ele[0]})
self.import_unuse_hertz_library[ele[0]] = str_re1[0]
def check_import(self, line: str) -> bool:
"""Check if this import line is the library should to be converted"""
for key in package_map.keys():
if line.find(key) != -1:
return True
return False
def handle_import(self, fd):
"""convert import"""
line = fd.readline()
has_other_framework = False
pattern = re.compile('"(.*)"')
while line:
if line.startswith("//"):
self.before_import += line
line = fd.readline()
elif re.search(r"import \(", line):
line = fd.readline()
while not re.search(r"\)", line):
line = line.strip()
if self.check_import(line):
has_other_framework = True
self.handle_alias(line, self.import_hertz_library)
else:
self.import_other_library += "\n" + line
ele = line.split(" ")
if len(ele) > 1:
self.other_package_alias.append({"alias": ele[0]})
else:
pattern = re.compile('"(.*)"')
str_re1 = pattern.findall(line)
if len(str_re1) > 0:
packageNameList = str_re1[0].split("/")
self.other_package_alias.append(
{"alias": packageNameList[len(packageNameList) - 1]}
)
line = fd.readline()
break
elif re.search("import ", line):
if self.check_import(line):
has_other_framework = True
ele = line.split(" ")
if len(ele) == 2:
str_re1 = pattern.findall(line)
self.import_hertz_library.append(
{"name": str_re1[0], "alias": "fasthttp"}
)
else:
str_re1 = pattern.findall(line)
self.import_hertz_library.append(
{"name": str_re1[0], "alias": ele[1]}
)
break
else:
self.before_import += line
line = fd.readline()
return has_other_framework
def handle_body(self, fd):
"""convert body"""
for item in self.import_hertz_library:
if item.get("name"):
self.package_map[item["alias"]] = package_map.get(item["name"])
line = fd.readline()
while line:
for key in self.package_map:
allStructs = self.package_map.get(key)
if not allStructs:
continue
if key in self.import_unuse_hertz_library:
self.import_unuse_hertz_library.pop(key)
for item in allStructs:
# match xxx.yyyy || *xxx.yyyy || \txxx.yyyy || (xxx.yyyy || []xxx.yyyy
if re.search(
r"[^a-zA-Z]{}\.{}[^a-zA-Z]".format(key, item["name"]), line
):
line = re.sub(
r"{}\.{}".format(key, item["name"]),
"{}.{}".format(
item["alias"],
item["afterName"]
if "afterName" in item
else item["name"],
),
line,
)
self.import_new_hertz_library[item.get("pkgName")] = item.get(
"alias"
)
self.body += line
line = fd.readline()
def handle_import_conflict(self):
for key in package_map:
for item in package_map[key]:
tmp = item["pkgName"].split("/")
item["alias"] = tmp[len(tmp) - 1]
for key in package_map:
for p in package_map[key]:
for o in self.other_package_alias:
if p.get("alias") == o.get("alias"):
p["alias"] = "h" + p["alias"]
def print_file(self, path):
hertzLibrary = ""
for key in self.import_new_hertz_library:
tmp = key.split("/")
alias = ""
if tmp[len(tmp) - 1] != self.import_new_hertz_library.get(key):
alias = self.import_new_hertz_library.get(key)
hertzLibrary += '{} "{}"\n'.format(alias, key)
for key in self.import_unuse_hertz_library:
tmp = self.import_unuse_hertz_library[key].split("/")
alias = ""
if tmp[len(tmp) - 1] != key:
alias = key
hertzLibrary += '{} "{}"\n'.format(
alias, self.import_unuse_hertz_library[key]
)
line = "{}\nimport({}\n{}\n)\n{}".format(
self.before_import, self.import_other_library, hertzLibrary, self.body
)
wopen = open(path, "w")
wopen.write(line)
wopen.close()
def find_route_handler(self):
filePath = self.items_dir()
for path in filePath:
fd = open(path, "r")
line = fd.readline()
while line:
if line.strip().startswith("//"):
line = fd.readline()
continue
elif re.search(
r"(GET|POST|DELETE|OPTION|PUT|PATCH|HEAD|Any|USE)\(", line
):
line = line.strip()
# delete comment
line_with_comment = line.split("//")
line = line_with_comment[0]
items = line.split(",")
last_item = items[-1]
tmp = re.findall(r"[^\W0-9]\w*", last_item)
if len(tmp) != 0:
self.route_handler_name.append(tmp[-1])
line = fd.readline()
def find_func(self, fd, line, lines):
var_name = []
tmp = []
tmp.append(line)
line = line.strip().split("(")
app_ctx_name = re.findall(r"[^\W0-9]\w*", line[-1])[0]
var_name.append(app_ctx_name)
line = fd.readline()
brace_num = 0
while line:
temp = re.findall(r"\{", line)
brace_num = brace_num + len(temp)
temp = re.findall(r"\}", line)
brace_num = brace_num - len(temp)
tmp.append(line)
if re.search(r"[^a-zA-Z]var [^\W0-9]\w*", line):
var = re.findall(r"[^\W0-9]\w*", line.strip())
var_name.extend(var[1:-1])
elif re.search(":=", line):
line = line.strip().split(":=")
var = re.findall(r"[^\W0-9]\w*", line[0])
var_name.extend(var)
if brace_num == -1:
break
line = fd.readline()
ctx_name = ""
for name in self.backup_var:
if name not in var_name:
ctx_name = name
break
first_line = tmp[0]
first_line = first_line.split("(")
for idx, val in enumerate(first_line):
if idx == len(first_line) - 1:
lines = lines + "(" + ctx_name + " context.Context, " + val
elif idx != 0:
lines = lines + "(" + val
else:
lines = lines + val
for val in tmp[1:]:
if re.search(r"[^a-zA-Z]{}.Context\(\)".format(app_ctx_name), val):
val = re.sub(r"{}.Context\(\)".format(app_ctx_name), ctx_name, val)
lines = lines + val
return lines, line
def add_import_context(self, path):
lines = ""
fd = open(path, "r")
line = fd.readline()
while line:
if re.search(r"import \(", line):
lines += line
lines += '"context"\n'
while not re.search(r"\)", line):
line = fd.readline()
if re.search(r"\)", line):
break
if not re.search('"context"', line):
lines += line
elif re.search("import ", line):
lines += 'import "context"'
lines += line
else:
lines += line
line = fd.readline()
wopen = open(path, "w")
wopen.write(lines)
wopen.close()
def handle_context(self):
"""convert other handle function to hertz handle function"""
self.find_route_handler()
filePath = self.items_dir()
for path in filePath:
lines = ""
fd = open(path, "r")
line = fd.readline()
need_add_import = False
while line:
has_found = False
for item in self.route_handler_name:
if re.search(
r"func.*{}\(.* \*app.RequestContext\)".format(item), line
):
lines, line = self.find_func(fd, line, lines)
has_found = True
need_add_import = True
if not has_found:
lines = lines + line
line = fd.readline()
wopen = open(path, "w")
wopen.write(lines)
wopen.close()
if need_add_import:
self.add_import_context(path)
def pre_handle(self):
# check go.mod
if not os.path.exists(os.path.join(self.rootname, "go.mod")):
print(
"could not found go.mod in %s, please ensure the path is a go module project"
% self.rootname
)
exit(1)
# check the git repository is dirty
exec = os.popen("cd " + self.rootname + " && git status -s", "r")
if exec.read() != "":
print(
"The git repository is dirty, please commit or stall all changes and try again"
)
exit(1)
def transfer(self):
self.pre_handle()
self.format_code()
filePath = self.items_dir()
for path in filePath:
fd = open(path, "r")
has_other_framework = self.handle_import(fd)
if not has_other_framework:
fd.close()
self.reset()
continue
self.handle_import_conflict()
self.handle_body(fd)
self.print_file(path)
fd.close()
self.reset()
self.format_code()
self.handle_context()
self.format_code()
def reset(self):
self.before_import = ""
self.import_hertz_library = []
self.import_other_library = ""
self.import_unuse_hertz_library = {}
self.import_new_hertz_library = {}
self.body = ""
self.package_map = {}
self.other_package_alias = []
if __name__ == "__main__":
if len(sys.argv) < 2:
print("No project path passed in")
exit(1)
transfer = Transfer(sys.argv[1])
transfer.transfer()