-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_utilities.py
260 lines (228 loc) · 9 KB
/
common_utilities.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
import os
import sys
import rclone_shorthands_constants as cst
import psutil
import re
import subprocess
import time
import threading
import keyboard
from rclone_shorthands_constants import CMDFlags
lock = threading.Lock()
class TimeoutException(Exception):
pass
def auto_type():
with lock:
time.sleep(0.7)
keyboard.press(" ")
keyboard.release(" ")
def timeout_handler():
print("\nIt looks like the current terminal isn't compatible with some functions of the program, Try with some other terminal emulator or run the program with arguments like ./program.exe -c to run in the same terminal\nExiting Now...", flush=True)
os._exit(1)
class CommonUtils:
def __init__(self):
self._choose_operations()
def _choose_operations(self):
platform = sys.platform
if platform.startswith('win'):
self._which_os = cst.WINDOWS
self.generate_python_string = lambda: "python"
self.continue_operations = lambda compatibility: self._windows_operations(
compatibility)
elif platform.startswith('linux'):
self._which_os = cst.LINUX
self.generate_python_string = lambda: "python" + self._python_version
self.continue_operations = lambda compatibility: self._posix_operations(
compatibility)
elif platform.startswith('darwin'):
self._which_os = cst.MACOS
self.generate_python_string = lambda: "python" + self._python_version
self.continue_operations = lambda compatibility: self._posix_operations(
compatibility)
else:
self._which_os = "Other"
self.continue_operations = lambda compatibility: self._other_operations(
compatibility)
def _windows_operations(self, compatibility):
if compatibility == CMDFlags.PAUSE.val:
self._pause_method = self._compat_pause
is_posix = self._check_unix_shell()
if not is_posix:
is_windows = self._check_win_shell()
if is_windows:
self._shell_type = "Windows Shell"
self._clear_screen = self._win_clrscr
else:
self._shell_type = "Unknown Shell"
self._clear_screen = self._compat_clrscr
else:
self._shell_type = "Posix Shell"
self._clear_screen = self._unix_clrscr
elif compatibility == CMDFlags.CLEAR_SCREEN.val:
self._clear_screen = self._compat_clrscr
is_posix = self._check_unix_shell()
if not is_posix:
is_windows = self._check_win_shell()
if is_windows:
self._shell_type = "Windows Shell"
self._pause_method = self._win_pause
else:
self._shell_type = "Unknown Shell"
self._pause_method = self._compat_pause
else:
self._is_winpty = self._check_winpty()
self._shell_type = "Posix Shell"
if self._is_winpty or self._check_win_unix_term():
self._pause_method = self._win_pause
else:
self._pause_method = self._compat_pause
elif compatibility == CMDFlags.BOTH.val:
self._pause_method = self._compat_pause
self._clear_screen = self._compat_clrscr
is_posix = self._check_unix_shell()
if not is_posix:
is_windows = self._check_win_shell()
if is_windows:
self._shell_type = "Windows Shell"
else:
self._shell_type = "Unknown Shell"
else:
self._is_winpty = self._check_winpty()
self._shell_type = "Posix Shell"
else:
is_posix = self._check_unix_shell()
if not is_posix:
is_windows = self._check_win_shell()
if is_windows:
self._shell_type = "Windows Shell"
self._clear_screen = self._win_clrscr
self._pause_method = self._win_pause
else:
self._other_operations()
else:
self._shell_type = "Posix Shell"
self._is_winpty = self._check_winpty()
if self._is_winpty or self._check_win_unix_term():
self._pause_method = self._win_pause
self._clear_screen = self._unix_clrscr
else:
self._clear_screen = self._unix_clrscr
self._pause_method = self._compat_pause
#self._compat_status = "p"
def _posix_operations(self, compatibility):
if compatibility == CMDFlags.PAUSE.val:
self._pause_method = self._compat_pause
if self._check_unix_shell():
self._shell_type = "Posix Shell"
self._clear_screen = self._unix_clrscr
else:
self._shell_type = "Unknown Shell"
self._clear_screen = self._compat_clrscr
elif compatibility == CMDFlags.CLEAR_SCREEN.val:
self._clear_screen = self._compat_clrscr
if self._check_unix_shell():
self._shell_type = "Posix Shell"
self._pause_method = self._posix_pause
else:
self._shell_type = "Unknown Shell"
self._pause_method = self._compat_pause
elif compatibility == CMDFlags.BOTH.val:
self._pause_method = self._compat_pause
self._clear_screen = self._compat_clrscr
if self._check_unix_shell():
self._shell_type = "Posix Shell"
else:
self._shell_type = "Unknown Shell"
else:
if self._check_unix_shell():
self._shell_type = "Posix Shell"
self._pause_method = self._posix_pause
self._clear_screen = self._unix_clrscr
else:
self._other_operations()
def _other_operations(self, compatibility):
#self._compat_status = "cp"
self._shell_type = "Unknown Shell"
self._pause_method = self._compat_pause
self._clear_screen = self._compat_clrscr
def _posix_pause(self):
import termios
import tty
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def _win_pause(self):
import msvcrt
return msvcrt.getch()
def _compat_pause(self):
import getpass
getpass.getpass(prompt="")
def _unix_clrscr(self):
return os.system("printf '\033c'")
def _win_clrscr(self):
return os.system("cls")
def _compat_clrscr(self):
import shutil
return print("\n"*shutil.get_terminal_size().lines*2+'\033[1;1H', end='')
def _check_unix_shell(self):
try:
exit_code = subprocess.run(
['id'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except FileNotFoundError:
return False
def _check_win_shell(self):
current_process = psutil.Process()
while current_process.parent() is not None:
current_process = current_process.parent()
parent_name = current_process.name()
if re.fullmatch('pwsh|pwsh.exe|powershell|powershell.exe|cmd|cmd.exe', parent_name):
return True
return False
def _check_winpty(self):
try:
subprocess.run(['winpty', '--version'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except:
return False
def _check_win_unix_term(self):
print("Analysing terminal support...", end='', flush=True)
auto_type_thread = threading.Thread(target=auto_type)
try:
timer = threading.Timer(1.5, timeout_handler)
timer.start()
auto_type_thread.start()
import msvcrt
msvcrt.getch()
except Exception as e:
print("Error Occured")
return False
finally:
auto_type_thread.join()
timer.cancel()
timer.join()
print("\nDone")
return True
def get_os(self):
return self._which_os
def pause(self):
return self._pause_method()
def clear_screen(self):
return self._clear_screen()
def shell_type(self):
return self._shell_type
def check_winpty(self):
return getattr(self, '_is_winpty', False) is True
def check_python_version(self):
self._python_version = sys.version_info.major
return self._python_version >= 3
def is_compat(self):
return self._compat_status
def get_py_exe(self):
return self._python_exe