@@ -26,11 +26,11 @@ def __init__(self, ssh, pid):
26
26
self .pid = pid
27
27
28
28
def kill (self ):
29
- command = f "kill { self .pid } "
29
+ command = "kill {}" . format ( self .pid )
30
30
self .ssh .exec_command (command )
31
31
32
32
def cmdline (self ):
33
- command = f "ps -p { self . pid } -o cmd --no-headers"
33
+ command = "ps -p {} -o cmd --no-headers" . format ( self . pid )
34
34
stdin , stdout , stderr = self .ssh .exec_command (command )
35
35
cmdline = stdout .read ().decode ('utf-8' ).strip ()
36
36
return cmdline .split ()
@@ -84,9 +84,9 @@ def _read_ssh_key(self):
84
84
key = paramiko .RSAKey .from_private_key_file (self .ssh_key )
85
85
return key
86
86
except FileNotFoundError :
87
- raise ExecUtilException (message = f "No such file or directory: '{ self . ssh_key } '" )
87
+ raise ExecUtilException (message = "No such file or directory: '{}'" . format ( self . ssh_key ) )
88
88
except Exception as e :
89
- ExecUtilException (message = f "An error occurred while reading the ssh key: { e } " )
89
+ ExecUtilException (message = "An error occurred while reading the ssh key: {}" . format ( e ) )
90
90
91
91
def exec_command (self , cmd : str , wait_exit = False , verbose = False , expect_error = False ,
92
92
encoding = None , shell = True , text = False , input = None , stdout = None ,
@@ -131,7 +131,7 @@ def exec_command(self, cmd: str, wait_exit=False, verbose=False, expect_error=Fa
131
131
if error_found :
132
132
if exit_status == 0 :
133
133
exit_status = 1
134
- raise ExecUtilException (message = f "Utility exited with non-zero code. Error: { error .decode (encoding or 'utf-8' )} " ,
134
+ raise ExecUtilException (message = "Utility exited with non-zero code. Error: {}" . format ( error .decode (encoding or 'utf-8' )) ,
135
135
command = cmd ,
136
136
exit_code = exit_status ,
137
137
out = result )
@@ -148,7 +148,7 @@ def environ(self, var_name: str) -> str:
148
148
Args:
149
149
- var_name (str): The name of the environment variable.
150
150
"""
151
- cmd = f "echo ${ var_name } "
151
+ cmd = "echo ${}" . format ( var_name )
152
152
return self .exec_command (cmd , encoding = 'utf-8' ).strip ()
153
153
154
154
def find_executable (self , executable ):
@@ -166,7 +166,7 @@ def find_executable(self, executable):
166
166
167
167
def is_executable (self , file ):
168
168
# Check if the file is executable
169
- is_exec = self .exec_command (f "test -x { file } && echo OK" )
169
+ is_exec = self .exec_command ("test -x {} && echo OK" . format ( file ) )
170
170
return is_exec == b"OK\n "
171
171
172
172
def set_env (self , var_name : str , var_val : str ):
@@ -176,7 +176,7 @@ def set_env(self, var_name: str, var_val: str):
176
176
- var_name (str): The name of the environment variable.
177
177
- var_val (str): The value to be set for the environment variable.
178
178
"""
179
- return self .exec_command (f "export { var_name } ={ var_val } " )
179
+ return self .exec_command ("export {}={}" . format ( var_name , var_val ) )
180
180
181
181
# Get environment variables
182
182
def get_user (self ):
@@ -195,12 +195,12 @@ def makedirs(self, path, remove_existing=False):
195
195
- remove_existing (bool): If True, the existing directory at the path will be removed.
196
196
"""
197
197
if remove_existing :
198
- cmd = f "rm -rf { path } && mkdir -p { path } "
198
+ cmd = "rm -rf {} && mkdir -p {}" . format ( path , path )
199
199
else :
200
- cmd = f "mkdir -p { path } "
200
+ cmd = "mkdir -p {}" . format ( path )
201
201
exit_status , result , error = self .exec_command (cmd , verbose = True )
202
202
if exit_status != 0 :
203
- raise Exception (f "Couldn't create dir { path } because of error { error } " )
203
+ raise Exception ("Couldn't create dir {} because of error {}" . format ( path , error ) )
204
204
return result
205
205
206
206
def rmdirs (self , path , verbose = False , ignore_errors = True ):
@@ -211,7 +211,7 @@ def rmdirs(self, path, verbose=False, ignore_errors=True):
211
211
- verbose (bool): If True, return exit status, result, and error.
212
212
- ignore_errors (bool): If True, do not raise error if directory does not exist.
213
213
"""
214
- cmd = f "rm -rf { path } "
214
+ cmd = "rm -rf {}" . format ( path )
215
215
exit_status , result , error = self .exec_command (cmd , verbose = True )
216
216
if verbose :
217
217
return exit_status , result , error
@@ -224,11 +224,11 @@ def listdir(self, path):
224
224
Args:
225
225
path (str): The path to the directory.
226
226
"""
227
- result = self .exec_command (f "ls { path } " )
227
+ result = self .exec_command ("ls {}" . format ( path ) )
228
228
return result .splitlines ()
229
229
230
230
def path_exists (self , path ):
231
- result = self .exec_command (f "test -e { path } ; echo $?" , encoding = 'utf-8' )
231
+ result = self .exec_command ("test -e {}; echo $?" . format ( path ) , encoding = 'utf-8' )
232
232
return int (result .strip ()) == 0
233
233
234
234
@property
@@ -239,7 +239,7 @@ def pathsep(self):
239
239
elif os_name == "nt" :
240
240
pathsep = ";"
241
241
else :
242
- raise Exception (f "Unsupported operating system: { os_name } " )
242
+ raise Exception ("Unsupported operating system: {}" . format ( os_name ) )
243
243
return pathsep
244
244
245
245
def mkdtemp (self , prefix = None ):
@@ -249,7 +249,7 @@ def mkdtemp(self, prefix=None):
249
249
- prefix (str): The prefix of the temporary directory name.
250
250
"""
251
251
if prefix :
252
- temp_dir = self .exec_command (f "mktemp -d { prefix } XXXXX" , encoding = 'utf-8' )
252
+ temp_dir = self .exec_command ("mktemp -d {}XXXXX" . format ( prefix ) , encoding = 'utf-8' )
253
253
else :
254
254
temp_dir = self .exec_command ("mktemp -d" , encoding = 'utf-8' )
255
255
@@ -262,7 +262,7 @@ def mkdtemp(self, prefix=None):
262
262
263
263
def mkstemp (self , prefix = None ):
264
264
if prefix :
265
- temp_dir = self .exec_command (f "mktemp { prefix } XXXXX" , encoding = 'utf-8' )
265
+ temp_dir = self .exec_command ("mktemp {}XXXXX" . format ( prefix ) , encoding = 'utf-8' )
266
266
else :
267
267
temp_dir = self .exec_command ("mktemp" , encoding = 'utf-8' )
268
268
@@ -277,8 +277,8 @@ def copytree(self, src, dst):
277
277
if not os .path .isabs (dst ):
278
278
dst = os .path .join ('~' , dst )
279
279
if self .isdir (dst ):
280
- raise FileExistsError (f "Directory { dst } already exists." )
281
- return self .exec_command (f "cp -r { src } { dst } " )
280
+ raise FileExistsError ("Directory {} already exists." . format ( dst ) )
281
+ return self .exec_command ("cp -r {} {}" . format ( src , dst ) )
282
282
283
283
# Work with files
284
284
def write (self , filename , data , truncate = False , binary = False , read_and_write = False , encoding = 'utf-8' ):
@@ -344,10 +344,10 @@ def touch(self, filename):
344
344
345
345
This method behaves as the 'touch' command in Unix. It's equivalent to calling 'touch filename' in the shell.
346
346
"""
347
- self .exec_command (f "touch { filename } " )
347
+ self .exec_command ("touch {}" . format ( filename ) )
348
348
349
349
def read (self , filename , binary = False , encoding = None ):
350
- cmd = f "cat { filename } "
350
+ cmd = "cat {}" . format ( filename )
351
351
result = self .exec_command (cmd , encoding = encoding )
352
352
353
353
if not binary and result :
@@ -357,9 +357,9 @@ def read(self, filename, binary=False, encoding=None):
357
357
358
358
def readlines (self , filename , num_lines = 0 , binary = False , encoding = None ):
359
359
if num_lines > 0 :
360
- cmd = f "tail -n { num_lines } { filename } "
360
+ cmd = "tail -n {} {}" . format ( num_lines , filename )
361
361
else :
362
- cmd = f "cat { filename } "
362
+ cmd = "cat {}" . format ( filename )
363
363
364
364
result = self .exec_command (cmd , encoding = encoding )
365
365
@@ -371,31 +371,31 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
371
371
return lines
372
372
373
373
def isfile (self , remote_file ):
374
- stdout = self .exec_command (f "test -f { remote_file } ; echo $?" )
374
+ stdout = self .exec_command ("test -f {}; echo $?" . format ( remote_file ) )
375
375
result = int (stdout .strip ())
376
376
return result == 0
377
377
378
378
def isdir (self , dirname ):
379
- cmd = f "if [ -d { dirname } ]; then echo True; else echo False; fi"
379
+ cmd = "if [ -d {} ]; then echo True; else echo False; fi" . format ( dirname )
380
380
response = self .exec_command (cmd )
381
381
return response .strip () == b"True"
382
382
383
383
def remove_file (self , filename ):
384
- cmd = f "rm { filename } "
384
+ cmd = "rm {}" . format ( filename )
385
385
return self .exec_command (cmd )
386
386
387
387
# Processes control
388
388
def kill (self , pid , signal ):
389
389
# Kill the process
390
- cmd = f "kill -{ signal } { pid } "
390
+ cmd = "kill -{} {}" . format ( signal , pid )
391
391
return self .exec_command (cmd )
392
392
393
393
def get_pid (self ):
394
394
# Get current process id
395
395
return int (self .exec_command ("echo $$" , encoding = 'utf-8' ))
396
396
397
397
def get_process_children (self , pid ):
398
- command = f "pgrep -P { pid } "
398
+ command = "pgrep -P {}" . format ( pid )
399
399
stdin , stdout , stderr = self .ssh .exec_command (command )
400
400
children = stdout .readlines ()
401
401
return [PsUtilProcessProxy (self .ssh , int (child_pid .strip ())) for child_pid in children ]
@@ -437,4 +437,4 @@ def db_connect(self, dbname, user, password=None, host="127.0.0.1", port=5432, s
437
437
return conn
438
438
except Exception as e :
439
439
self .tunnel .stop ()
440
- raise ExecUtilException ("Could not create db tunnel." )
440
+ raise ExecUtilException ("Could not create db tunnel. {}" . format ( e ) )
0 commit comments