2
2
import platform
3
3
import re
4
4
import shutil
5
+ import signal
5
6
import subprocess
6
7
import sys
7
8
import urllib .request
8
9
import zipfile
9
10
from pathlib import Path
10
11
12
+ import psutil
11
13
import wget
12
14
13
15
jdk_path = Path .home ().joinpath ('ThetaData' ).joinpath ('ThetaTerminal' ) \
14
16
.joinpath ('jdk-19.0.1' ).joinpath ('bin' )
15
17
16
18
to_extract = Path .home ().joinpath ('ThetaData' ).joinpath ('ThetaTerminal' )
17
19
20
+ _thetadata_jar = "ThetaTerminal.jar"
21
+
18
22
19
23
def bar_progress (current , total , width = 80 ):
20
24
progress_message = "Downloading open-jdk 19.0.1 --> %d%% Complete" % (current / total * 100 )
@@ -52,12 +56,12 @@ def _verify_java():
52
56
# version = float(re.search(pattern, version.decode('utf8')).groups()[0])
53
57
54
58
# if version < 11:
55
- # print('Java 11 or higher is required to use this API. You are using using Java '
59
+ # print('Java 11 or higher is required to use this API. You are using Java '
56
60
# + str(version) + '. Please upgrade to a newer version.')
57
61
# exit(1)
58
62
59
63
60
- def launch_terminal (username : str = None , passwd : str = None , use_bundle : bool = True , jvm_mem : int = 0 ):
64
+ def launch_terminal (username : str = None , passwd : str = None , use_bundle : bool = True , jvm_mem : int = 0 , move_jar : bool = True ):
61
65
cwd = None
62
66
use_it = False
63
67
@@ -66,7 +70,8 @@ def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool =
66
70
67
71
if use_it :
68
72
cwd = jdk_path
69
- shutil .move ("ThetaTerminal.jar" , str (cwd .joinpath ('ThetaTerminal.jar' )))
73
+ if move_jar :
74
+ shutil .move ("ThetaTerminal.jar" , str (cwd .joinpath ('ThetaTerminal.jar' )))
70
75
else :
71
76
_verify_java ()
72
77
@@ -88,9 +93,49 @@ def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool =
88
93
print (line .decode ('utf-8' ).rstrip ("\n " ))
89
94
90
95
91
- def check_download (auto_update : bool ):
92
- if os .path .exists ('ThetaTerminal.jar' ) or auto_update :
93
- jar = urllib .request .urlopen ("https://download-latest.thetadata.us" )
94
- with open ('ThetaTerminal.jar' , 'wb' ) as output :
95
- output .write (jar .read ())
96
- output .close ()
96
+ def check_download (auto_update : bool ) -> bool :
97
+ try :
98
+ if os .path .exists ('ThetaTerminal.jar' ) or auto_update :
99
+ jar = urllib .request .urlopen ("https://download-latest.thetadata.us" )
100
+ with open ('ThetaTerminal.jar' , 'wb' ) as output :
101
+ output .write (jar .read ())
102
+ output .close ()
103
+ return True
104
+ except :
105
+ print ('Unable to fetch the latest terminal version.' )
106
+ return False
107
+
108
+
109
+ def kill_existing_terminal () -> None :
110
+ """
111
+ Utility function to kill any ThetaData terminal processes by scanning all running proceeses
112
+ and killing such process
113
+ """
114
+ for pid in psutil .pids ():
115
+ try :
116
+ cmdline_args = psutil .Process (pid = pid ).cmdline ()
117
+ for arg in cmdline_args :
118
+ if _thetadata_jar in arg :
119
+ os .kill (pid , signal .SIGTERM )
120
+ except :
121
+ pass
122
+
123
+
124
+ def is_terminal_instance_running () -> bool :
125
+ """
126
+ Checks if thetadata terminal is running or not
127
+ Returns:
128
+ bool: True if running else False
129
+ """
130
+ running = False
131
+ for pid in psutil .pids ():
132
+ try :
133
+ cmdline_args = psutil .Process (pid = pid ).cmdline ()
134
+ for arg in cmdline_args :
135
+ if _thetadata_jar in arg :
136
+ running = True
137
+ break
138
+ except :
139
+ pass
140
+ return running
141
+
0 commit comments