-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfl_backend.py
More file actions
394 lines (310 loc) · 11.8 KB
/
tfl_backend.py
File metadata and controls
394 lines (310 loc) · 11.8 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
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
386
387
388
389
390
391
392
393
394
import subprocess
import os
from directory_manager import python_paths, base_Directory, currentVE, totalVEs
from shutil import rmtree
import sqlite3
from pakages import messagebox
Current_default_env='None'; Current_default_env_dir = 'None'
thread_lock = 'open'
activeVE = ''
base_Directory = base_Directory
active_path = ''
def check_python_path():
""" Returns 404 if python not found in any path """
temp = []
for paths in python_paths:
if os.path.exists(paths):
valid_path = f'{paths}\python.exe'
if os.path.exists(valid_path):
temp.append(1)
return valid_path
else:
temp.append(0)
else:
temp.append(0)
if not all(temp):
return 404
thread_list=[]
def execute_VE(name, path, py_abs_path):
try:
py_Ver_found = check_python_path()
if py_Ver_found != 404:
if py_abs_path == py_Ver_found:
subprocess.run(f'{py_abs_path} -m venv {path}\\{name} && exit()', shell=True, stdout=subprocess.PIPE)
thread_list.append('success')
return 'success'
else:
if os.path.exists(py_abs_path):
subprocess.run(f'{py_abs_path} -m venv {path}\\{name} && exit()', shell=True,
stdout=subprocess.PIPE)
thread_list.append('success')
return 'success'
else:
thread_list.append('customPythonPathWrong')
return 'customPythonPathWrong'
else:
thread_list.append('pythonNotFound')
return 'pythonNotFound'
except Exception as error:
thread_list.append(error)
return error
class Data_IO:
def __init__(self):
pass
def writeDefaultVE(self, name):
try:
with open(currentVE, 'w') as file:
file.write(f'{name}')
except Exception as error:
return -101, error
def fetchDefaultVE(self):
try:
with open(currentVE, 'r') as file:
raw_data = file.read()
return raw_data
except Exception as error:
return -101, error
class Core_ops:
def __init__(self):
self.create_base_dir()
pass
def create_base_dir(self):
if os.path.exists(base_Directory):
pass
else:
os.mkdir(base_Directory)
def open_jupyter(self, fun, type):
global activeVE
try:
if activeVE != 'NotSpecified':
if activeVE != '':
if Current_default_env_dir == base_Directory:
p2 = subprocess.Popen(['start', 'cmd', '/k',
f'{f"{Current_default_env_dir}/{Current_default_env}/Scripts/activate.bat && cd {active_path}/{activeVE} && {type}"}'],
shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
fun()
p2.wait(1)
else:
p2 = subprocess.Popen(['start', 'cmd', '/k',
f'{f"{active_path}/Scripts/activate.bat && cd {active_path} && {type}"}'],
shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
fun()
p2.wait(1)
else:
return 0
else:
return 0
except Exception as error:
print(error)
def create_new_virtual_environment(self, name, path, py_abs_path):
""" Returns -201 if the VE already exist
Return -101 if failed to create VE
"""
try:
if os.path.exists(f'{path}/{name}'):
return -201
else:
if path == base_Directory and os.path.exists(path):
temp_ops = name.split(' ')
if len(temp_ops) > 1:
name = '_'.join(temp_ops)
response = execute_VE(name, path, py_abs_path)
# t1=Thread(target=execute_VE,args=[name, path, py_abs_path])
# t1.daemon=1
# t1.start()
# while 1:
# try:
# if thread_list[0]=='success':
# response='success'
# break
# else:
# response=thread_list[0]
# break
# except:
# pass
if response == 'success':
universal_Path_database('add', name, path)
return 'success'
else:
return response
else:
if os.path.exists(path):
temp_ops = name.split(' ')
if len(temp_ops) > 1:
name = '_'.join(temp_ops)
response = execute_VE(name, path, py_abs_path)
# t2 = Thread(target=execute_VE, args=[name, path, py_abs_path])
# t2.daemon = 1
# t2.start()
# while 1:
# try:
# if thread_list[0] == 'success':
# response = 'success'
# break
# else:
# response = thread_list[0]
# break
# except:
# pass
if response == 'success':
universal_Path_database('add', name, path + name)
return 'success'
else:
return response
else:
return 'pathDontExist'
except Exception as error:
return -101, error
def get_VE(self):
""" Returns -1 if default VE is not available """
VEs = os.walk(base_Directory).__next__()[1]
VEs_2 = universal_Path_database('showall', '', '')
final = [i for i in VEs_2 if i not in VEs]
return 1, VEs + final
def get_Packages(self, VEname, path, caller):
if caller == 'internal':
cmd = f'{base_Directory}/{VEname}/Lib/site-packages'
else:
cmd = f'{path}/Lib/site-packages'
if os.path.exists(cmd):
dirs = os.walk(cmd).__next__()
if '__pycache__' in dirs[1]:
dirs[1].remove('__pycache__')
return dirs[1], dirs[2]
else:
return -1
def deleteVENV(self, name):
try:
rmtree(f'{base_Directory}/{name}')
return 1
except Exception as error:
return -1
def get_pythons():
try:
pythons = subprocess.run('py -0p', shell=True, stdout=subprocess.PIPE)
hard_paths = pythons.stdout.decode('utf-8').strip().split('\n')
python_dic={}
for paths in hard_paths:
python_dic[paths.strip().split(' ')[0]]=paths.strip().split(' ')[-1]
if '(venv)' in python_dic.keys():
python_dic.pop('(venv)')
return 1, python_dic,
except Exception as error:
return 0, error
ver = get_pythons()
def validate_script(path, script_name):
if os.path.exists(f'{path}/{script_name}'):
version = path.split('/')[-1]
return 1, version
else:
return 0,
def validate_VE_dir(path):
if os.path.exists(f'{path}'):
return 1
return 0
def chk_valid_path_for_ve(path,name=''):
if path==base_Directory:
if os.path.exists(f'{path}/{name}/Scripts/python.exe'):
return 1
else:
return 0
else :
if os.path.exists(f'{path}/Scripts/python.exe'):
return 1
else:
return 0
def debug_func():
import time
time.sleep(2)
return 'success'
class PrimaryExecution:
def __init__(self):
pass
def get_previous_VE(self):
with open(currentVE) as file:
current = file.read()
if os.path.exists(f'{base_Directory}/{current}'):
if current !='' or current !=None:
return 1,current,base_Directory
else:
return 101, #There was no active Env previously
else:
try:
connection = sqlite3.connect(totalVEs)
except:
return -1, # Connection Error
cur = connection.cursor()
raw_data = cur.execute(f'select * from tab where name="{current}"')
temp_raw = raw_data.fetchall()
if len(temp_raw):
_, currentVE_path = temp_raw[0]
Dir_validation = validate_VE_dir(currentVE_path)
if Dir_validation:
return 1,current,currentVE_path
else:
return 102, # The directory of external VE is mmissing
else:
return 0, # The VE is invalid
def set_current():
global Current_default_env_dir, Current_default_env,activeVE,active_path
Chk_error = PrimaryExecution().get_previous_VE()
if Chk_error[0] == 1:
Current_default_env = Chk_error[1]
Current_default_env_dir = Chk_error[2]
activeVE=Current_default_env
active_path=Current_default_env_dir
return 1, Current_default_env, Current_default_env_dir
elif Chk_error[0] == 101 or Chk_error[0] == -1 or not Chk_error[0]:
messagebox.showerror('Missing Directories or Files', 'Something is not as what we expected!')
return 0,
elif Chk_error[0] == 102:
messagebox.showerror('Missing Directories or Files', 'The previous VE Directory is no longer available.')
return 0,
else:
return 0,
class Install_Pakages:
def __init__(self):
pass
def install_pkgs_via_pip(self, name, dir, pkgname):
activate_cmd = f'{dir}/Scripts'
install_cmd = f'pip install {pkgname}'
print(activate_cmd)
p2 = subprocess.Popen(
['start', 'cmd', '/k', f'cd {activate_cmd} && activate.bat && {install_cmd}'],
shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p2.wait(1)
def universal_Path_database(calltype, name, path,debug=None):
temp_all_VE = []
connection = sqlite3.connect(totalVEs)
cur = connection.cursor()
try:
if calltype == "add":
cur.execute('insert into tab values (?,?)', (name, path))
connection.commit()
return 1
elif calltype == 'read':
raw_file = cur.execute(f'select path from tab where name ="{name}"')
data = raw_file.fetchall()[0][0]
return data
elif calltype == 'delete':
cur.execute(f'delete from tab where name="{name}"')
connection.commit()
return 1
elif calltype == 'showall':
raw_file = cur.execute(f'select name from tab')
data = raw_file.fetchall()
for val in data:
temp_all_VE.append(val[0])
return temp_all_VE
else:
pass
except Exception as error:
return 0
finally:
connection.close()
def title_updater():
return activeVE
# print(universal_Path_database('showall','',''))
set_current()