-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgoozlib.py
More file actions
300 lines (286 loc) · 13.7 KB
/
Copy pathgoozlib.py
File metadata and controls
300 lines (286 loc) · 13.7 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
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
import subprocess
import tempfile
import os
import json
class GoozCLI:
# Initial Values
port = ""
username = ""
workdir = ""
template = {"port":"","username":"","workdir":""}
out = ""
outputupload = ""
ignored = ""
test_out = ""
def gooz_init(self):
try:
f = open('config.json')
data = json.load(f)
self.port = data["port"]
self.username = data["username"]
self.workdir = data["workdir"]
return "OK"
except:
print("\033[91mconfig.json file not found\033[0m")
print("\033[91mNeed setup process\033[0m")
print("\033[94mExample:\033[0m")
print("\033[94mpython goozcli.py setup\033[0m")
return "PASS"
def load(self):
cmd = "ampy -p "+self.port+" ls"
output = ""
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(cmd, stdout=tempf,stderr=tempf,shell=True)
proc.wait()
tempf.seek(0)
output = tempf.read().decode("utf-8")
self.out = output.splitlines()
def ls(self):
print(self.out)
def upload(self,param):
clipath = os.getcwd()
os.chdir(self.workdir)
print("Workdir -> {}".format(self.workdir))
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen("dir "+self.workdir+" /B", stdout=tempf,shell=True)
proc.wait()
tempf.seek(0)
output = tempf.read().decode("utf-8")
self.outputupload = output.splitlines()
print("These files will be upload")
f = open(clipath+"\ignored.txt","r",encoding="utf-8")
self.ignored = f.readlines()
for i in self.outputupload:
for a in self.ignored:
if i == a:
self.outputupload.remove(i)
print(self.outputupload)
try:
if param == "-y":
try:
for i in self.outputupload:
try:
os.system("ampy -p "+self.port+" put "+i)
print(f"\U00002714 \033[92m{i} has been uploaded successfully\033[0m")
except:
print(f"\033[91m{i} could not be uploaded !!\033[0m")
print("\U00002705 \033[92mSuccessfully Completed !!!\033[0m")
except:
print("\033[91mThis process haven't completed!!\033[0m")
else:
accept = input("\033[93mDo you accept this process? [Y/N] \033[0m")
if accept == "YES" or accept == "Y" or accept == "yes" or accept == "y" or accept == "Yes":
try:
for i in self.outputupload:
try:
os.system("ampy -p "+self.port+" put "+i)
print(f"\U00002714 \033[92m{i} has been uploaded successfully\033[0m")
except:
print(f"\033[91m{i} could not be uploaded !!\033[0m")
print("\U00002705 \033[92mSuccessfully Completed !!!\033[0m")
except:
print("\033[91mThis process haven't completed!!\033[0m")
else:
print("\033[91mProcess has been canceled\033[0m")
except:
print("Error")
def setup(self):
if os.path.exists("config.json"):
print("\033[94mConfig file exists. No need setup\033[0m")
else:
print("\033[91mconfig.json file not found\033[0m")
print("\033[94mSetup process has been started\033[0m")
try:
self.configuration()
print("\033[92mSetup has been done successfully\033[0m")
except:
print("\033[91mSetup has been failed\033[0m")
def delete(self,param):
if param == "-y":
for i in self.out:
try:
if i[-3:] == ".py":
os.system("ampy -p "+self.port+" rm "+i)
else:
os.system("ampy -p "+self.port+" rmdir "+i)
print(f"\U00002714 \033[92m{i} has been deleted successfully\033[0m")
except:
print(f"\033[91m{i} could not be deleted !!\033[0m")
print("\U00002705 \033[92mSuccessfully Completed !!!\033[0m")
else:
accept = input("\033[93mDo you accept this process? [Y/N] \033[0m")
if accept == "YES" or accept == "Y" or accept == "yes" or accept == "y" or accept == "Yes":
for i in self.out:
try:
if i[-3:] == ".py":
os.system("ampy -p "+self.port+" rm "+i)
elif "." in i:
os.system("ampy -p "+self.port+" rm "+i)
else:
os.system("ampy -p "+self.port+" rmdir "+i)
print(f"\U00002714 \033[92m{i} has been deleted successfully\033[0m")
except:
print(f"\033[91m{i} could not be deleted !!\033[0m")
print("\U00002705 \033[92mSuccessfully Completed !!!\033[0m")
else:
print("\033[91mProcess has been canceled\033[0m")
def show_parameters(self):
print("Port -> {}".format(self.port))
print("Workdir -> {}".format(self.workdir))
print("Username -> {}".format(self.username))
def get_files(self,param):
if param == "--all" or param == "-a":
for i in self.out:
print(i)
else:
cmd = "ampy -p "+self.port+" get "+param
output = ""
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(cmd, stdout=tempf,shell=True)
proc.wait()
tempf.seek(0)
output = tempf.read().decode("utf-8")
a = output.split("\r\n")
f = open("./getfolder"+param,"w+",encoding="utf-8")
for i in a:
f.write(i)
f.close()
print("\033[92mDEBUG:File has been taken successfully\033[0m")
def test_gooz(self):
os.system("ampy -p "+self.port+" run test.py")
def configuration(self):
_port = input("Please enter board port -> ")
_username = input("Please enter username -> ")
_workdir = input("Please enter workdir path(OS folders path) -> ")
#print(_port,_username,_workdir)
_config = {"port":"","username":"","workdir":""}
_config["port"] = _port
_config["username"] = _username
_config["workdir"] = _workdir
print("\033[94mConfiguration JSON -> {} \033[0m".format(_config))
try:
f = open("config.json","w",encoding="utf-8")
json.dump(_config,f,indent=4)
f.close()
_config = {"port":"","username":"","workdir":""}
print("\U00002705 \033[92mconfig.json has been changed!\033[0m")
except:
print("\U0000274C \033[91mProcess can not be done!!\033[0m")
def webgooz_creator(self,filename):
backend_template = {"apis":[]}
backend_nvic = {"filename":"","codes":""}
f = open(filename,"r",encoding="utf-8")
lines = f.readlines()
for i in lines:
if "def" in i:
path = i[4:len(i)-4]
paths = path.split("_")
newPath = ""
for a in paths:
newPath += "/"
newPath += a
newPath += ".py"
backend_nvic["filename"] = newPath
newCodes = "def run():\n"
for codeLine in range(len(lines)):
if i == lines[codeLine]:
lineCounter = 1
while True:
if "def" in lines[codeLine+lineCounter] or "last" in lines[codeLine+lineCounter]:
backend_nvic["codes"] = newCodes.replace("\n","backendlineflag")
break
elif not "def" in lines[codeLine+lineCounter]:
newCodes += lines[codeLine+lineCounter]
lineCounter += 1
backend_template["apis"].append(backend_nvic)
backend_nvic = {"filename":"","codes":""}
print(backend_template)
with open(filename[:-3]+".json", "w") as fp:
json.dump(backend_template, fp, indent=4)
f.close()
def convert_package(self):
pkg_template = {"name":"","codes":""}
code_template = {"filename":"","code":""}
cli_path = os.getcwd()
os.chdir(self.workdir)
package_name = input("Please enter package name -> ")
print("Is your package in app or another folder?")
print("If your package is under app folder please enter only \033[95m'd'\033[0m")
print("If your package is under another folder please enter folder path")
print("\033[96mEXAMPLE: /mynewapp/newapp\033[0m")
print("\033[93mWARNING: Another folder must be under workdir\033[0m")
package_path = input("Please enter folder -> ")
if package_path == "d":
try:
os.chdir(self.workdir+"/app/"+package_name)
print(os.listdir())
files = os.listdir()
accept=input("\033[93mWARNING: These files convert, do you agree? [y,N]\033[0m -> ")
if accept == "YES" or accept == "Y" or accept == "yes" or accept == "y" or accept == "Yes":
print("\033[92mProcess has been accepted\033[0m")
print("\033[94mProcess is doing\033[0m")
codes_repo = []
for i in files:
if "." in i:
code_template["filename"] = i
f = open(i,"r",encoding="utf-8")
codes_file=f.readlines()
changed_code_array=[]
for i in codes_file:
changed_code_array.append(i.replace("\n","pkglineflag"))
for i in changed_code_array:
print(i)
tmp_code = "".join(changed_code_array)
tmp_code = tmp_code.replace("\n","pkglineflag")
code_template["code"] = tmp_code
codes_repo.append(code_template)
code_template = {"filename":"","code":""}
pkg_template["codes"]=codes_repo
pkg_template["name"]=package_name
os.chdir(cli_path)
with open(package_name+".json", "w") as fp:
json.dump(pkg_template, fp, indent=4)
print("Process has been finished")
else:
print("\033[91mProcess has been canceled\033[0m")
except:
print("\033[91mERROR: This folder can not be reachable\033[0m")
print("\033[93mWARNING: May be this folder has been deleted\033[0m")
else:
try:
os.chdir(self.workdir+package_path+"/"+package_name)
print(os.listdir())
files = os.listdir()
accept=input("\033[93mWARNING: These files convert, do you agree? [y,N]\033[0m -> ")
if accept == "YES" or accept == "Y" or accept == "yes" or accept == "y" or accept == "Yes":
print("\033[92mProcess has been accepted\033[0m")
print("\033[94mProcess is doing\033[0m")
codes_repo = []
for i in files:
if i != "manage.py":
code_template["filename"] = i
f = open(i,"r",encoding="utf-8")
codes_file=f.readlines()
tmp_code = "".join(codes_file)
tmp_code = tmp_code.replace("\n","\\n")
code_template["code"] = tmp_code
codes_repo.append(code_template)
code_template = {"filename":"","code":""}
pkg_template["codes"]=codes_repo
pkg_template["name"]=package_name
f = open("manage.py","r",encoding="utf-8")
manager_snip = f.readlines()
manager_snip.insert(0,"\\n")
manager_snip[-1] = manager_snip[-1] + "\\n"
tmp_manager = "".join(manager_snip)
tmp_manager = tmp_manager.replace("\n","\\n")
pkg_template["managersnip"] = tmp_manager
os.chdir(cli_path)
with open(package_name+".json", "w") as fp:
json.dump(pkg_template, fp, indent=4)
print("Process has been finished")
else:
print("\033[91mProcess has been canceled\033[0m")
except:
print("\033[91mERROR: This folder can not be reachable\033[0m")
print("\033[93mWARNING: May be this folder has been deleted\033[0m")