-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpatch_run.py
More file actions
108 lines (92 loc) · 3.55 KB
/
Copy pathpatch_run.py
File metadata and controls
108 lines (92 loc) · 3.55 KB
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
from psutil import process_iter
from toTs import ToTs
from toTaskNew import ToTaskNew
import os
import subprocess
import json
def process_all_java_files(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(".java"):
java_path = os.path.join(dirpath, filename)
# 读取 .java 文件内容
with open(java_path, 'r', encoding='utf-8') as f:
java_content = f.read()
with open('Tmp.java', 'w') as new_file:
new_file.write(java_content)
# 处理内容
processed_content = run()
# 写入 index.ets(同目录下,追加模式)
ets_path = os.path.join(dirpath, "index.ets")
with open(ets_path, 'w', encoding='utf-8') as f:
f.write(processed_content)
def run_jar_with_input():
result = subprocess.run(['java', '-jar', 'java_target/maven-demo.jar'], capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError("Java environment not detected, terminating the program.")
return result.stdout
def deleteZhu(mycode):
if mycode[0] == '`':
lines = mycode.splitlines()
lines = lines[1:-1]
if lines[-1][0] == '`':
lines = lines[0:-1]
result_string = "\n".join(lines)
return result_string
else:
return mycode
def run():
run_jar_with_input()
file_path = 'result.json'
with open(file_path, 'r') as file:
result = json.load(file)
code = result['ast_output']
origin_code = code
flag = result['flag']
flag = flag % 4
if flag / 2 == 1:
code = ''
refer = ("import { SynStart, SynEnd, wait, notify, SharedBoolean, SharedString, SharedNumber, Syc, isMainThread, addFunc, Runnable, Thread } from "
"'./ThreadBridge';\n\n") + code
for c in result['classes']:
class_code = c['class_code']
class_flag = c['class_flag']
tmp = ToTaskNew(class_flag, refer, origin_code).run(class_code) + '\n\n\n'
tmp = deleteZhu(tmp)
code += tmp
# st.code(tmp)
else:
code = ToTs().getStart(code)
if flag / 2 == 1:
refer = ("import { SynStart, SynEnd, wait, notify, SharedBoolean, SharedString, SharedNumber, Syc, isMainThread, addFunc, Runnable, Thread } from "
"'./ThreadBridge';\n\n")
wash = deleteZhu(ToTaskNew().addWash(refer + code))
code = refer + wash + '\n\n' + code
code = ToTs().getClean(code)
code = deleteZhu(code)
main_code = """if (isMainThread()) {
// You can put the entry of your code here to test.
}"""
code = code + '\n\n' + main_code
last_code = code
error_flag = 0
for _ in range(3):
with open('Output.ts', 'w') as file:
file.write(code)
process = subprocess.Popen(['tsc', 'Output.ts'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if len(stdout.decode().splitlines()) <= 1:
error_flag = 0
break
# st.code(stdout.decode())
error_flag = 1
code = deleteZhu(ToTs().getFix(code, stdout.decode()))
# st.code(code)
if error_flag == 1:
code = last_code
print(code)
with open('Output.ts', 'w') as file:
file.write(code)
return code
if __name__ == "__main__":
process_all_java_files("C:\\Users\\34203\\Desktop\\Java2ArkTS\\tests")